ObjectManager.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #ifndef ObjectManager_h
00020 #define ObjectManager_h
00021 
00022 #include <vector>
00023 #include <string>
00024 #include <algorithm>
00025 
00026 #include <coil/Mutex.h>
00027 #include <coil/Guard.h>
00028 
00048 template <typename Identifier, typename Object, typename Predicate>
00049 class ObjectManager
00050 {
00051 public:
00052   typedef std::vector<Object*>                  ObjectVector;
00053   typedef typename ObjectVector::iterator       ObjectVectorItr;
00054   typedef typename ObjectVector::const_iterator ObjectVectorConstItr;
00055   typedef coil::Mutex Mutex;
00056   typedef coil::Guard<coil::Mutex> Guard;
00057 
00058 
00074   ObjectManager(){};
00075   
00091   ~ObjectManager(){};
00092   
00118   bool registerObject(Object* obj)
00119   {
00120     ObjectVectorItr it;
00121     Guard guard(m_objects._mutex);
00122     
00123     it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
00124                       Predicate(obj));
00125     if (it == m_objects._obj.end())
00126       {
00127         m_objects._obj.push_back(obj);
00128         return true;
00129       }
00130     return false;
00131   }
00132   
00158   Object* unregisterObject(const Identifier& id)
00159   {
00160     ObjectVectorItr it;
00161     Guard guard(m_objects._mutex);
00162     
00163     it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
00164                       Predicate(id));
00165     if (it != m_objects._obj.end())
00166       {
00167         Object* obj(*it);
00168         m_objects._obj.erase(it);
00169         return obj;
00170       }
00171     return NULL;;
00172   }
00173   
00202   Object* find(const Identifier& id) const
00203   {
00204     ObjectVectorConstItr it;
00205     Guard guard(m_objects._mutex);
00206     it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
00207                       Predicate(id));
00208     if (it != m_objects._obj.end())
00209       {
00210         return *it;
00211       }
00212     return NULL;
00213   }
00214   
00234   std::vector<Object*> getObjects() const
00235   {
00236     Guard guard(m_objects._mutex);
00237     return m_objects._obj;
00238   }
00239   
00247   template <class Pred>
00248   Pred for_each(Pred p)
00249   {
00250     Guard guard(m_objects._mutex);
00251     return std::for_each(m_objects._obj.begin(), m_objects._obj.end(), p);
00252   }
00253   
00261   template <class Pred>
00262   Pred for_each(Pred p) const
00263   {
00264     Guard guard(m_objects._mutex);
00265     return std::for_each(m_objects._obj.begin(), m_objects._obj.end(), p);
00266   }
00267   
00268 protected:
00276   struct Objects
00277   {
00278     mutable Mutex _mutex;
00279     ObjectVector _obj;
00280   };
00288   Objects m_objects;
00289 };
00290 #endif // ObjectManager_h


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:05