首页 > 解决方案 > 小项目的C++内存和设计问题

问题描述

我尝试创建一个小型库来监听 MAC 和 PC 上的多个鼠标。(现在是 MAC)

我已经开始了一些简单的ATM 不起作用。由于我是 C++ 的菜鸟,我想在这件事上向社区寻求帮助。我应该如何在代码中设计它?我想在这里使用智能指针是我的代码,可以免费下载:

Github: 开源项目

一个文件中的所有内容:

设备类

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 年的,所以它不是很有描述性......

提前致谢。

标签: c++design-patternsmemory-managementc++14

解决方案


正如其他用户所提到的,这更多的是审查而不是错误代码问题;话虽如此,我唯一能找到的是以下内容:

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();

推荐阅读