summaryrefslogtreecommitdiff
path: root/CDAL/CommDeviceManager.h
blob: fef36de91ea8b4814c4961958e9fab6deae41ea6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * CommDeviceManager.h
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

#pragma once

#include "CDAL.h"
#include "CommDevice.h"
#include <vector>
#include <libusb.h>

class CommDeviceManager
{
public:
    static const std::vector<CommDevice*>& getAllDevices();

    static void destroyDevice(CommDevice* device);
    static void destroyAll();

    template <class T, class U>
    static T* createDevice(U port) {
        T* device = new T(port);

        T* existingDevice = dynamic_cast<T*>(getDevice<T>(port));

        if (0 != existingDevice) {
            destroyDevice(existingDevice);
        }

        devices_.push_back(device);

        return device;
    }

    template <class T, class U>
    static T* getDevice(U port) {
        T* device;

        for (std::vector<CommDevice*>::iterator i = devices_.begin(); i != devices_.end(); ++i) {
            device = dynamic_cast<T*>(*i);

            if (0 != device && device->getPort() == port) {
                return device;
            }
        }

        return 0;
    }


private:
    static std::vector<CommDevice*> devices_;
private:
    CommDeviceManager() {}
    CommDeviceManager(const CommDeviceManager&) {}
    ~CommDeviceManager() {}
};

// template specialization used to get device for LCD object mapping
template <>
inline CommDevice* CommDeviceManager::getDevice<CommDevice, void*>(void* object)
{
    for (std::vector<CommDevice*>::iterator i = devices_.begin(); i != devices_.end(); ++i) {
        if ((*i)->getObject() == object) {
            return *i;
        }
    }

    return 0;
}