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