00001 /* 00002 This file is part of the Virtual Object System of 00003 the Interreality project (http://interreality.org). 00004 00005 Copyright (C) 2001-2003 Peter Amstutz 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Lesser General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public 00018 License along with this library; if not, write to the Free Software 00019 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 00021 Peter Amstutz <tetron@interreality.org> 00022 */ 00023 #ifndef _LISTENER_HH_ 00024 #define _LISTENER_HH_ 00025 00026 /** @file 00027 Defines ChildChangeListener, ParentChangeListener, and TypeChangeListener. 00028 */ 00029 00030 #include <vos/corelibs/vos/vosdefs.hh> 00031 #include <vos/corelibs/vos/refcount.hh> 00032 00033 #include <string> 00034 00035 namespace VOS 00036 { 00037 class Vobject; 00038 00039 /** @class VobjectEvent listener.hh vos/corelibs/vos/vobject.hh 00040 * 00041 * This class describes an event changing the state of a Vobject. 00042 This event has either been requested to happen or has already 00043 happened, depending respectively on whether it is supplied as part 00044 of an access control callback or a listener notification callback. 00045 @note A VobjectEvent is a reference counted object. This means 00046 that you may keep a reference to an event which has been passed to 00047 you if you increment its count with acquire(). 00048 */ 00049 class VOS_API VobjectEvent : public RefCounted 00050 { 00051 public: 00052 typedef enum {TypeInsert, TypeRemove, ParentInsert, ParentRemove, 00053 ChildInsert, ChildReplace, ChildRemove, 00054 ReadChild, ReadParent, ReadType, 00055 ChildrenListen, ParentListen} EventType; 00056 private: 00057 EventType event; 00058 Vobject* initiator; 00059 Vobject* from; 00060 int position; 00061 string newstring; // used for both contextual name and new type events 00062 Vobject* newchild; 00063 Vobject* oldchild; 00064 public: 00065 /** Construct a ReadChild, ReadParent, ChildInserted, 00066 ChildRemoved, ParentInserted or ParentRemoved event. 00067 00068 @param et The type of event that occured 00069 @param init The object which caused this event to occur 00070 @param fromobj The parent object involved 00071 @param pos The position of the child object in question 00072 @param contextname The contextual name of the child object in question 00073 @param childobj The child object involved 00074 */ 00075 VobjectEvent(EventType et, Vobject& init, 00076 Vobject& fromobj, int pos, const string& contextname, 00077 Vobject& childobj); 00078 00079 /** Construct a ChildReplaced event. 00080 @param et The type of event that occured 00081 @param init The object which caused this event to occur 00082 @param fromobj The parent object 00083 @param pos The position of the child object in question 00084 @param contextname The contextual name of the child object in question 00085 @param newobject The new child at this position 00086 @param oldobj The old child at this position 00087 */ 00088 VobjectEvent(EventType et, Vobject& init, 00089 Vobject& fromobj, int pos, const string& contextname, 00090 Vobject& newobj, Vobject& oldobj); 00091 00092 /** Construct a ReadType, TypeInserted or TypeRemoved event. 00093 @param et The type of event that occured 00094 @param init The object which caused this event to occur 00095 @param fromobj The parent object of this child change event 00096 @param type The newly added type string 00097 */ 00098 VobjectEvent(EventType et, Vobject& init, Vobject& fromobject, const string& type); 00099 00100 /** Construct a ChildrenListen or ParentListen event 00101 @param et The type of event that occured 00102 @param requester The object which wants to start listening 00103 @param fromobj The object that will be listened to 00104 */ 00105 VobjectEvent(EventType et, Vobject& requester, Vobject& fromobject); 00106 00107 virtual ~VobjectEvent(); 00108 00109 /** @return What type of event this is. */ 00110 EventType getEvent() { return event; } 00111 00112 /** @return The object which requested or initiated this event. 00113 Always valid. For access control checks, this is the object 00114 which is requesting access. Note that unlike most parts of 00115 the API the reference count on the returned object is NOT 00116 increased (acquire is NOT called) so if you plan on keeping a 00117 reference to it past the lifetime of the callback, you must 00118 call acquire yourself! This also means you should NOT assign 00119 the returned value to a vRef! 00120 */ 00121 Vobject* getInitiator() { return initiator; } 00122 00123 /** @return The parent Vobject being changed. Always a vaild 00124 object. Note that unlike most parts of the API the reference 00125 count on the returned object is NOT increased (acquire is NOT 00126 called) so if you plan on keeping a reference to it past the 00127 lifetime of the callback, you must call acquire yourself! 00128 This also means you should NOT assign the returned value to a 00129 vRef! */ 00130 Vobject* getParent() { return from; } 00131 00132 /** @return The Vobject which is changing state. Always a valid 00133 object. Note that unlike most parts of the API the reference 00134 count on the returned object is NOT increased (acquire is NOT 00135 called) so if you plan on keeping a reference to it past the 00136 lifetime of the callback, you must call acquire yourself! 00137 This also means you should NOT assign the returned value to a 00138 vRef! 00139 */ 00140 Vobject* getAffectedObject() { return from; } 00141 00142 /** @return The child Vobject which was affected on a ParentInsert 00143 or ParentRemove event, or the requested object to read on a 00144 ReadChild event. Will be NULL for TypeInsert, TypeRemove and 00145 all Listen events. Note that unlike most parts of the API the 00146 reference count on the returned object is NOT increased 00147 (acquire is NOT called) so if you plan on keeping a reference 00148 to it past the lifetime of the callback, you must call acquire 00149 yourself! This also means you should NOT assign the returned 00150 value to a vRef! */ 00151 Vobject* getChild() { return newchild; } 00152 00153 /** @return The child Vobject which was newly added on a 00154 ChildInsert or ChildReplace event. Will be NULL for 00155 ChildRemove, TypeInsert, TypeRemove and all Read and Listen 00156 events. Note that unlike most parts of the API the reference 00157 count on the returned object is NOT increased (acquire is NOT 00158 called) so if you plan on keeping a reference to it past the 00159 lifetime of the callback, you must call acquire yourself! 00160 This also means you should NOT assign the returned value to a 00161 vRef! */ 00162 Vobject* getNewChild() { return newchild; } 00163 00164 /** @return The previous Vobject occupying this position. Only 00165 valid for ChildRemove and ChildReplace events, NULL otherwise. 00166 Note that unlike most parts of the API the reference count on 00167 the returned object is NOT increased (acquire is NOT called) 00168 so if you plan on keeping a reference to it past the lifetime 00169 of the callback, you must call acquire yourself! This also 00170 means you should NOT assign the returned value to a vRef! 00171 */ 00172 Vobject* getOldChild() { return oldchild; } 00173 00174 /** @return The absolute position of the child Vobject in parent, or -1 if this is a 00175 TypeInserted or TypeRemoved event.*/ 00176 int getPosition() { return position; } 00177 00178 /** @return The contextual name of the child Vobject. */ 00179 const string& getContextualName() { return newstring; } 00180 00181 /** @return The added or removed type for type change events. */ 00182 const string& getNewType() { return newstring; } 00183 }; 00184 00185 00186 /** @class ChildChangeListener listener.hh vos/corelibs/vos/vobject.hh 00187 * 00188 * Interface to be called when a listened-to object has some 00189 * change to its child list. 00190 */ 00191 class VOS_API ChildChangeListener 00192 { 00193 public: 00194 /** Called when a child has been inserted into the listened-to children list */ 00195 virtual void notifyChildInserted(VobjectEvent& e) = 0; 00196 00197 /** Called when a child has replaced another child in the listened-to children list */ 00198 virtual void notifyChildReplaced(VobjectEvent& e) = 0; 00199 00200 /** Called when a child has been deleted in the listened-to children list */ 00201 virtual void notifyChildRemoved(VobjectEvent& e) = 0; 00202 }; 00203 00204 /** @class ParentChangeListener listener.hh vos/corelibs/vos/vobject.hh 00205 * Interface to be called when a listened-to object has some 00206 change to its parent set. 00207 */ 00208 class VOS_API ParentChangeListener 00209 { 00210 public: 00211 /** Called when an object has aquired a new parent. */ 00212 virtual void notifyParentInserted(VobjectEvent& e) = 0; 00213 00214 /** Called when an object has lost a parent. */ 00215 virtual void notifyParentRemoved(VobjectEvent& e) = 0; 00216 }; 00217 00218 /** @class TypeChangeListener listener.hh vos/corelibs/vos/vobject.hh 00219 * Interface to be called when a listened-to object has some 00220 change to its type set. 00221 */ 00222 class VOS_API TypeChangeListener 00223 { 00224 public: 00225 /** Called when an object has gained a type. */ 00226 virtual void notifyTypeInserted(VobjectEvent& e) = 0; 00227 00228 /** Called when an object has lost a type. */ 00229 virtual void notifyTypeRemoved(VobjectEvent& e) = 0; 00230 }; 00231 00232 /** @class DoNothingListener listener.hh vos/corelibs/vos/vobject.hh 00233 * Trivial listener implementation that does nothing. 00234 * It can be useful, however, to keep a local cache of remote objects up to 00235 * date. 00236 */ 00237 class VOS_API DoNothingListener : public ChildChangeListener, 00238 public ParentChangeListener, 00239 public TypeChangeListener 00240 { 00241 public: 00242 static DoNothingListener static_; 00243 00244 virtual void notifyChildInserted(VobjectEvent& /*e*/) { } 00245 virtual void notifyChildReplaced(VobjectEvent& /*e*/) { } 00246 virtual void notifyChildRemoved(VobjectEvent& /*e*/) { } 00247 virtual void notifyParentInserted(VobjectEvent& /*e*/) { } 00248 virtual void notifyParentRemoved(VobjectEvent& /*e*/) { } 00249 virtual void notifyTypeInserted(VobjectEvent& /*e*/) { } 00250 virtual void notifyTypeRemoved(VobjectEvent& /*e*/) { } 00251 }; 00252 } 00253 00254 #endif