OpenRTM
1.0.0
Main Page
Namespaces
Classes
Files
File List
File Members
tmp
buildd
ros-hydro-openrtm-aist-1.1.0-13quantal-20150213-1351
src
lib
rtm
ObjectManager.h
Go to the documentation of this file.
1
// -*- C++ -*-
19
#ifndef RTC_OBJECTMANAGER_H
20
#define RTC_OBJECTMANAGER_H
21
22
#include <
rtm/RTC.h
>
23
24
#include <vector>
25
#include <string>
26
#include <algorithm>
27
28
#include <
coil/Mutex.h
>
29
#include <
coil/Guard.h
>
30
50
template
<
typename
Identifier,
typename
Object,
typename
Predicate>
51
class
ObjectManager
52
{
53
public
:
54
typedef
std::vector<Object*>
ObjectVector
;
55
typedef
typename
ObjectVector::iterator
ObjectVectorItr
;
56
typedef
typename
ObjectVector::const_iterator
ObjectVectorConstItr
;
57
typedef
coil::Mutex
Mutex
;
58
typedef
coil::Guard<coil::Mutex>
Guard
;
74
ObjectManager
(){};
75
91
~ObjectManager
(
void
){};
92
118
bool
registerObject
(Object* obj)
119
{
120
ObjectVectorItr
it;
121
Guard
guard(
m_objects
.
_mutex
);
122
123
it = std::find_if(
m_objects
.
_obj
.begin(),
m_objects
.
_obj
.end(),
124
Predicate(obj));
125
if
(it ==
m_objects
.
_obj
.end())
126
{
127
m_objects
.
_obj
.push_back(obj);
128
return
true
;
129
}
130
return
false
;
131
}
132
158
Object*
unregisterObject
(
const
Identifier&
id
)
159
{
160
ObjectVectorItr
it;
161
Guard
guard(
m_objects
.
_mutex
);
162
163
it = std::find_if(
m_objects
.
_obj
.begin(),
m_objects
.
_obj
.end(),
164
Predicate(
id
));
165
if
(it !=
m_objects
.
_obj
.end())
166
{
167
Object* obj(*it);
168
m_objects
.
_obj
.erase(it);
169
return
obj;
170
}
171
return
NULL;;
172
}
173
202
Object*
find
(
const
Identifier&
id
)
const
203
{
204
ObjectVectorConstItr
it;
205
Guard
guard(
m_objects
.
_mutex
);
206
it = std::find_if(
m_objects
.
_obj
.begin(),
m_objects
.
_obj
.end(),
207
Predicate(
id
));
208
if
(it !=
m_objects
.
_obj
.end())
209
{
210
return
*it;
211
}
212
return
NULL;
213
}
214
234
std::vector<Object*>
getObjects
()
const
235
{
236
Guard
guard(
m_objects
.
_mutex
);
237
return
m_objects
.
_obj
;
238
}
239
247
template
<
class
Pred>
248
Pred
for_each
(Pred p)
249
{
250
Guard
guard(
m_objects
.
_mutex
);
251
return
std::for_each
(
m_objects
.
_obj
.begin(),
m_objects
.
_obj
.end(), p);
252
}
253
261
template
<
class
Pred>
262
Pred
for_each
(Pred p)
const
263
{
264
Guard
guard(
m_objects
.
_mutex
);
265
return
std::for_each
(
m_objects
.
_obj
.begin(),
m_objects
.
_obj
.end(), p);
266
}
267
268
protected
:
276
struct
Objects
277
{
278
mutable
Mutex
_mutex
;
279
ObjectVector
_obj
;
280
};
288
Objects
m_objects
;
289
};
290
#endif // RTC_OBJECTMANAGER_H
Generated on Fri Feb 13 2015 13:58:09 for OpenRTM by
1.8.1.2