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;
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 class.
Object * find(const Identifier &id) const
Find the object.
ObjectManager()
Constructor.
coil::Mutex Mutex
Objects m_objects
The list of registered objects.
Class for managing objects.
bool registerObject(Object *obj)
Register the specified object.
std::vector< Object * > ObjectVector
Object * unregisterObject(const Identifier &id)
Unregister the specified object.
~ObjectManager(void)
Destructor.
Pred for_each(Pred p)
Functor for searching object.
coil::Guard< coil::Mutex > Guard
RTComponent header.
ObjectVector::const_iterator ObjectVectorConstItr
std::vector< Object * > getObjects() const
Get a list of obejects that are registerd.
Pred for_each(Pred p) const
Functor for searching object.
ObjectVector::iterator ObjectVectorItr
Functor for_each(CorbaSequence &seq, Functor f)
Apply the functor to all CORBA sequence elements.
Definition: CORBA_SeqUtil.h:98


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:53