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

/home/tetron/hack/vos/libs/vos/vos/property.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_HH_
00025 #define _PROPERTY_HH_
00026 
00027 #include <typeinfo>
00028 #include <set>
00029 #include <assert.h>
00030 
00031 #include <vos/vos/vos.hh>
00032 
00033 namespace VOS {
00034     class PropertyAccessControl;
00035     class PropertyListener;
00036     class PropertyEvent;
00037 
00038 /** @class Property property.hh vos/metaobjects/property/property.hh
00039  *  @ingroup libmetaobject_property
00040  *
00041  *  Property stores data of any type and size.
00042  */
00043 class VOS_API Property : public MetaObject
00044 {
00045 private:
00046     Message::Priority priority;
00047 
00048 protected:
00049     Property(VobjectBase* superobject);
00050 
00051 public:
00052 
00053     /** Get length (bytes) of decoded data. */
00054     virtual int getLength() = 0;
00055 
00056     /** Read decoded data into target, possibly performing decode if necesary
00057      * @see read(std::string, int, int)
00058      */
00059     virtual void read(std::string& target) = 0;
00060 
00061     /** Read a substring of decoded data into target, possibly performing decode if necesary
00062      * @param target    Place data in this string
00063      * @param start     Byte offset to start reading
00064      * @param length    Number of bytes to read. If this parameter is -1, read
00065      *                  until the end of the data.
00066      */
00067     virtual void read(std::string& target, int start, int length) = 0;
00068 
00069     /** Return decoded data, possibly performing decode if necesary
00070      * @see read(std::string, int int)
00071      */
00072     virtual std::string read() = 0;
00073 
00074     /** Return substring of decoded data, possibly performing decode if necesary
00075         @see read(std::string, int int)
00076      */
00077     virtual std::string read(int start, int length) = 0;
00078 
00079     /** Write newdata into property value, starting at byte position start */
00080     virtual void write(int start, const std::string& newdata) = 0;
00081 
00082     /** Completely change the value and type of data stored in this property.
00083         @param newdata New data.
00084         @param newtype The type of the new datatype. If no type identifier was supplied, the previous one is kept.
00085         NOTE: the initial datatype (set by the constructor) is "?" which is (or should be) an invalid value for
00086         any application.
00087     */
00088     virtual void replace(const std::string& newdata, const std::string& newtype = "?") = 0;
00089 
00090     /** Return type of decoded data (usually a MIME type) */
00091     virtual const std::string getDataType() = 0;
00092 
00093     /** Set/get this property, converting from/to non-string types.
00094         For information on formatted replace and read see snprintf and
00095         scanf in the standard C library.
00096     */
00097     //@{
00098     void replace(const char* format, size_t maxlen, ...);
00099     void replace(const char* format, const char* type, size_t maxlen, ...);
00100     void replace(const char* str);
00101     void read(const char* format, ...);
00102     void replace(bool b);
00103     void replace(int i);
00104     void replace(float x);
00105     void replace(float x, float y);
00106     void replace(float x, float y, float z);
00107     void replace(float x, float y, float z, float r);
00108     void replace(double x);
00109     void replace(double x, double y);
00110     void replace(double x, double y, double z);
00111     void replace(double x, double y, double z, double r);
00112     void read(int& i);
00113     void read(bool& i);
00114     void read(double& x);
00115     void read(double& x, double& y);
00116     void read(double& x, double& y, double& z);
00117     void read(double& x, double& y, double& z, double& r);
00118     void read(float& x);
00119     void read(float& x, float& y);
00120     void read(float& x, float& y, float& z);
00121     void read(float& x, float& y, float& z, float& r);
00122     void read(std::vector<float>& vec);
00123     void read(std::vector<int>& vec);
00124     void replace(std::vector<float>& vec);
00125     void replace(std::vector<int>& vec);
00126     //@}
00127 
00128     /** Set/Get a subproperty on an object, returning relation between object and property.
00129         For information on formatted setProperty and getProperty see snprintf and
00130         scanf in the standard C library.
00131      *  @param on   Object to set a subproperty on
00132      *  @param propname Name of the property to set
00133      *  @param type  Datatype of the new value.
00134      *  @param fmt Format string (printf()-style)
00135      *  @param maxlen Maximum buffer size for formatting property value and
00136      *  replacing (using snprintf()).
00137      *  @return object expressing relation between the object and the property
00138      */
00139     //@{
00140     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& on,
00141              const std::string& propname, const std::string& type,
00142             const char* fmt, size_t maxlen, ...);
00143     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& on,
00144              const std::string& propname, const std::string& type,
00145             const std::string& pac, const char* fmt, size_t maxlen, ...);
00146     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& on,
00147              const std::string& propname, const char* fmt, ...);
00148 
00149     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& on,
00150                                                  const std::string& propname,
00151                                                  const std::string& propval,
00152                                                  const std::string& valtype,
00153                                                  const std::string& pac = "",
00154                                                  bool letErrorThrough = false);
00155 
00156     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& base,
00157                                                         const std::string& propname,
00158                                                         bool b,
00159                                                         const std::string& pac = "");
00160 
00161     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& base,
00162                                                         const std::string& propname,
00163                                                         int i,
00164                                                         const std::string& pac = "");
00165 
00166     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& base,
00167                                                         const std::string& propname,
00168                                                         double x,
00169                                                         const std::string& pac = "");
00170 
00171     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& base,
00172                                                         const std::string& propname,
00173                                                         double x, double y,
00174                                                         const std::string& pac = "");
00175 
00176     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& base,
00177                                                         const std::string& name,
00178                                                         int x, int y,
00179                                                         const std::string& pac = "");
00180 
00181     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& base,
00182                                                         const std::string& propname,
00183                                                         double x, double y, double z,
00184                                                         const std::string& pac = "");
00185 
00186     static VUtil::vRef<ParentChildRelation> setProperty(Vobject& base,
00187                                                         const std::string& propname,
00188                                                         double x, double y, double z, double r,
00189                                                         const std::string& pac = "");
00190 
00191     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00192                                                         const std::string& propname,
00193                                                         std::string& data,
00194                                                         std::string& datatype);
00195 
00196     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00197                                                         const std::string& propname,
00198                                                         int& i);
00199 
00200     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00201                                                         const std::string& propname,
00202                                                         bool& i);
00203 
00204     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00205                                                         const std::string& propname,
00206                                                         double& x);
00207 
00208     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00209                                                         const std::string& propname,
00210                                                         double& x, double& y);
00211 
00212     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00213                                                         const std::string& propname,
00214                                                         double& x, double& y, double& z);
00215 
00216     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00217                                                         const std::string& propname,
00218                                                         double& x, double& y, double& z, double& r);
00219 
00220     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00221                                                         const std::string& propname,
00222                                                         float& x);
00223 
00224     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00225                                                         const std::string& propname,
00226                                                         float& x, float& y);
00227 
00228     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00229                                                         const std::string& name,
00230                                                         int& x, int& y);
00231 
00232     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00233                                                         const std::string& propname,
00234                                                         float& x, float& y, float& z);
00235 
00236     static VUtil::vRef<ParentChildRelation> getProperty(Vobject& base,
00237                                                         const std::string& propname,
00238                                                         float& x, float& y, float& z, float& r);
00239 
00240     //@}
00241 
00242     virtual void setPriority(Message::Priority p) { priority = p; };
00243     virtual Message::Priority getPriority() { return priority; }
00244 
00245     /** Add a new property listener to this property. This listener's methods
00246      * will be called when the property is modified.
00247      * @see PropertyListener
00248      * @param pl    The new property listener. If it is also a RefCounted
00249      * object, references will be acquired correctly.
00250      * @param refresh  ?? If supplied, determines whether listener is
00251      * immediately notified ??
00252      */
00253     virtual void addPropertyListener(PropertyListener* pl, bool refresh = true) = 0;
00254 
00255     /** Remove the property listener 'pl' from this property.
00256      * @param pl    Listener to remove. If it is also a RefCounted object,
00257      * references will be released correctly.
00258      */
00259     virtual void removePropertyListener(PropertyListener* pl) = 0;
00260 
00261     /** Write property value to a file.
00262      * If offset and length are supplied, write substring.
00263      * @param filename  Name of file to open and write to. Any previous contents
00264      *                  will be replaced.
00265      * @param offset    If supplied, beginning of substring to write from
00266      *                  property value.
00267      * @param length    If supplied, length of substring to write from property
00268      *                  value.
00269      * @throw runtime_error if there is an error opening or writing to the file.
00270      */
00271     void writeToFile(const std::string& filename, int offset = 0, int length = -1);
00272 
00273     /** Read data from file into this property.
00274      * @param filename  Name of file to read from.
00275      * @param type      New datatype for this property. If empty ("") or
00276      *                  omitted, then the existing datatype will be used.
00277      * @throw runtime_error On error opening or reading from file.
00278      */
00279     void readFromFile(const std::string& filename, const std::string& type = "");
00280 
00281     /** Return MetaObject type, "property" */
00282     virtual const std::string getVOSType() = 0;
00283 
00284     virtual void doSaveState(MessageBlock& output, std::set<std::string>& types, bool portable);
00285 
00286     /** Specify if this property should be cached locally.  Only
00287         meaningful for remote properties.  This means that we become a
00288         listener of the remote property and receive updates.  Calls to
00289         read() will be serviced using the cached values for the
00290         property and will not go out to the network.
00291         @param b If true, use cache.  If false, the property will not
00292         be cached (unless it is being listened to by another part of the program.)
00293      */
00294     virtual void cacheProperty(bool b);
00295 };
00296 
00297 /** @class PropertyAccessControl property.hh vos/metaobjects/property/property.hh
00298  *  Use subclasses of PropertyAccessControl to implement various access control
00299  *  policies on property reads and writes.
00300  *  @ingroup libmetaobject_property
00301  */
00302 class VOS_API PropertyAccessControl
00303 {
00304 public:
00305     virtual ~PropertyAccessControl() { }
00306 
00307     /** AccessControl callback to check if a given Vobject is
00308         permitted to read a section of this property.
00309         @param event the requested read event
00310         @param message Will be set to a message describing the reason for a failure.
00311     */
00312     virtual bool checkReadPermission(PropertyEvent& event, std::string& message) = 0;
00313 
00314     /** AccessControl callback to check if a given Vobject is
00315         permitted to change this property.
00316         @param event the requested change event
00317         @param message Will be set to contain a message describing reason for a failure.
00318     */
00319     virtual bool checkChangePermission(PropertyEvent& event, std::string& message) = 0;
00320 
00321     /** Get a short string describing this policy. */
00322     virtual const std::string getPolicyName() = 0;
00323 };
00324 
00325 extern VOS_API PolicyDomain<PropertyAccessControl> PropertyAccessControlPolicies;
00326 
00327 /** @class NoPropertyAccessControl property.hh vos/metaobjects/property/property.hh
00328  * @ingroup libmetaobject_property
00329  * Access control policy for properties that always says yes. */
00330 class VOS_API AcceptAllPropertyAccessControl : public PropertyAccessControl
00331 {
00332 public:
00333     static AcceptAllPropertyAccessControl static_;
00334 
00335     virtual ~AcceptAllPropertyAccessControl() { }
00336 
00337     virtual const std::string getPolicyName()
00338         { return "property:accept-all"; }
00339 
00340     virtual bool checkReadPermission(PropertyEvent& event, std::string& message)
00341         { return true; };
00342     virtual bool checkChangePermission(PropertyEvent& event, std::string& message)
00343         { return true; };
00344 };
00345 
00346 /** @class ReadOnlyPropertyAccessControl property.hh vos/metaobjects/property/property.hh
00347  * @ingroup libmetaobject_property
00348  * Access control policy for properties permits unlimited reads but denies
00349  * allwrites and replaces.
00350  * @ingroup libmetaobject_property
00351  */
00352 class VOS_API ReadOnlyPropertyAccessControl : public PropertyAccessControl
00353 {
00354 public:
00355     static ReadOnlyPropertyAccessControl static_;
00356 
00357     virtual ~ReadOnlyPropertyAccessControl() { }
00358 
00359     virtual const std::string getPolicyName()
00360         { return "property:read-only"; }
00361 
00362     virtual bool checkReadPermission(PropertyEvent& event, std::string& message)
00363         { return true; };
00364     virtual bool checkChangePermission(PropertyEvent& event, std::string& message)
00365         { message = "This property is read-only."; return false; };
00366 };
00367 
00368 /** @class DenyAllPropertyAccessControl property.hh vos/metaobjects/property/property.hh
00369  * Access control policy for properties permits unlimited reads but denies
00370  * allwrites and replaces.
00371  * @ingroup libmetaobject_property
00372  */
00373 class VOS_API DenyAllPropertyAccessControl : public PropertyAccessControl
00374 {
00375 public:
00376     static DenyAllPropertyAccessControl static_;
00377 
00378     virtual ~DenyAllPropertyAccessControl() { }
00379 
00380     virtual const std::string getPolicyName()
00381         { return "property:deny-all"; }
00382 
00383     virtual bool checkReadPermission(PropertyEvent& event, std::string& message)
00384         { return false; };
00385     virtual bool checkChangePermission(PropertyEvent& event, std::string& message)
00386         { return false; };
00387 };
00388 
00389 }
00390 
00391 #ifndef VOS_EXPORTS
00392 IMPORT_METAOBJECT_FACTORIES(DefaultPropertyACs)
00393 IMPORT_METAOBJECT_FACTORIES(BasicLocalProperty)
00394 IMPORT_METAOBJECT_FACTORIES(RemoteProperty)
00395 #endif
00396 
00397 
00398 #endif