关于小项目的C ++内存和设计问题

问题描述 投票:-1回答:1

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

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

Github:Open Source Project

一个文件中的所有内容:

Device Class

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;

};

Devices Class

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;
    }
}
};

main

int main(int argc, const char *argv[])
{
std::shared_ptr<Devices> devices;

devices->printDeviceIDs();
devices->getDevicesArray();
devices->printDeviceIDs();
}

有人知道一个好的模式吗? 也许我使用智能指针完全错误? iOKit库也是从1985年开始的,所以它不是很具描述性......

提前致谢。

c++ design-patterns memory-management c++14
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.