forked from minodekit/pxt-minode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiNodeModulePool.h
80 lines (59 loc) · 1.42 KB
/
MiNodeModulePool.h
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
#ifndef MINODE_MODULE_POOL_H
#define MINODE_MODULE_POOL_H
#include "mbed.h"
#include <vector>
#include "MicroBitConfig.h"
#include "MicroBitComponent.h"
#include "MicroBitEvent.h"
#include "MiNodeComponent.h"
#include "MiNodeConn.h"
template<class T>
class MiNodeModulePool {
public:
MiNodeModulePool() {
}
~MiNodeModulePool() {
typename std::vector<T*>::iterator it;
for(it = modules.begin(); it != modules.end(); it++) {
T* pModule = *it;
delete pModule;
}
}
T* attach(ConnName conn) {
typename std::vector<T*>::iterator it;
T* findModule = NULL;
for(it = modules.begin(); it != modules.end(); it++) {
T* pModule = *it;
if(conn == pModule->getConnector()) {
findModule = pModule;
break;
}
}
if(findModule == NULL) {
findModule = new T();
findModule->attach(conn);
modules.push_back(findModule);
}
return findModule;
}
T* attach(AnalogConnName conn) {
typename std::vector<T*>::iterator it;
T* findModule = NULL;
for(it = modules.begin(); it != modules.end(); it++) {
T* pModule = *it;
if(conn == pModule->AgetConnector()) {
findModule = pModule;
break;
}
}
if(findModule == NULL) {
findModule = new T();
findModule->attach(conn);
modules.push_back(findModule);
}
return findModule;
}
private:
std::vector<T*> modules;
};
#endif