ObjectManager.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #ifndef RTC_OBJECTMANAGER_H
00020 #define RTC_OBJECTMANAGER_H
00021 
00022 #include <rtm/RTC.h>
00023 
00024 #include <vector>
00025 #include <string>
00026 #include <algorithm>
00027 
00028 #include <coil/Mutex.h>
00029 #include <coil/Guard.h>
00030 
00050 template <typename Identifier, typename Object, typename Predicate>
00051 class ObjectManager
00052 {
00053 public:
00054   typedef std::vector<Object*>                  ObjectVector;
00055   typedef typename ObjectVector::iterator       ObjectVectorItr;
00056   typedef typename ObjectVector::const_iterator ObjectVectorConstItr;
00057   typedef coil::Mutex Mutex;
00058   typedef coil::Guard<coil::Mutex> Guard;
00074   ObjectManager(){};
00075   
00091   ~ObjectManager(void){};
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 // RTC_OBJECTMANAGER_H


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