Properties: const getters

This commit is contained in:
Maciej Suminski 2020-02-06 08:40:55 +01:00 committed by Jon Evans
parent 8fa513e903
commit d5677a6dfe
3 changed files with 24 additions and 25 deletions

View File

@ -42,8 +42,7 @@ public:
bool Set( PROPERTY_BASE* aProperty, wxAny& aValue ) bool Set( PROPERTY_BASE* aProperty, wxAny& aValue )
{ {
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance(); PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
TYPE_ID thisType = TYPE_HASH( *this ); void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
if( object ) if( object )
aProperty->setter( object, aValue ); aProperty->setter( object, aValue );
@ -55,8 +54,7 @@ public:
bool Set( PROPERTY_BASE* aProperty, T aValue ) bool Set( PROPERTY_BASE* aProperty, T aValue )
{ {
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance(); PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
TYPE_ID thisType = TYPE_HASH( *this ); void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
if( object ) if( object )
aProperty->set<T>( object, aValue ); aProperty->set<T>( object, aValue );
@ -83,16 +81,15 @@ public:
return object != nullptr; return object != nullptr;
} }
wxAny Get( PROPERTY_BASE* aProperty ) wxAny Get( PROPERTY_BASE* aProperty ) const
{ {
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance(); PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
TYPE_ID thisType = TYPE_HASH( *this ); const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
return object ? aProperty->getter( object ) : wxAny(); return object ? aProperty->getter( object ) : wxAny();
} }
template<typename T> template<typename T>
T Get( PROPERTY_BASE* aProperty ) T Get( PROPERTY_BASE* aProperty ) const
{ {
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance(); PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
TYPE_ID thisType = TYPE_HASH( *this ); TYPE_ID thisType = TYPE_HASH( *this );
@ -101,7 +98,7 @@ public:
} }
template<typename T> template<typename T>
boost::optional<T> Get( const wxString& aProperty ) boost::optional<T> Get( const wxString& aProperty ) const
{ {
PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance(); PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
TYPE_ID thisType = TYPE_HASH( *this ); TYPE_ID thisType = TYPE_HASH( *this );
@ -110,7 +107,7 @@ public:
if( prop ) if( prop )
{ {
void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() ); const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
if( object ) if( object )
ret = prop->get<T>( object ); ret = prop->get<T>( object );

View File

@ -67,7 +67,7 @@ class GETTER_BASE
public: public:
virtual ~GETTER_BASE() {} virtual ~GETTER_BASE() {}
virtual T operator()( Owner* aOwner ) const = 0; virtual T operator()( const Owner* aOwner ) const = 0;
}; };
template<typename Owner, typename T, typename FuncType> template<typename Owner, typename T, typename FuncType>
@ -77,9 +77,10 @@ public:
GETTER( FuncType aFunc ) GETTER( FuncType aFunc )
: m_func( aFunc ) : m_func( aFunc )
{ {
wxCHECK( m_func, /*void*/ );
} }
T operator()( Owner* aOwner ) const override T operator()( const Owner* aOwner ) const override
{ {
return ( aOwner->*m_func )(); return ( aOwner->*m_func )();
} }
@ -104,11 +105,11 @@ public:
SETTER( FuncType aFunc ) SETTER( FuncType aFunc )
: m_func( aFunc ) : m_func( aFunc )
{ {
wxCHECK( m_func, /*void*/ );
} }
void operator()( Owner* aOwner, T aValue ) override void operator()( Owner* aOwner, T aValue ) override
{ {
wxCHECK( m_func, /*void*/ );
( aOwner->*m_func )( aValue ); ( aOwner->*m_func )( aValue );
} }
@ -121,7 +122,7 @@ template<typename Owner, typename T, typename Base = Owner>
class METHOD class METHOD
{ {
public: public:
constexpr static GETTER_BASE<Owner, T>* Wrap( T (Base::*aFunc)() ) static GETTER_BASE<Owner, T>* Wrap( T (Base::*aFunc)() )
{ {
return new GETTER<Owner, T, T (Base::*)()>( aFunc ); return new GETTER<Owner, T, T (Base::*)()>( aFunc );
} }
@ -143,7 +144,7 @@ public:
constexpr static GETTER_BASE<Owner, T>* Wrap( const T (Base::*aFunc)() const ) constexpr static GETTER_BASE<Owner, T>* Wrap( const T (Base::*aFunc)() const )
{ {
return new GETTER<Owner, T, const T (Base::*)() const>( aFunc ); return new GETTER<Owner, T, T (Base::*)() const>( aFunc );
} }
constexpr static GETTER_BASE<Owner, T>* Wrap( const T& (Base::*aFunc)() const ) constexpr static GETTER_BASE<Owner, T>* Wrap( const T& (Base::*aFunc)() const )
@ -266,7 +267,7 @@ protected:
} }
template<typename T> template<typename T>
T get( void* aObject ) T get( const void* aObject ) const
{ {
wxAny a = getter( aObject ); wxAny a = getter( aObject );
@ -276,8 +277,9 @@ protected:
return wxANY_AS( a, T ); return wxANY_AS( a, T );
} }
private:
virtual void setter( void* aObject, wxAny& aValue ) = 0; virtual void setter( void* aObject, wxAny& aValue ) = 0;
virtual wxAny getter( void* aObject ) const = 0; virtual wxAny getter( const void* aObject ) const = 0;
private: private:
const wxString m_name; const wxString m_name;
@ -293,8 +295,7 @@ template<typename Owner, typename T, typename Base = Owner>
class PROPERTY : public PROPERTY_BASE class PROPERTY : public PROPERTY_BASE
{ {
public: public:
typedef typename std::decay<T>::type BASE_TYPE; using BASE_TYPE = typename std::decay<T>::type;
typedef void (Base::*SETTER)( T );
template<typename SetType, typename GetType> template<typename SetType, typename GetType>
PROPERTY( const wxString& aName, PROPERTY( const wxString& aName,
@ -357,9 +358,9 @@ protected:
(*m_setter)( o, value ); (*m_setter)( o, value );
} }
virtual wxAny getter( void* obj ) const override virtual wxAny getter( const void* obj ) const override
{ {
Owner* o = reinterpret_cast<Owner*>( obj ); const Owner* o = reinterpret_cast<const Owner*>( obj );
wxAny res = (*m_getter)( o ); wxAny res = (*m_getter)( o );
return res; return res;
} }
@ -413,7 +414,7 @@ public:
} }
} }
virtual void setter( void* obj, wxAny& v ) override void setter( void* obj, wxAny& v ) override
{ {
wxCHECK( !( PROPERTY<Owner, T, Base>::IsReadOnly() ), /*void*/ ); wxCHECK( !( PROPERTY<Owner, T, Base>::IsReadOnly() ), /*void*/ );
Owner* o = reinterpret_cast<Owner*>( obj ); Owner* o = reinterpret_cast<Owner*>( obj );
@ -434,9 +435,9 @@ public:
} }
} }
virtual wxAny getter( void* obj ) const override wxAny getter( const void* obj ) const override
{ {
Owner* o = reinterpret_cast<Owner*>( obj ); const Owner* o = reinterpret_cast<const Owner*>( obj );
wxAny res = static_cast<T>( (*PROPERTY<Owner, T, Base>::m_getter)( o ) ); wxAny res = static_cast<T>( (*PROPERTY<Owner, T, Base>::m_getter)( o ) );
return res; return res;
} }

View File

@ -127,7 +127,8 @@ static struct CLASS_A_DESC
propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point", &A::setPoint, &A::getPoint ) ); propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point", &A::setPoint, &A::getPoint ) );
propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point2", &A::setPoint, &A::getPoint2 ) ); propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point2", &A::setPoint, &A::getPoint2 ) );
propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point3", &A::setPoint3, &A::getPoint3 ) ); // TODO non-const getters are not supported
//propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point3", &A::setPoint3, &A::getPoint3 ) );
propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point4", &A::setPoint4, &A::getPoint4 ) ); propMgr.AddProperty( new PROPERTY<A, wxPoint>( "point4", &A::setPoint4, &A::getPoint4 ) );
} }
} _CLASS_A_DESC; } _CLASS_A_DESC;