OpenRTM-aist  2.1.0
ObjectManager.h
[詳解]
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 
50 template <typename Identifier, typename Object, typename Predicate>
52 {
53 public:
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  {
118  ObjectVectorItr it;
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  {
158  ObjectVectorItr it;
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 
266 protected:
274  struct Objects
275  {
276  ~Objects() = default;
277  mutable std::mutex _mutex;
279  };
288 };
289 #endif // RTC_OBJECTMANAGER_H
RTComponent header
オブジェクト管理用クラス
Definition: ObjectManager.h:52
Object * unregisterObject(const Identifier &id)
指定したオブジェクトを登録解除する
Definition: ObjectManager.h:156
std::vector< Object * > ObjectVector
Definition: ObjectManager.h:54
~ObjectManager()=default
デストラクタ
bool registerObject(Object *obj)
指定したオブジェクトを登録する
Definition: ObjectManager.h:116
typename ObjectVector::iterator ObjectVectorItr
Definition: ObjectManager.h:55
Pred for_each(Pred p)
オブジェクト検索用ファンクタ
Definition: ObjectManager.h:246
Object * find(const Identifier &id) const
オブジェクトを検索する
Definition: ObjectManager.h:200
ObjectManager()=default
コンストラクタ
Pred for_each(Pred p) const
オブジェクト検索用ファンクタ
Definition: ObjectManager.h:260
typename ObjectVector::const_iterator ObjectVectorConstItr
Definition: ObjectManager.h:56
Objects m_objects
登録済みオブジェクト・リスト
Definition: ObjectManager.h:287
std::vector< Object * > getObjects() const
登録されているオブジェクトのリストを取得する
Definition: ObjectManager.h:232
Functor for_each(CorbaSequence &seq, Functor f)
CORBA sequence に対して functor を適用する
Definition: CORBA_SeqUtil.h:98
オブジェクト管理用構造体
Definition: ObjectManager.h:275
std::mutex _mutex
Definition: ObjectManager.h:277
ObjectVector _obj
Definition: ObjectManager.h:278