Object.h

Go to the documentation of this file.
00001 // @(#)root/reflex:$Id: Object.h 36314 2010-10-12 12:40:57Z axel $
00002 // Author: Stefan Roiser 2004
00003 
00004 // Copyright CERN, CH-1211 Geneva 23, 2004-2006, All rights reserved.
00005 //
00006 // Permission to use, copy, modify, and distribute this software for any
00007 // purpose is hereby granted without fee, provided that this copyright and
00008 // permissions notice appear in all copies and derivatives.
00009 //
00010 // This software is provided "as is" without express or implied warranty.
00011 
00012 #ifndef Reflex_Object
00013 #define Reflex_Object
00014 
00015 // Include files
00016 #include "Reflex/Type.h"
00017 #include <string>
00018 #include <vector>
00019 
00020 namespace Reflex {
00021 // forward declarations
00022 
00023 /**
00024  * @class Object Object.h Reflex/Object.h
00025  * @author Stefan Roiser
00026  * @date 24/06/2004
00027  * @ingroup Ref
00028  */
00029 class RFLX_API Object {
00030 public:
00031    /** constructor */
00032    Object(const Type& type = Type(),
00033           void* mem = 0);
00034 
00035 
00036    /** constructor */
00037    Object(const Object &);
00038 
00039 
00040    /** destructor */
00041    ~Object() {}
00042 
00043 
00044    template <typename T>
00045    static Object
00046    Create(T& v) {
00047       return Object(Type::ByTypeInfo(typeid(T)), &v);
00048    }
00049 
00050 
00051    /**
00052     * operator assigment
00053     */
00054    Object operator =(const Object& obj);
00055 
00056 
00057    /**
00058     * operator ==
00059     */
00060    bool operator ==(const Object& obj);
00061 
00062 
00063    /**
00064     * inequal operator
00065     */
00066    bool operator !=(const Object& obj);
00067 
00068 
00069    /**
00070     * operator bool
00071     */
00072    operator bool() const;
00073 
00074 
00075    /**
00076     * Address will return the memory address of the object
00077     * @return memory address of object
00078     */
00079    void* Address() const;
00080 
00081 
00082    /**
00083     * CastObject an object from this class type to another one
00084     * @param  to is the class type to cast into
00085     * @param  obj the memory address of the object to be casted
00086     */
00087    Object CastObject(const Type& to) const;
00088 
00089 
00090    /**
00091     * Destruct will call the destructor of a type and remove its memory
00092     * allocation if desired
00093     */
00094    void Destruct() const;
00095 
00096 
00097    /**
00098     * DynamicType is used to discover the dynamic type (useful in
00099     * case of polymorphism)
00100     * @return the actual class of the object
00101     */
00102    Type DynamicType() const;
00103 
00104 
00105    /**
00106     * Get the data member value
00107     * @param dm name of the data member to get
00108     * @return member value as object
00109     */
00110    Object Get(const std::string& dm) const;
00111 
00112 
00113    /**
00114     * Invoke a member function of the object
00115     * @param fm name of the member function
00116     * @param ret Object to put the return value into (can be 0 for function returning void)
00117     * @param args a vector of memory addresses to parameter values
00118     * @return the return value of the function as object
00119     */
00120    void Invoke(const std::string& fm,
00121                Object* ret = 0,
00122                const std::vector<void*>& args = std::vector<void*>()) const;
00123 
00124 
00125    /**
00126     * Invoke a member function of the object
00127     * @param fm name of the member function
00128     * @param ret Object to put the return value into (can be 0 for function returning void)
00129     * @param args a vector of memory addresses to parameter values
00130     * @return the return value of the function as object
00131     */
00132    template <typename T>
00133    void
00134    Invoke(const std::string& fm,
00135           T& ret,
00136           const std::vector<void*>& args = std::vector<void*>()) const {
00137       Object retO(Type::ByTypeInfo(typeid(T)), &ret);
00138       Invoke(fm, &retO, args);
00139    }
00140 
00141 
00142    /**
00143     * Invoke a member function of the object
00144     * @param fm name of the member function
00145     * @param sign the signature of the member function (for overloads)
00146     * @param ret Object to put the return value into (can be 0 for function returning void)
00147     * @param args a vector of memory addresses to parameter values
00148     * @return the return value of the function as object
00149     */
00150    void Invoke(const std::string& fm,
00151                const Type& sign,
00152                Object* ret = 0,
00153                const std::vector<void*>& args = std::vector<void*>()) const;
00154 
00155 
00156    /**
00157     * Invoke a member function of the object
00158     * @param fm name of the member function
00159     * @param sign the signature of the member function (for overloads)
00160     * @param ret Object to put the return value into (can be 0 for function returning void)
00161     * @param args a vector of memory addresses to parameter values
00162     * @return the return value of the function as object
00163     */
00164    template <typename T>
00165    void
00166    Invoke(const std::string& fm,
00167           const Type& sign,
00168           T& ret,
00169           const std::vector<void*>& args = std::vector<void*>()) const {
00170       Object retO(Type::ByTypeInfo(typeid(T)), &ret);
00171       Invoke(fm, sign, &retO, args);
00172    }
00173 
00174 
00175    /**
00176     * Set will set a data member value of this object
00177     * @param dm the name of the data member
00178     * @param value the memory address of the value to set
00179     */
00180    void Set(const std::string& dm,
00181             const void* value) const;
00182 
00183 
00184    /**
00185     * Set will set a data member value of this object
00186     * @param dm the name of the data member
00187     * @param value the memory address of the value to set
00188     */
00189    template <class T>
00190    void Set(const std::string& dm,
00191             const T& value) const;
00192 
00193 
00194    /**
00195     * TypeOf will return the type of the object
00196     * @return type of the object
00197     */
00198    Type TypeOf() const;
00199 
00200 private:
00201    friend class ValueObject;
00202 
00203    /** */
00204    void Set2(const std::string& dm,
00205              const void* value) const;
00206 
00207    /**
00208     * the type of the object
00209     * @link aggregation
00210     * @clientCardinality 1
00211     * @supplierCardinality 1
00212     * @label object type
00213     **/
00214    Type fType;
00215 
00216 
00217    /**
00218     * the address of the object
00219     */
00220    mutable
00221    void* fAddress;
00222 
00223 };    // class Object
00224 
00225 
00226 /**
00227  * Object_Cast can be used to cast an object into a given type
00228  * (no additional checks are performed for the time being)
00229  * @param o the object to be casted
00230  * @return the address of the object casted into type T
00231  */
00232 template <class T> T Object_Cast(const Object& o);
00233 
00234 
00235 } // namespace Reflex
00236 
00237 #include "Reflex/Tools.h"
00238 
00239 //-------------------------------------------------------------------------------
00240 template <class T>
00241 inline T
00242 Reflex::Object_Cast(const Object& o) {
00243 //-------------------------------------------------------------------------------
00244    return *(T*) o.Address();
00245 }
00246 
00247 
00248 //-------------------------------------------------------------------------------
00249 inline Reflex::Object::Object(const Type& type,
00250                               void* mem)
00251 //-------------------------------------------------------------------------------
00252    : fType(type),
00253    fAddress(mem) {
00254 }
00255 
00256 
00257 //-------------------------------------------------------------------------------
00258 inline Reflex::Object::Object(const Object& obj)
00259 //-------------------------------------------------------------------------------
00260    : fType(obj.fType),
00261    fAddress(obj.fAddress) {
00262 }
00263 
00264 
00265 //-------------------------------------------------------------------------------
00266 inline Reflex::Object
00267 Reflex::Object::operator =(const Object& obj) {
00268 //-------------------------------------------------------------------------------
00269    fType = obj.fType;
00270    fAddress = obj.fAddress;
00271    return *this;
00272 }
00273 
00274 
00275 //-------------------------------------------------------------------------------
00276 inline bool
00277 Reflex::Object::operator ==(const Object& obj) {
00278 //-------------------------------------------------------------------------------
00279    return fType == obj.fType && fAddress == obj.fAddress;
00280 }
00281 
00282 
00283 //-------------------------------------------------------------------------------
00284 inline bool
00285 Reflex::Object::operator !=(const Object& obj) {
00286 //-------------------------------------------------------------------------------
00287    return fType != obj.fType || fAddress != obj.fAddress;
00288 }
00289 
00290 
00291 //-------------------------------------------------------------------------------
00292 inline
00293 Reflex::Object::operator bool() const {
00294 //-------------------------------------------------------------------------------
00295    if (fType && fAddress) {
00296       return true;
00297    }
00298    return false;
00299 }
00300 
00301 
00302 //-------------------------------------------------------------------------------
00303 inline void*
00304 Reflex::Object::Address() const {
00305 //-------------------------------------------------------------------------------
00306    return fAddress;
00307 }
00308 
00309 
00310 //-------------------------------------------------------------------------------
00311 inline Reflex::Object
00312 Reflex::Object::CastObject(const Type& to) const {
00313 //-------------------------------------------------------------------------------
00314    if (*this) {
00315       return fType.CastObject(to, *this);
00316    }
00317    return Object();
00318 }
00319 
00320 
00321 //-------------------------------------------------------------------------------
00322 inline void
00323 Reflex::Object::Destruct() const {
00324 //-------------------------------------------------------------------------------
00325    if (*this) {
00326       fType.Destruct(fAddress);
00327       fAddress = 0;
00328    }
00329 }
00330 
00331 
00332 //-------------------------------------------------------------------------------
00333 inline Reflex::Type
00334 Reflex::Object::DynamicType() const {
00335 //-------------------------------------------------------------------------------
00336    return fType.DynamicType(*this);
00337 }
00338 
00339 
00340 //-------------------------------------------------------------------------------
00341 inline void
00342 Reflex::Object::Set(const std::string& dm,
00343                     const void* value) const {
00344 //-------------------------------------------------------------------------------
00345    Set2(dm, value);
00346 }
00347 
00348 
00349 //-------------------------------------------------------------------------------
00350 template <class T>
00351 inline void
00352 Reflex::Object::Set(const std::string& dm,
00353                     const T& value) const {
00354 //-------------------------------------------------------------------------------
00355    Set2(dm, &value);
00356 }
00357 
00358 
00359 //-------------------------------------------------------------------------------
00360 inline Reflex::Type
00361 Reflex::Object::TypeOf() const {
00362 //-------------------------------------------------------------------------------
00363    return fType;
00364 }
00365 
00366 
00367 #endif // Reflex_Object

Generated on Tue Jul 5 14:25:54 2011 for ROOT_528-00b_version by  doxygen 1.5.1