doil/ObjectManager.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef ObjectManager_h
20 #define ObjectManager_h
21 
22 #include <vector>
23 #include <string>
24 #include <algorithm>
25 
26 #include <coil/Mutex.h>
27 #include <coil/Guard.h>
28 
48 template <typename Identifier, typename Object, typename Predicate>
50 {
51 public:
52  typedef std::vector<Object*> ObjectVector;
53  typedef typename ObjectVector::iterator ObjectVectorItr;
54  typedef typename ObjectVector::const_iterator ObjectVectorConstItr;
55  typedef coil::Mutex Mutex;
57 
58 
75 
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 // ObjectManager_h
Mutex class.
Object * find(const Identifier &id) const
Find the object.
ObjectManager()
Constructor.
coil::Mutex Mutex
~ObjectManager()
Destructor.
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.
Pred for_each(Pred p)
Functor for searching object.
coil::Guard< coil::Mutex > Guard
The structure for object management.
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 Thu Jun 6 2019 19:25:59