00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _METAOBJECT_HH_
00024 #define _METAOBJECT_HH_
00025
00026 #include <stdexcept>
00027 #include <typeinfo>
00028 #include <vos/corelibs/vos/vosdefs.hh>
00029 #include <vos/corelibs/vos/vobject.hh>
00030
00031 namespace VOS
00032 {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 class VOS_API MetaObject : public virtual Vobject
00045 {
00046 private:
00047 std::deque<MetaObject*> typehandlers;
00048 std::set<std::string> touchedNonces;
00049 protected:
00050 MetaObject(MetaObject* superobject);
00051 MetaObject* superobject;
00052
00053
00054
00055
00056
00057
00058
00059
00060 virtual void doSaveState(MessageBlock& output, std::set<std::string>& types, bool portable);
00061
00062
00063
00064
00065 virtual void doExcise();
00066
00067 public:
00068
00069 virtual ~MetaObject();
00070
00071
00072
00073
00074
00075
00076
00077
00078 virtual void addObjectExtension(MetaObject* moe);
00079
00080
00081
00082
00083 virtual const std::string getType();
00084
00085
00086
00087
00088
00089 MetaObject* getSuperObject();
00090
00091
00092
00093
00094 MetaObject* getTopObject();
00095
00096
00097
00098
00099 const std::deque<MetaObject*>& getTypeHandlers();
00100
00101
00102
00103
00104 virtual void initializeSecurity(Vobject& requester);
00105
00106 virtual const std::string& getName();
00107 virtual Site& getSite();
00108 virtual const URL& getURL();
00109 virtual bool isLocal();
00110 virtual bool isRemote();
00111 virtual const TypeSet& getTypes() throw (AccessControlError, RemoteError);
00112 virtual void addType(const std::string& s);
00113 virtual const ParentSet& getParents()
00114 throw (AccessControlError, RemoteError);
00115 virtual const ChildList& getChildren()
00116 throw (AccessControlError, RemoteError);
00117
00118 virtual void sendMessage(Message* m);
00119 virtual void sendMessage(MessageBlock* m);
00120 virtual Message* receiveMessage() throw (MessageQueueEmptyError);
00121 virtual bool hasMessageAvailable();
00122
00123 virtual void sendUpdateMessage(Message* m);
00124 virtual Message* receiveUpdateMessage() throw (MessageQueueEmptyError);
00125 virtual bool hasUpdateMessageAvailable();
00126
00127 virtual Vobject& findObject(const std::string& path)
00128 throw (NoSuchSiteError, NoSuchObjectError, URL::BadURLError, AccessControlError, RemoteError);
00129 virtual ParentChildRelation& findChild(const std::string& path)
00130 throw (NoSuchObjectError, AccessControlError, RemoteError);
00131 virtual Vobject::ParentChildRelation& findParent(Vobject& parent)
00132 throw (NoSuchObjectError, AccessControlError, RemoteError);
00133 virtual void setChild(int position, const std::string& contextual_name, Vobject* child)
00134 throw (AccessControlError, RemoteError);
00135 virtual void insertChild(int position, const std::string& contextual_name, Vobject* child)
00136 throw (AccessControlError, RemoteError);
00137 virtual void removeChild(int position)
00138 throw (AccessControlError, RemoteError);
00139
00140 virtual void addTypeListener(TypeChangeListener* tl, bool notifyImmediately = true);
00141 virtual void addParentListener(ParentChangeListener* pl, bool notifyImmediately = true);
00142 virtual void addChildListener(ChildChangeListener* cl, bool notifyImmediately = true);
00143 virtual void removeTypeListener(TypeChangeListener* tl);
00144 virtual void removeParentListener(ParentChangeListener* pl);
00145 virtual void removeChildListener(ChildChangeListener* cl);
00146
00147 virtual void saveState(MessageBlock& output, std::set<std::string>& types, bool portable);
00148
00149 virtual void addFlag(const std::string& flag);
00150 virtual void removeFlag(const std::string& flag);
00151 virtual bool checkFlag(const std::string& flag);
00152
00153
00154
00155
00156
00157
00158
00159
00160 virtual void excise();
00161
00162
00163 virtual void acquire();
00164
00165
00166 virtual void release();
00167
00168
00169 virtual int getCount();
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 virtual void initialize();
00185
00186
00187
00188
00189
00190
00191
00192
00193 template<class C> static C meta_cast(Vobject* v)
00194 {
00195 if(C ret = dynamic_cast<C>(v)) return ret;
00196 else if(MetaObject* m = dynamic_cast<MetaObject*>(v)) return meta_cast<C>(m);
00197 else return 0;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207 template<class C> static C meta_cast(Vobject& v) throw (bad_cast)
00208 {
00209 try {
00210 C ret = dynamic_cast<C>(v);
00211 return ret;
00212 } catch(bad_cast) {
00213 MetaObject& m = dynamic_cast<MetaObject&>(v);
00214 return meta_cast<C>(m);
00215 }
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 template<class C> static C meta_cast(MetaObject* v, bool fromsuper=false)
00236 {
00237 if(!fromsuper && v->superobject != 0) return meta_cast<C>(v->superobject, false);
00238 if(C ret = dynamic_cast<C>(v)) {
00239 return ret;
00240 } else {
00241 for(std::deque<MetaObject*>::iterator i=v->typehandlers.begin();
00242 i != v->typehandlers.end();
00243 i++) {
00244 if(C ret = meta_cast<C>(*i, true)) return ret;
00245 }
00246 }
00247 return 0;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 template<class C> static C meta_cast(MetaObject& v, bool fromsuper=false) throw (bad_cast)
00265 {
00266 if(!fromsuper && v.superobject != 0) {
00267 return meta_cast<C>(*(v.superobject), false);
00268 }
00269 try {
00270 return dynamic_cast<C>(v);
00271 } catch(bad_cast) {
00272 for(std::deque<MetaObject*>::iterator i=v.typehandlers.begin();
00273 i != v.typehandlers.end();
00274 i++) {
00275 try {
00276 return meta_cast<C>(**i, true);
00277 } catch(bad_cast){
00278 }
00279 }
00280 } catch(runtime_error) {
00281 } catch(...) {
00282 }
00283 throw bad_cast();
00284 }
00285
00286 template<class T> void addMessageHandler(const std::string& method, T* theobj,
00287 void (T::* MessageHandler)(Message*),
00288 bool addExHandler = false)
00289 {
00290 dynamic_cast<VobjectImplementation*>(getTopObject())->addMessageHandler<T>(method, theobj,
00291 MessageHandler, addExHandler);
00292 };
00293 template<class T> void addUpdateHandler(const std::string& method, T* theobj,
00294 void (T::* MessageHandler)(Message*),
00295 bool addExHandler = false)
00296 {
00297 dynamic_cast<VobjectImplementation*>(getTopObject())->addUpdateHandler<T>(method, theobj,
00298 MessageHandler, addExHandler);
00299 };
00300 template<class T> void removeMessageHandler(const std::string& method, T* theobj,
00301 void (T::* MessageHandler)(Message*))
00302 {
00303 dynamic_cast<VobjectImplementation*>(getTopObject())->removeMessageHandler<T>(method, theobj, MessageHandler);
00304 };
00305 template<class T> void removeUpdateHandler(const std::string& method, T* theobj,
00306 void (T::* MessageHandler)(Message*))
00307 {
00308 dynamic_cast<VobjectImplementation*>(getTopObject())->removeUpdateHandler<T>(method, theobj, MessageHandler);
00309 };
00310 };
00311
00312
00313
00314
00315
00316
00317
00318 #define CREATE_METAOBJECT(SITE, CLASS) VOS::MetaObject::meta_cast<CLASS*>((SITE)->createMetaObject(#CLASS, typeid(CLASS).name(), 0))
00319
00320 }
00321
00322 #endif