ValueObject.h

Go to the documentation of this file.
00001 // @(#)root/reflex:$Id: ValueObject.h 29288 2009-07-01 13:03:35Z axel $
00002 // Author: Pere Mato 2006
00003 
00004 // Copyright CERN, CH-1211 Geneva 23, 2004-2005, 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_ValueObject
00013 #define Reflex_ValueObject
00014 
00015 // Include files
00016 #include "Reflex/Any.h"
00017 #include "Reflex/Object.h"
00018 #include "Reflex/Builder/TypeBuilder.h"
00019 
00020 
00021 namespace Reflex {
00022 /**
00023  * @class ValueObject ValueObject.h Reflex/ValueObject.h
00024  * @author Pere Mato
00025  * @date 01/09/2006
00026  * @ingroup Ref
00027  */
00028 class RFLX_API ValueObject: public Object {
00029 public:
00030    /** constructor */
00031    ValueObject();
00032 
00033    /** constructor */
00034    template <typename T>
00035    static ValueObject Create(const T& v);
00036 
00037    /** constructor */
00038    ValueObject(const ValueObject &o);
00039 
00040    /** destructor */
00041    ~ValueObject();
00042 
00043    /** get the actual value */
00044    template <typename T> const T& Value();
00045 
00046    template <typename T> ValueObject& Assign(const T&);
00047 
00048 private:
00049    /** the value of the generic object by value */
00050    Any fValue;
00051 
00052 };    // class ValueObject
00053 } // namespace Reflex
00054 
00055 
00056 //-------------------------------------------------------------------------------
00057 inline Reflex::ValueObject::ValueObject() {
00058 //-------------------------------------------------------------------------------
00059 }
00060 
00061 
00062 //-------------------------------------------------------------------------------
00063 template <typename T>
00064 inline Reflex::ValueObject
00065 Reflex::ValueObject::Create(const T& v) {
00066 //-------------------------------------------------------------------------------
00067    ValueObject ret;
00068    ret.Assign(v);
00069    return ret;
00070 }
00071 
00072 
00073 //-------------------------------------------------------------------------------
00074 inline Reflex::ValueObject::ValueObject(const ValueObject& o):
00075    Object(o.TypeOf(), 0),
00076    fValue(o.fValue) {
00077 //-------------------------------------------------------------------------------
00078    if (TypeOf().IsPointer()) {
00079       fAddress = *(void**) fValue.Address();
00080    } else { fAddress = fValue.Address(); }
00081 }
00082 
00083 
00084 //-------------------------------------------------------------------------------
00085 template <typename T>
00086 inline Reflex::ValueObject&
00087 Reflex::ValueObject::Assign(const T& v) {
00088 //-------------------------------------------------------------------------------
00089    fValue = Any(v);
00090    fType = GetType<T>();
00091 
00092    if (TypeOf().IsPointer()) {
00093       fAddress = *(void**) fValue.Address();
00094    } else { fAddress = fValue.Address(); }
00095    return *this;
00096 }
00097 
00098 
00099 //-------------------------------------------------------------------------------
00100 inline Reflex::ValueObject::~ValueObject() {
00101 //-------------------------------------------------------------------------------
00102 }
00103 
00104 
00105 //-------------------------------------------------------------------------------
00106 template <typename T>
00107 inline const T&
00108 Reflex::ValueObject::Value() {
00109 //-------------------------------------------------------------------------------
00110    return *(T*) fAddress;
00111 }
00112 
00113 
00114 #endif // Reflex_ValueObject

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