我尝试创建一个小型库来侦听MAC和PC上的多个鼠标。 (现在是MAC)
我已经开始一些无法使用ATM的简单操作。由于我是C ++的新手,因此我想向社区寻求帮助。我应该如何在代码中设计它?我想使用智能指针,这是我的代码,feedl可免费下载它:
Github: Open Source Project
所有内容都放在一个文件中:
class Device;
class Device {
public:
Device(){
std::cout << "###### Create Device - Empty" << std::endl;
this->x_previous = 0;
this->y_previous = 0;
this->x_current = 0;
this->y_current = 0;
}
Device( size_t _deviceID, std::string _device_name){
std::cout << "###### Create Device - 0.0/0.0" << std::endl;
this->device_id = std::make_shared<size_t>(_deviceID);
this->device_name = std::make_shared<std::string>(_device_name);
this->x_previous = 0;
this->y_previous = 0;
this->x_current = 0;
this->y_current = 0;
}
Device(size_t _deviceID, std::string _device_name, float _xStart, float _yStart){
std::cout << "###### Create Device - " << _xStart << "/" << _yStart << std::endl;
this->device_id = std::make_shared<size_t>(_deviceID);
this->device_name = std::make_shared<std::string>(_device_name);
this->x_previous = _xStart;
this->y_previous = _yStart;
this->x_current = _xStart;
this->y_current = _yStart;
}
~Device(){
std::cout << "###### Destroyed Device" << std::endl;
}
const size_t getId () const{
return (size_t)this->device_id.get();
};
const std::string getName() const{
return "Not Implementet yet"; //this->device_name.get() does not work because of std::basic_string wtf?
};
const float getDeltaX() const{
return x_previous - x_current;
};
const float getDeltaY() const{
return y_previous - y_current;
};
private:
std::shared_ptr<size_t> device_id;
std::shared_ptr<std::string> device_name;
float x_previous;
float y_previous;
float x_current;
float y_current;
};
class Devices{
public:
Devices(){
std::cout << "###### Created Empty Devices List" << std::endl;
this->list = std::unique_ptr<std::list<Device> >();
}
explicit Devices(std::unique_ptr<std::list<Device> > _list){
std::cout << "###### Created Moved Devices List" << std::endl;
this->list = std::move(_list);
}
~Devices(){
std::cout << "###### Destroyed Devices List" << std::endl;
}
std::unique_ptr<std::list<Device> > list;
void getDevicesArray() {
CFMutableDictionaryRef usb_dictionary;
io_iterator_t io_device_iterator;
kern_return_t assembler_kernel_return_value;
io_service_t device_id;
// set up a matching dictionary for the class
usb_dictionary = IOServiceMatching(kIOUSBDeviceClassName);
if (usb_dictionary == NULL) {
std::cout << "failed to fetch USB dictionary" << std::endl;
return; // still empty
}
// Now we have a dictionary, get an iterator.
assembler_kernel_return_value = IOServiceGetMatchingServices(kIOMasterPortDefault, usb_dictionary, &io_device_iterator);
if (assembler_kernel_return_value != KERN_SUCCESS) {
std::cout << "failed to get a kern_return" << std::endl;
return; // still empty
}
io_name_t device_name = "unkown device";
device_id = IOIteratorNext(io_device_iterator); // getting first device
while (device_id) {
device_id = IOIteratorNext(io_device_iterator); //set id type: io_service_t
IORegistryEntryGetName(device_id, device_name); //set name type: io_name_t
this->list.get()->push_back(Device(device_id, device_name));
}
//Done, release the iterator
IOObjectRelease(io_device_iterator);
}
void printDeviceIDs(){
for (auto const& device : *this->list.get()) {
std::cout << "#" << device.getId() << std::endl;
std::cout << "| name: " << "\t" << device.getName() << std::endl;
std::cout << "#-----------------------------------------------#" << std::endl;
}
}
};
int main(int argc, const char *argv[])
{
std::shared_ptr<Devices> devices;
devices->printDeviceIDs();
devices->getDevicesArray();
devices->printDeviceIDs();
}
有人知道有一个好的模式吗?
还可能我使用智能指针完全错误吗?
而且iOKit库来自1985或类似的东西,所以它不是很具描述性...
谢谢。
答案 0 :(得分:1)
正如其他用户所提到的,这更多的是回顾,而不是错误代码问题; 话虽如此,我唯一能找到的是以下内容:
std::unique_ptr<std::list<Device> > list;
您应该将其更改为以下内容:
std::list<Device> list;
和您的主要
int main(int argc, const char *argv[])
{
std::shared_ptr<Devices> devices;
devices->printDeviceIDs();
devices->getDevicesArray();
devices->printDeviceIDs();
} 应该应该足够使用了:
Devices devices; devices.printDeviceIDs();