interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/libs/vos/vos/propertylistener.hh

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001, 2002 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 
00022     Additions by Reed Hedges  marked with initials "rh" and date.
00023 */
00024 #ifndef _PROPERTY_LISTENER_HH_
00025 #define _PROPERTY_LISTENER_HH_
00026 
00027 #include <vos/vos/vos.hh>
00028 #include <vos/vutil/refcount.hh>
00029 #include <vos/vos/property.hh>
00030 
00031 namespace VOS {
00032     class Property;
00033 //    class ExtrapolatedProperty;
00034     class PropertyEvent;
00035 
00036 /** @class PropertyListener propertylistener.hh vos/metaobjects/property/propertylistener.hh
00037   * @ingroup libmetaobject_property
00038  *  PropertyListener objects will be notified of all changes to Properties
00039  *  to which they are listening. Create a subclass and override
00040  *  notifyPropertyChanged, create an instance, and then use
00041  *  Property::addPropertyListener to register your listener with that
00042  *  property.
00043  */
00044 class VOS_API PropertyListener {
00045 public:
00046     virtual ~PropertyListener() { }
00047 
00048     /** Notification interface that a property has been changed.
00049      * Override this method in your subclass.
00050      *  @param event Event describing what happened. Note the 'const' declaration.
00051      */
00052     virtual void notifyPropertyChange(const PropertyEvent& event) = 0;
00053 };
00054 
00055 
00056 /** @class PropertyEvent propertylistener.hh vos/metaobjects/property/propertylistener.hh
00057   * @ingroup libmetaobject_property
00058  *  PropertyEvents are passed to PropertyListener objectss to notify them of
00059  *  changes to the properties they are listening to.
00060  */
00061     class VOS_API PropertyEvent : public VUtil::RefCounted
00062 {
00063 public:
00064     typedef enum {PropertyWrite, PropertyReplace, PropertyRead} EventType;
00065 
00066 private:
00067     EventType event;
00068     std::string oldvalue;
00069     std::string newvalue;
00070     std::string olddatatype;
00071     std::string newdatatype;
00072     Vobject* initiator;
00073     Property* property;
00074     int offset;
00075     int length;
00076     bool isACcheck;
00077 
00078 public:
00079     PropertyEvent(EventType et, Vobject* init, Property& property,
00080                   const std::string& value,
00081                   const std::string& datatype,
00082                   bool isACcheck);
00083     PropertyEvent(EventType et, Vobject* init, Property& property,
00084                   const std::string& value,
00085                   const std::string& datatype,
00086                   const std::string& oldvalue,
00087                   const std::string& olddatatype,
00088                   bool isACcheck);
00089     PropertyEvent(EventType et, Vobject* init, Property& property,
00090                   int offset,
00091                   int length,
00092                   const std::string& value,
00093                   const std::string& datatype,
00094                   bool isACcheck);
00095     PropertyEvent(EventType et, Vobject* init, Property& property,
00096                   int offset,
00097                   int length,
00098                   const std::string& value,
00099                   const std::string& datatype,
00100                   const std::string& oldvalue,
00101                   const std::string& olddatatype,
00102                   bool isACcheck);
00103 
00104     virtual ~PropertyEvent();
00105 
00106     /** @return What type of event this is. */
00107     EventType getEvent() const { return event; }
00108 
00109     /** @return The object which requested or initiated this event.
00110         Always valid.  For access control checks, this is the object
00111         which is requesting access.
00112      */
00113     VUtil::vRef<Vobject> getInitiator() const { return VUtil::vRef<Vobject>(initiator, true); }
00114 
00115     /** @return The property which is being accessed or changed.
00116         Always valid.  For access control checks, this is the object
00117         which is requesting access.
00118      */
00119     VUtil::vRef<Property> getProperty() const;
00120 
00121     /** @return The beginning of the changed section.
00122         @note If you want to know what exactly section of the property
00123         changed on a write event, use
00124         @code
00125 string section = e.getNewValue().substr(e.getOffset(), e.getLength());
00126         @endcode
00127     */
00128     unsigned int getOffset() const { return offset; }
00129 
00130     /** @return The size of the changed section. */
00131     unsigned int getLength() const { return length; }
00132 
00133     /** @return The previous value of this property, prior to a
00134         proposed change (for access control checks) or prior to the
00135         most recent change (for notify events). */
00136     const std::string& getOldValue() const { return oldvalue; }
00137 
00138     /** @return For an access control check, this is the proposed new
00139         value (it has not been commited yet).  For a notify event, the
00140         new value for this property.  */
00141     const std::string& getNewValue() const { return newvalue; }
00142 
00143     /** @return For an access control check, this is the old property
00144         value (the change has not been commited yet).  For a notify
00145         event it is the current value of the property.  */
00146     const std::string& getValue() const
00147         { return (isACcheck ? oldvalue : newvalue); }
00148 
00149     /** Only to be used for access control checks.  This allows you to
00150         modify the proposed new value for this property.
00151         @param v the new value to use */
00152     void setNewValue(const std::string& v) { newvalue = v; }
00153 
00154     /** @return The previous datatype of this property, prior to a
00155         proposed change (for access control checks) or prior to the
00156         most recent change (for notify events). */
00157     const std::string& getOldDataType() const { return olddatatype; }
00158 
00159     /** @return For an access control check, this is the proposed new
00160         datatype (it has not been commited yet).  For a notify event, the
00161         new datatype for this property.  */
00162     const std::string& getNewDataType() const { return newdatatype; }
00163 
00164     /** @return For an access control check, this is the old property
00165         datatype (the change has not been commited yet).  For a notify
00166         event it is the current datatype of the property.  */
00167     const std::string& getDataType() const
00168         { return (isACcheck ? olddatatype : newdatatype); }
00169 
00170     /** Only to be used for access control checks.  This allows you to
00171         modify the proposed new datatype for this property.
00172         @param dt the new datatype to use */
00173     void setNewDataType(const std::string& dt) { newdatatype = dt; }
00174 
00175     void deliverTo(PropertyListener* pl) { pl->notifyPropertyChange(*this); };
00176 };
00177 
00178 #if 0
00179 /* @class ExtrapolatedPropertyEvent propertylistener.hh vos/metaobjects/property/propertylistener.hh
00180  *  @ingroup libmetaobject_property
00181  *  Listener for ExtrapolatedProperty changes.
00182  */
00183 class VOS_API ExtrapolatedPropertyListener {
00184 public:
00185     virtual void notifyBaseChange(ExtrapolatedProperty& ep,
00186                                       const std::vector<double>& position,
00187                                       const std::vector<double>& velocity,
00188                                       const std::vector<double>& acceleration,
00189                                       double t) = 0;
00190     virtual void notifyPositionChange(ExtrapolatedProperty& ep,
00191                                       const std::vector<double>& position,
00192                                       const std::vector<double>& velocity,
00193                                       const std::vector<double>& acceleration,
00194                                       double t) = 0;
00195 };
00196 #endif
00197 
00198 
00199 /** @class DoNothingPropertyListener propertylistener.hh vos/metaobjects/property/propertylistener.hh
00200  *  @ingroup libmetaobject_property
00201  *  This is a PropertyListener that does nothing; however, it can be used
00202  *  to keep a RemoteProperty up to date with changes in a LocalProperty,
00203  *  speeding up reads of the RemoteProperty.
00204  */
00205 class VOS_API DoNothingPropertyListener : public virtual PropertyListener
00206 //, public virtual ExtrapolatedPropertyListener
00207 {
00208 public:
00209     static DoNothingPropertyListener static_;
00210 
00211     virtual void notifyPropertyChange(const PropertyEvent& event) {};
00212 
00213 /*
00214     virtual void notifyPositionChange(ExtrapolatedProperty& , const std::vector<double>& , const std::vector<double>& ,
00215                                       const std::vector<double>& , double ) {};
00216     virtual void notifyBaseChange(ExtrapolatedProperty& , const std::vector<double>& , const std::vector<double>& ,
00217                                    const std::vector<double>& , double ) {};
00218 */
00219 };
00220 
00221 
00222     class VOS_API RemotePropertyListener :  public MetaObject, public PropertyListener
00223     {
00224     public:
00225         RemotePropertyListener(VobjectBase* superobject);
00226         virtual ~RemotePropertyListener() { }
00227 
00228         virtual void notifyPropertyChange(const PropertyEvent& event);
00229     };
00230 
00231 }
00232 
00233 #endif