OpenRTM-aist 2.0.2
Loading...
Searching...
No Matches
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 <mutex>
25
26#include <algorithm>
27#include <vector>
28#include <string>
29
30
50template <typename Identifier, typename Object, typename Predicate>
52{
53public:
54 using ObjectVector = std::vector<Object*>;
55 using ObjectVectorItr = typename ObjectVector::iterator;
56 using ObjectVectorConstItr = typename ObjectVector::const_iterator;
72 ObjectManager() = default;
73
89 ~ObjectManager() = default;
90
116 bool registerObject(Object* obj)
117 {
119 std::lock_guard<std::mutex> guard(m_objects._mutex);
120
121 it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
122 Predicate(obj));
123 if (it == m_objects._obj.end())
124 {
125 m_objects._obj.emplace_back(obj);
126 return true;
127 }
128 return false;
129 }
130
156 Object* unregisterObject(const Identifier& id)
157 {
159 std::lock_guard<std::mutex> guard(m_objects._mutex);
160
161 it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
162 Predicate(id));
163 if (it != m_objects._obj.end())
164 {
165 Object* obj(*it);
166 m_objects._obj.erase(it);
167 return obj;
168 }
169 return nullptr;
170 }
171
200 Object* find(const Identifier& id) const
201 {
203 std::lock_guard<std::mutex> guard(m_objects._mutex);
204 it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
205 Predicate(id));
206 if (it != m_objects._obj.end())
207 {
208 return *it;
209 }
210 return nullptr;
211 }
212
232 std::vector<Object*> getObjects() const
233 {
234 std::lock_guard<std::mutex> guard(m_objects._mutex);
235 return m_objects._obj;
236 }
237
245 template <class Pred>
246 Pred for_each(Pred p)
247 {
248 std::lock_guard<std::mutex> guard(m_objects._mutex);
249 return std::for_each(m_objects._obj.begin(), m_objects._obj.end(), p);
250 }
251
259 template <class Pred>
260 Pred for_each(Pred p) const
261 {
262 std::lock_guard<std::mutex> guard(m_objects._mutex);
263 return std::for_each(m_objects._obj.begin(), m_objects._obj.end(), p);
264 }
265
266protected:
274 struct Objects
275 {
276 ~Objects() = default;
277 mutable std::mutex _mutex;
279 };
288};
289#endif // RTC_OBJECTMANAGER_H
RTComponent header.
Class for managing objects.
Definition ObjectManager.h:52
std::vector< Object * > ObjectVector
Definition ObjectManager.h:54
~ObjectManager()=default
Destructor.
bool registerObject(Object *obj)
Register the specified object.
Definition ObjectManager.h:116
typename ObjectVector::iterator ObjectVectorItr
Definition ObjectManager.h:55
Pred for_each(Pred p)
Functor for searching object .
Definition ObjectManager.h:246
Object * unregisterObject(const Identifier &id)
Unregister the specified object.
Definition ObjectManager.h:156
ObjectManager()=default
Constructor.
Object * find(const Identifier &id) const
Find the object.
Definition ObjectManager.h:200
Pred for_each(Pred p) const
Functor for searching object .
Definition ObjectManager.h:260
typename ObjectVector::const_iterator ObjectVectorConstItr
Definition ObjectManager.h:56
Objects m_objects
The list of registered objects .
Definition ObjectManager.h:287
std::vector< Object * > getObjects() const
Get a list of obejects that are registerd.
Definition ObjectManager.h:232
The structure for object management .
Definition ObjectManager.h:275
std::mutex _mutex
Definition ObjectManager.h:277
ObjectVector _obj
Definition ObjectManager.h:278