OpenRTM-aist  1.2.1
ObjectManager.h
[詳解]
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>
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;
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
Mutex クラス
Definition: Mutex.h:40
Object * find(const Identifier &id) const
オブジェクトを検索する
Definition: ObjectManager.h:202
ObjectManager()
コンストラクタ
Definition: ObjectManager.h:74
ObjectVector _obj
Definition: ObjectManager.h:279
coil::Mutex Mutex
Definition: ObjectManager.h:57
Objects m_objects
登録済みオブジェクト・リスト
Definition: ObjectManager.h:288
オブジェクト管理用クラス
Definition: ObjectManager.h:51
bool registerObject(Object *obj)
指定したオブジェクトを登録する
Definition: ObjectManager.h:118
std::vector< Object * > ObjectVector
Definition: ObjectManager.h:54
Guard template class.
Object * unregisterObject(const Identifier &id)
指定したオブジェクトを登録解除する
Definition: ObjectManager.h:158
~ObjectManager(void)
デストラクタ
Definition: ObjectManager.h:91
Mutex _mutex
Definition: ObjectManager.h:278
Pred for_each(Pred p)
オブジェクト検索用ファンクタ
Definition: ObjectManager.h:248
coil::Guard< coil::Mutex > Guard
Definition: ObjectManager.h:58
オブジェクト管理用構造体
Definition: ObjectManager.h:276
RTComponent header.
ObjectVector::const_iterator ObjectVectorConstItr
Definition: ObjectManager.h:56
std::vector< Object * > getObjects() const
登録されているオブジェクトのリストを取得する
Definition: ObjectManager.h:234
Pred for_each(Pred p) const
オブジェクト検索用ファンクタ
Definition: ObjectManager.h:262
ObjectVector::iterator ObjectVectorItr
Definition: ObjectManager.h:55
Functor for_each(CorbaSequence &seq, Functor f)
CORBA sequence に対して functor を適用する
Definition: CORBA_SeqUtil.h:98