00001 #ifndef ABSTRACTPROPERTY_H
00002 #define ABSTRACTPROPERTY_H
00003
00004 #include "Types.h"
00005 #include "Error.h"
00006 #include "PropertyEvent.h"
00007 #include "InitializeInterface.h"
00008
00009 #include <typeinfo>
00010
00015 namespace RenderTools {
00016
00017 class AbstractProperty : public InitializeInterface, public enable_shared_from_this< AbstractProperty > {
00018 public:
00019
00020 typedef unsigned int SemanticMask;
00021 enum Semantic {
00022 NONE = 0x0000,
00023 MASK = 0x0001,
00024 COLOR = 0x0002,
00025 SAMPLER = 0x0004,
00026 MODEL = 0x0008,
00027 FILENAME = 0x0010,
00028 SHADER = 0x0020,
00029 CONTAINER = 0x0040,
00030 };
00031
00032 struct NullDeleter {
00033 void operator()( void const * ) const {}
00034 };
00035
00036 AbstractProperty( void );
00037 virtual ~AbstractProperty( void );
00038
00039 virtual void onPropertyEvent( const PropertyEvent & p );
00040 virtual void createProperties( void );
00041 virtual const string getTypeName( bool ofComponent = false ) const;
00042
00043 virtual void onInitialize( void );
00044 virtual const string getValue( int vectorElement = -1 ) const;
00045 virtual bool setValue( const string value, int vectorElement = -1 );
00046 virtual const void * getPointer( unsigned int offset ) const;
00047 virtual unsigned int getNumComponents( void ) const;
00048 virtual unsigned int getNumVectorElements( void ) const;
00049 virtual void setOwner( const PropertyContainerPtr & owner );
00050 virtual bool isContainer( void ) const;
00051 virtual bool isString( void ) const;
00052 virtual bool isInteger( void ) const;
00053 virtual bool isReal( void ) const;
00054 virtual bool isBoolean( void ) const;
00055 virtual bool isEnum( void ) const;
00056 virtual bool isMatrix( void ) const;
00057 virtual bool isMultiComponent( void ) const;
00058 virtual bool isVector( void ) const;
00059 virtual const XMLNodePtr toXML( const XMLNodePtr & parent = XMLNodePtr() ) const;
00060 virtual void setName( const XMLNodePtr & xml = XMLNodePtr() );
00061 virtual void setName( const string name );
00062 virtual void recalculatePath( void );
00063 void setSemantic( SemanticMask semantic );
00064 void storePath( void );
00065 void removePath( void );
00066 void connect( const PropertyPtr & p, bool send = false );
00067 void disconnect( const PropertyPtr & p, bool send = false );
00068 void addEventListener( const PropertyPtr & p, bool send = false );
00069 void removeEventListener( const PropertyPtr & p, bool send = false );
00070 void sendPropertyEvent( const PropertyEvent & p );
00071 const PropertyContainerPtr getOwner( void ) const;
00072 const string & getName( void ) const;
00073 const string & getPath( void ) const;
00074 const string tracePath( void ) const;
00075 const string getDecoratedName( bool includeType = true, bool asVector = true, int vectorElement = -1, int component = -1 ) const;
00076 SemanticMask getSemantic( void ) const;
00077 static void clear( void );
00078 static const string find( const PropertyPtr & );
00079 static const PropertyPtr find( const string path );
00080 static const string createName( const string prefix = "p" );
00081
00082 template< class T >
00084 weak_ptr< T > getWeakPtr( void ){
00085 return( dynamic_pointer_cast< T, AbstractProperty >( shared_from_this() ) );
00086 }
00087
00088 template< class T >
00090 const weak_ptr< T > getWeakPtr( void ) const {
00091 return( const_cast< AbstractProperty * >( this )->getWeakPtr< T >() );
00092 }
00093
00094 template< class T >
00096 shared_ptr< T > getSharedPtr( void ){
00097 return( dynamic_pointer_cast< T, AbstractProperty >( shared_from_this() ) );
00098 }
00099
00100 template< class T >
00102 const shared_ptr< T > getSharedPtr( void ) const {
00103 return( const_cast< AbstractProperty * >( this )->getSharedPtr< T >() );
00104 }
00105
00106 template< class T >
00108 shared_ptr< T > getNullDeletingSharedPtr( void ){
00109 return( shared_ptr< T >( static_cast< T * >( this ), NullDeleter() ) );
00110 }
00111
00112 template< class T >
00114 const shared_ptr< T > getNullDeletingSharedPtr( void ) const {
00115 return( const_cast< AbstractProperty * >( this )->getNullDeletingSharedPtr< T >() );
00116 }
00117
00118 template< class T >
00120 weak_ptr< T > getNullDeletingWeakPtr( void ){
00121 return( weak_ptr< T >( static_cast< T * >( this ), NullDeleter() ) );
00122 }
00123
00124 template< class T >
00126 const shared_ptr< T > getNullDeletingWeakPtr( void ) const {
00127 return( const_cast< AbstractProperty * >( this )->getNullDeletingWeakPtr< T >() );
00128 }
00129
00130 friend ostream & operator << ( ostream & os, const AbstractProperty & rhs );
00131 friend const istream & operator >> ( const istream & is, AbstractProperty & rhs );
00132
00133 protected:
00134
00135 PropertyContainerWeakPtr m_owner;
00136 string m_path;
00137 string m_name;
00138 SemanticMask m_semantic;
00139 PropertyWeakList m_eventListeners;
00140
00141 static map< AbstractProperty *, string > s_pointers;
00142 static map< string, PropertyWeakPtr > s_paths;
00143 static unsigned long s_counter;
00144 };
00145
00146 };
00147
00148 #endif