jeudi 30 août 2018

C++ memory and design question on small project

I try to create a small library to listen for multiple mice on MAC and PC. (right now MAC)

I have started something simple that does not work ATM. Since I am a noob in C++ I wanted to ask the community for help in this matter. How should I design it in code? I wanted to use smart pointers here is my code, feedl free to download it:

Github: Open Source Project

Everything in one file:

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

Someone knows of a good pattern for that?
Also maybe I use smart pointers completely wrong?
Also the iOKit library is from 1985 or something so it is not very descriptive...

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire