00001
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #ifndef ObjectManager_h
00048 #define ObjectManager_h
00049
00050 #include <rtm/RTC.h>
00051
00052 #include <vector>
00053 #include <string>
00054 #include <algorithm>
00055
00056 #include <ace/Thread.h>
00057 #include <ace/Synch.h>
00058
00059 template <typename Identifier, typename Object, typename Predicate>
00060 class ObjectManager
00061 {
00062 public:
00063 typedef std::vector<Object*> ObjectVector;
00064 typedef typename ObjectVector::iterator ObjectVectorItr;
00065 typedef typename ObjectVector::const_iterator ObjectVectorConstItr;
00066
00067
00068 ObjectManager(){};
00069
00070
00071 ~ObjectManager(){};
00072
00073
00074 bool registerObject(Object* obj)
00075 {
00076 ObjectVectorItr it;
00077 ACE_Guard<ACE_Thread_Mutex> guard(m_objects._mutex);
00078
00079 it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
00080 Predicate(obj));
00081 if (it == m_objects._obj.end())
00082 {
00083 m_objects._obj.push_back(obj);
00084 return true;
00085 }
00086 return false;
00087 }
00088
00089
00090 Object* unregisterObject(const Identifier& id)
00091 {
00092 ObjectVectorItr it;
00093 ACE_Guard<ACE_Thread_Mutex> guard(m_objects._mutex);
00094
00095 it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
00096 Predicate(id));
00097 if (it != m_objects._obj.end())
00098 {
00099 Object* obj(*it);
00100 m_objects._obj.erase(it);
00101 return obj;
00102 }
00103 return NULL;;
00104 }
00105
00106
00107 Object* find(const Identifier& id) const
00108 {
00109 ObjectVectorConstItr it;
00110 ACE_Guard<ACE_Thread_Mutex> guard(m_objects._mutex);
00111 it = std::find_if(m_objects._obj.begin(), m_objects._obj.end(),
00112 Predicate(id));
00113 if (it != m_objects._obj.end())
00114 {
00115 return *it;
00116 }
00117 return NULL;
00118 }
00119
00120
00121 std::vector<Object*> getObjects() const
00122 {
00123 ACE_Guard<ACE_Thread_Mutex> guard(m_objects._mutex);
00124 return m_objects._obj;
00125 }
00126
00127
00128 template <class Pred>
00129 Pred for_each(Pred p)
00130 {
00131 ACE_Guard<ACE_Thread_Mutex> guard(m_objects._mutex);
00132 return std::for_each(m_objects._obj.begin(), m_objects._obj.end(), p);
00133 }
00134
00135 template <class Pred>
00136 Pred for_each(Pred p) const
00137 {
00138 ACE_Guard<ACE_Thread_Mutex> guard(m_objects._mutex);
00139 return std::for_each(m_objects._obj.begin(), m_objects._obj.end(), p);
00140 }
00141
00142 protected:
00143 struct Objects
00144 {
00145 mutable ACE_Thread_Mutex _mutex;
00146 ObjectVector _obj;
00147 };
00148 Objects m_objects;
00149 };
00150
00151 #endif // ObjectManager_h