2020-02-02 18:29:33 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 CERN
|
2021-01-25 12:42:36 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2020-02-02 18:29:33 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef PROPERTY_H
|
|
|
|
#define PROPERTY_H
|
|
|
|
|
2021-03-20 15:35:37 +00:00
|
|
|
#include <core/wx_stl_compat.h>
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
#include <wx/any.h>
|
|
|
|
#include <wx/string.h>
|
2020-10-01 15:15:15 +00:00
|
|
|
#include <wx/bitmap.h>
|
2020-10-25 18:31:55 +00:00
|
|
|
#include <wx/font.h> // required for propgrid
|
|
|
|
#include <wx/validate.h> // required for propgrid
|
2020-02-02 18:29:33 +00:00
|
|
|
#include <wx/propgrid/property.h>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <typeindex>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
class wxPGProperty;
|
|
|
|
class INSPECTABLE;
|
|
|
|
class PROPERTY_BASE;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class ENUM_MAP;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Common property types
|
2020-02-02 18:29:33 +00:00
|
|
|
enum PROPERTY_DISPLAY
|
|
|
|
{
|
2020-04-06 16:23:21 +00:00
|
|
|
PT_DEFAULT, ///< Default property for a given type
|
|
|
|
PT_SIZE, ///< Size expressed in distance units (mm/inch)
|
|
|
|
PT_COORD, ///< Coordinate expressed in distance units (mm/inch)
|
|
|
|
PT_DEGREE, ///< Angle expressed in degrees
|
|
|
|
PT_DECIDEGREE ///< Angle expressed in decidegrees
|
2020-02-02 18:29:33 +00:00
|
|
|
};
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Macro to generate unique identifier for a type
|
2020-02-02 18:29:33 +00:00
|
|
|
#define TYPE_HASH( x ) typeid( x ).hash_code()
|
|
|
|
#define TYPE_NAME( x ) typeid( x ).name()
|
|
|
|
//#define TYPE_HASH( x ) typeid( std::decay<x>::type ).hash_code()
|
|
|
|
|
|
|
|
template<typename Owner, typename T>
|
|
|
|
class GETTER_BASE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~GETTER_BASE() {}
|
|
|
|
|
2020-02-06 07:40:55 +00:00
|
|
|
virtual T operator()( const Owner* aOwner ) const = 0;
|
2020-02-02 18:29:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Owner, typename T, typename FuncType>
|
|
|
|
class GETTER : public GETTER_BASE<Owner, T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GETTER( FuncType aFunc )
|
|
|
|
: m_func( aFunc )
|
|
|
|
{
|
2020-02-06 07:40:55 +00:00
|
|
|
wxCHECK( m_func, /*void*/ );
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 07:40:55 +00:00
|
|
|
T operator()( const Owner* aOwner ) const override
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
return ( aOwner->*m_func )();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FuncType m_func;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Owner, typename T>
|
|
|
|
class SETTER_BASE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~SETTER_BASE() {}
|
|
|
|
|
|
|
|
virtual void operator()( Owner* aOwner, T aValue ) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Owner, typename T, typename FuncType>
|
|
|
|
class SETTER : public SETTER_BASE<Owner, T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SETTER( FuncType aFunc )
|
|
|
|
: m_func( aFunc )
|
|
|
|
{
|
2020-02-06 07:40:55 +00:00
|
|
|
wxCHECK( m_func, /*void*/ );
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()( Owner* aOwner, T aValue ) override
|
|
|
|
{
|
|
|
|
( aOwner->*m_func )( aValue );
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FuncType m_func;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Owner, typename T, typename Base = Owner>
|
|
|
|
class METHOD
|
|
|
|
{
|
|
|
|
public:
|
2020-02-06 07:40:55 +00:00
|
|
|
static GETTER_BASE<Owner, T>* Wrap( T (Base::*aFunc)() )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
return new GETTER<Owner, T, T (Base::*)()>( aFunc );
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static GETTER_BASE<Owner, T>* Wrap( const T (Base::*aFunc)() )
|
|
|
|
{
|
|
|
|
return new GETTER<Owner, T, T (Base::*)()>( aFunc );
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static GETTER_BASE<Owner, T>* Wrap( const T& (Base::*aFunc)() )
|
|
|
|
{
|
|
|
|
return new GETTER<Owner, T, const T& (Base::*)()>( aFunc );
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static GETTER_BASE<Owner, T>* Wrap( T (Base::*aFunc)() const )
|
|
|
|
{
|
|
|
|
return new GETTER<Owner, T, T (Base::*)() const>( aFunc );
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static GETTER_BASE<Owner, T>* Wrap( const T (Base::*aFunc)() const )
|
|
|
|
{
|
2020-02-06 07:40:55 +00:00
|
|
|
return new GETTER<Owner, T, T (Base::*)() const>( aFunc );
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static GETTER_BASE<Owner, T>* Wrap( const T& (Base::*aFunc)() const )
|
|
|
|
{
|
|
|
|
return new GETTER<Owner, T, const T& (Base::*)() const>( aFunc );
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static SETTER_BASE<Owner, T>* Wrap( void (Base::*aFunc)( T ) )
|
|
|
|
{
|
|
|
|
return aFunc ? new SETTER<Owner, T, void (Base::*)( T )>( aFunc ) : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static SETTER_BASE<Owner, T>* Wrap( void (Base::*aFunc)( T& ) )
|
|
|
|
{
|
|
|
|
return aFunc ? new SETTER<Owner, T, void (Base::*)( T& )>( aFunc ) : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static SETTER_BASE<Owner, T>* Wrap( void (Base::*aFunc)( const T& ) )
|
|
|
|
{
|
|
|
|
return aFunc ? new SETTER<Owner, T, void (Base::*)( const T& )>( aFunc ) : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
METHOD() = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PROPERTY_BASE
|
|
|
|
{
|
2020-12-21 18:06:03 +00:00
|
|
|
private:
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Used to generate unique IDs. Must come up front so it's initialized before ctor.
|
2020-12-21 18:06:03 +00:00
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
public:
|
2020-04-06 16:23:21 +00:00
|
|
|
PROPERTY_BASE( const wxString& aName, PROPERTY_DISPLAY aDisplay = PT_DEFAULT ) :
|
2020-07-21 22:42:40 +00:00
|
|
|
m_name( aName ),
|
2020-02-02 18:40:14 +00:00
|
|
|
m_display( aDisplay ),
|
2020-02-02 18:29:33 +00:00
|
|
|
m_availFunc( [](INSPECTABLE*)->bool { return true; } )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~PROPERTY_BASE()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const wxString& Name() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return a limited set of possible values (e.g. enum). Check with HasChoices() if a particular
|
2020-02-02 18:29:33 +00:00
|
|
|
* PROPERTY provides such set.
|
|
|
|
*/
|
|
|
|
virtual const wxPGChoices& Choices() const
|
|
|
|
{
|
|
|
|
static wxPGChoices empty;
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:59:45 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Set the possible values for for the property.
|
2020-02-05 14:59:45 +00:00
|
|
|
*/
|
|
|
|
virtual void SetChoices( const wxPGChoices& aChoices )
|
|
|
|
{
|
|
|
|
wxFAIL; // only possible for PROPERTY_ENUM
|
|
|
|
}
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return true if this PROPERTY has a limited set of possible values.
|
2020-02-02 18:29:33 +00:00
|
|
|
* @see PROPERTY_BASE::Choices()
|
|
|
|
*/
|
|
|
|
virtual bool HasChoices() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return true if aObject offers this PROPERTY.
|
2020-02-02 18:29:33 +00:00
|
|
|
*/
|
|
|
|
bool Available( INSPECTABLE* aObject ) const
|
|
|
|
{
|
|
|
|
return m_availFunc( aObject );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Set a callback function to determine whether an object provides this property.
|
2020-02-02 18:29:33 +00:00
|
|
|
*/
|
|
|
|
void SetAvailableFunc( std::function<bool(INSPECTABLE*)> aFunc )
|
|
|
|
{
|
|
|
|
m_availFunc = aFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return type-id of the Owner class.
|
2020-02-02 18:29:33 +00:00
|
|
|
*/
|
|
|
|
virtual size_t OwnerHash() const = 0;
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return type-id of the Base class.
|
2020-02-05 14:59:45 +00:00
|
|
|
*/
|
|
|
|
virtual size_t BaseHash() const = 0;
|
|
|
|
|
|
|
|
/**
|
2021-01-25 12:42:36 +00:00
|
|
|
* Return type-id of the property type.
|
2020-02-02 18:29:33 +00:00
|
|
|
*/
|
|
|
|
virtual size_t TypeHash() const = 0;
|
|
|
|
|
|
|
|
virtual bool IsReadOnly() const = 0;
|
|
|
|
|
2020-02-02 18:40:14 +00:00
|
|
|
PROPERTY_DISPLAY Display() const
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
2020-02-02 18:40:14 +00:00
|
|
|
return m_display;
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template<typename T>
|
|
|
|
void set( void* aObject, T aValue )
|
|
|
|
{
|
|
|
|
wxAny a = aValue;
|
|
|
|
setter( aObject, a );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-02-06 07:40:55 +00:00
|
|
|
T get( const void* aObject ) const
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
wxAny a = getter( aObject );
|
|
|
|
|
2020-02-05 14:59:45 +00:00
|
|
|
if ( !( std::is_enum<T>::value && a.CheckType<int>() ) && !a.CheckType<T>() )
|
2020-11-15 17:48:25 +00:00
|
|
|
throw std::invalid_argument( "Invalid requested type" );
|
2020-02-02 18:29:33 +00:00
|
|
|
|
2020-02-06 07:39:06 +00:00
|
|
|
return wxANY_AS( a, T );
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 07:40:55 +00:00
|
|
|
private:
|
2020-02-02 18:29:33 +00:00
|
|
|
virtual void setter( void* aObject, wxAny& aValue ) = 0;
|
2020-02-06 07:40:55 +00:00
|
|
|
virtual wxAny getter( const void* aObject ) const = 0;
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
private:
|
2020-02-02 18:29:33 +00:00
|
|
|
const wxString m_name;
|
|
|
|
const PROPERTY_DISPLAY m_display;
|
2020-02-02 18:29:33 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
std::function<bool(INSPECTABLE*)> m_availFunc; ///< Eval to determine if prop is available
|
2020-12-21 15:17:52 +00:00
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
friend class INSPECTABLE;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Owner, typename T, typename Base = Owner>
|
|
|
|
class PROPERTY : public PROPERTY_BASE
|
|
|
|
{
|
|
|
|
public:
|
2020-02-06 07:40:55 +00:00
|
|
|
using BASE_TYPE = typename std::decay<T>::type;
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
template<typename SetType, typename GetType>
|
|
|
|
PROPERTY( const wxString& aName,
|
|
|
|
void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )(),
|
2020-04-06 16:23:21 +00:00
|
|
|
PROPERTY_DISPLAY aDisplay = PT_DEFAULT )
|
2020-02-02 18:29:33 +00:00
|
|
|
: PROPERTY( aName, METHOD<Owner, T, Base>::Wrap( aSetter ),
|
2020-02-02 18:40:14 +00:00
|
|
|
METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename SetType, typename GetType>
|
|
|
|
PROPERTY( const wxString& aName,
|
|
|
|
void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )() const,
|
2020-04-06 16:23:21 +00:00
|
|
|
PROPERTY_DISPLAY aDisplay = PT_DEFAULT )
|
2020-02-02 18:29:33 +00:00
|
|
|
: PROPERTY( aName, METHOD<Owner, T, Base>::Wrap( aSetter ),
|
2020-02-02 18:40:14 +00:00
|
|
|
METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t OwnerHash() const override
|
|
|
|
{
|
|
|
|
return m_ownerHash;
|
|
|
|
}
|
|
|
|
|
2020-02-05 14:59:45 +00:00
|
|
|
size_t BaseHash() const override
|
|
|
|
{
|
|
|
|
return m_baseHash;
|
|
|
|
}
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
size_t TypeHash() const override
|
|
|
|
{
|
|
|
|
return m_typeHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsReadOnly() const override
|
|
|
|
{
|
|
|
|
return !m_setter;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PROPERTY( const wxString& aName, SETTER_BASE<Owner, T>* s, GETTER_BASE<Owner, T>* g,
|
2020-02-02 18:40:14 +00:00
|
|
|
PROPERTY_DISPLAY aDisplay )
|
|
|
|
: PROPERTY_BASE( aName, aDisplay ), m_setter( s ), m_getter( g ),
|
2020-02-05 14:59:45 +00:00
|
|
|
m_ownerHash( TYPE_HASH( Owner ) ), m_baseHash( TYPE_HASH( Base ) ),
|
|
|
|
m_typeHash( TYPE_HASH( BASE_TYPE ) )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~PROPERTY() {}
|
|
|
|
|
|
|
|
virtual void setter( void* obj, wxAny& v ) override
|
|
|
|
{
|
|
|
|
wxCHECK( !IsReadOnly(), /*void*/ );
|
|
|
|
|
|
|
|
if( !v.CheckType<T>() )
|
|
|
|
throw std::invalid_argument( "Invalid type requested" );
|
|
|
|
|
|
|
|
Owner* o = reinterpret_cast<Owner*>( obj );
|
|
|
|
BASE_TYPE value = wxANY_AS(v, BASE_TYPE);
|
|
|
|
(*m_setter)( o, value );
|
|
|
|
}
|
|
|
|
|
2020-02-06 07:40:55 +00:00
|
|
|
virtual wxAny getter( const void* obj ) const override
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
2020-02-06 07:40:55 +00:00
|
|
|
const Owner* o = reinterpret_cast<const Owner*>( obj );
|
2020-02-02 18:29:33 +00:00
|
|
|
wxAny res = (*m_getter)( o );
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Set method
|
2020-02-02 18:29:33 +00:00
|
|
|
std::unique_ptr<SETTER_BASE<Owner, T>> m_setter;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Get method
|
2020-02-02 18:29:33 +00:00
|
|
|
std::unique_ptr<GETTER_BASE<Owner, T>> m_getter;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Owner class type-id
|
2020-02-02 18:29:33 +00:00
|
|
|
const size_t m_ownerHash;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Base class type-id
|
2020-02-05 14:59:45 +00:00
|
|
|
const size_t m_baseHash;
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Property value type-id
|
2020-02-02 18:29:33 +00:00
|
|
|
const size_t m_typeHash;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Owner, typename T, typename Base = Owner>
|
|
|
|
class PROPERTY_ENUM : public PROPERTY<Owner, T, Base>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename SetType, typename GetType>
|
|
|
|
PROPERTY_ENUM( const wxString& aName,
|
|
|
|
void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )(),
|
2020-04-06 16:23:21 +00:00
|
|
|
PROPERTY_DISPLAY aDisplay = PT_DEFAULT )
|
2020-02-05 14:59:45 +00:00
|
|
|
: PROPERTY<Owner, T, Base>( aName, METHOD<Owner, T, Base>::Wrap( aSetter ),
|
2020-02-02 18:40:14 +00:00
|
|
|
METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
2020-02-05 14:59:45 +00:00
|
|
|
if ( std::is_enum<T>::value )
|
|
|
|
{
|
|
|
|
m_choices = ENUM_MAP<T>::Instance().Choices();
|
2022-02-09 11:14:08 +00:00
|
|
|
wxASSERT_MSG( m_choices.GetCount() > 0, wxT( "No enum choices defined" ) );
|
2020-02-05 14:59:45 +00:00
|
|
|
}
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename SetType, typename GetType>
|
|
|
|
PROPERTY_ENUM( const wxString& aName,
|
|
|
|
void ( Base::*aSetter )( SetType ), GetType( Base::*aGetter )() const,
|
2020-04-06 16:23:21 +00:00
|
|
|
PROPERTY_DISPLAY aDisplay = PT_DEFAULT )
|
2020-02-05 14:59:45 +00:00
|
|
|
: PROPERTY<Owner, T, Base>( aName, METHOD<Owner, T, Base>::Wrap( aSetter ),
|
2020-02-02 18:40:14 +00:00
|
|
|
METHOD<Owner, T, Base>::Wrap( aGetter ), aDisplay )
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
2020-02-05 14:59:45 +00:00
|
|
|
if ( std::is_enum<T>::value )
|
|
|
|
{
|
|
|
|
m_choices = ENUM_MAP<T>::Instance().Choices();
|
2022-02-09 11:14:08 +00:00
|
|
|
wxASSERT_MSG( m_choices.GetCount() > 0, wxT( "No enum choices defined" ) );
|
2020-02-05 14:59:45 +00:00
|
|
|
}
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-06 07:40:55 +00:00
|
|
|
void setter( void* obj, wxAny& v ) override
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
wxCHECK( !( PROPERTY<Owner, T, Base>::IsReadOnly() ), /*void*/ );
|
|
|
|
Owner* o = reinterpret_cast<Owner*>( obj );
|
|
|
|
|
|
|
|
if( v.CheckType<T>() )
|
|
|
|
{
|
|
|
|
T value = wxANY_AS(v, T);
|
|
|
|
(*PROPERTY<Owner, T, Base>::m_setter)( o, value );
|
|
|
|
}
|
|
|
|
else if (v.CheckType<int>() )
|
|
|
|
{
|
|
|
|
int value = wxANY_AS(v, int);
|
|
|
|
(*PROPERTY<Owner, T, Base>::m_setter)( o, static_cast<T>( value ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::invalid_argument( "Invalid type requested" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 07:40:55 +00:00
|
|
|
wxAny getter( const void* obj ) const override
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
2020-02-06 07:40:55 +00:00
|
|
|
const Owner* o = reinterpret_cast<const Owner*>( obj );
|
2020-02-02 18:29:33 +00:00
|
|
|
wxAny res = static_cast<T>( (*PROPERTY<Owner, T, Base>::m_getter)( o ) );
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wxPGChoices& Choices() const override
|
|
|
|
{
|
2020-02-02 18:29:33 +00:00
|
|
|
return m_choices.GetCount() > 0 ? m_choices : ENUM_MAP<T>::Instance().Choices();
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 14:59:45 +00:00
|
|
|
void SetChoices( const wxPGChoices& aChoices ) override
|
|
|
|
{
|
|
|
|
m_choices = aChoices;
|
|
|
|
}
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
bool HasChoices() const override
|
|
|
|
{
|
2020-02-02 18:29:33 +00:00
|
|
|
return Choices().GetCount() > 0;
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
wxPGChoices m_choices;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TYPE_CAST_BASE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~TYPE_CAST_BASE() {}
|
|
|
|
virtual void* operator()( void* aPointer ) const = 0;
|
|
|
|
virtual const void* operator()( const void* aPointer ) const = 0;
|
|
|
|
virtual size_t BaseHash() const = 0;
|
|
|
|
virtual size_t DerivedHash() const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Base, typename Derived>
|
|
|
|
class TYPE_CAST : public TYPE_CAST_BASE
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TYPE_CAST()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void* operator()( void* aPointer ) const override
|
|
|
|
{
|
|
|
|
Base* base = reinterpret_cast<Base*>( aPointer );
|
|
|
|
return static_cast<Derived*>( base );
|
|
|
|
}
|
|
|
|
|
|
|
|
const void* operator()( const void* aPointer ) const override
|
|
|
|
{
|
|
|
|
const Base* base = reinterpret_cast<const Base*>( aPointer );
|
|
|
|
return static_cast<const Derived*>( base );
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t BaseHash() const override
|
|
|
|
{
|
|
|
|
return TYPE_HASH( Base );
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DerivedHash() const override
|
|
|
|
{
|
|
|
|
return TYPE_HASH( Derived );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class ENUM_MAP
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static ENUM_MAP<T>& Instance()
|
|
|
|
{
|
|
|
|
static ENUM_MAP<T> inst;
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENUM_MAP& Map( T aValue, const wxString& aName )
|
|
|
|
{
|
|
|
|
m_choices.Add( aName, static_cast<int>( aValue ) );
|
2020-06-17 22:35:32 +00:00
|
|
|
m_reverseMap[ aName ] = aValue;
|
2020-02-02 18:29:33 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-07-23 10:35:05 +00:00
|
|
|
ENUM_MAP& Undefined( T aValue )
|
2020-07-19 21:22:49 +00:00
|
|
|
{
|
2020-07-23 10:35:05 +00:00
|
|
|
m_undefined = aValue;
|
|
|
|
return *this;
|
2020-07-19 21:22:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 18:29:33 +00:00
|
|
|
const wxString& ToString( T value ) const
|
|
|
|
{
|
2020-07-23 10:35:05 +00:00
|
|
|
static const wxString s_undef = "UNDEFINED";
|
|
|
|
|
2020-07-29 21:12:17 +00:00
|
|
|
int idx = m_choices.Index( static_cast<int>( value ) );
|
2020-07-23 10:35:05 +00:00
|
|
|
|
|
|
|
if( idx >= 0 && idx < (int) m_choices.GetCount() )
|
2020-07-29 21:12:17 +00:00
|
|
|
return m_choices.GetLabel( static_cast<int>( idx ) );
|
2020-07-23 10:35:05 +00:00
|
|
|
else
|
|
|
|
return s_undef;
|
2020-02-02 18:29:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 12:02:50 +00:00
|
|
|
bool IsValueDefined( T value ) const
|
|
|
|
{
|
|
|
|
int idx = m_choices.Index( static_cast<int>( value ) );
|
|
|
|
|
|
|
|
if( idx >= 0 && idx < (int) m_choices.GetCount() )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-17 22:35:32 +00:00
|
|
|
const T ToEnum( const wxString value )
|
|
|
|
{
|
2020-07-19 21:22:49 +00:00
|
|
|
if( m_reverseMap.count( value ) )
|
|
|
|
return m_reverseMap[ value ];
|
|
|
|
else
|
2020-07-23 10:35:05 +00:00
|
|
|
return m_undefined;
|
2020-06-17 22:35:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 22:28:54 +00:00
|
|
|
wxPGChoices& Choices()
|
2020-02-02 18:29:33 +00:00
|
|
|
{
|
|
|
|
return m_choices;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-19 21:22:49 +00:00
|
|
|
wxPGChoices m_choices;
|
2020-06-17 22:35:32 +00:00
|
|
|
std::unordered_map<wxString, T> m_reverseMap;
|
2020-07-23 10:35:05 +00:00
|
|
|
T m_undefined; // Returned if the string is not recognized
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
ENUM_MAP<T>()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Helper macros to handle enum types
|
2020-12-21 15:17:52 +00:00
|
|
|
#define DECLARE_ENUM_TO_WXANY( type ) \
|
|
|
|
template <> \
|
|
|
|
class wxAnyValueTypeImpl<type> : public wxAnyValueTypeImplBase<type> \
|
|
|
|
{ \
|
|
|
|
WX_DECLARE_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> ) \
|
|
|
|
public: \
|
|
|
|
wxAnyValueTypeImpl() : wxAnyValueTypeImplBase<type>() {} \
|
|
|
|
virtual ~wxAnyValueTypeImpl() {} \
|
|
|
|
virtual bool ConvertValue( const wxAnyValueBuffer& src, wxAnyValueType* dstType, \
|
|
|
|
wxAnyValueBuffer& dst ) const override \
|
|
|
|
{ \
|
|
|
|
type value = GetValue( src ); \
|
|
|
|
ENUM_MAP<type>& conv = ENUM_MAP<type>::Instance(); \
|
2021-05-03 12:02:50 +00:00
|
|
|
if( ! conv.IsValueDefined( value ) ) \
|
|
|
|
{ \
|
|
|
|
return false; \
|
|
|
|
} \
|
2020-12-21 15:17:52 +00:00
|
|
|
if( dstType->CheckType<wxString>() ) \
|
|
|
|
{ \
|
|
|
|
wxAnyValueTypeImpl<wxString>::SetValue( conv.ToString( value ), dst ); \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
if( dstType->CheckType<int>() ) \
|
|
|
|
{ \
|
|
|
|
wxAnyValueTypeImpl<int>::SetValue( static_cast<int>( value ), dst ); \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} \
|
2020-02-02 18:29:33 +00:00
|
|
|
};
|
|
|
|
|
2020-12-21 15:17:52 +00:00
|
|
|
#define IMPLEMENT_ENUM_TO_WXANY( type ) WX_IMPLEMENT_ANY_VALUE_TYPE( wxAnyValueTypeImpl<type> )
|
2020-02-02 18:29:33 +00:00
|
|
|
|
2020-12-21 15:17:52 +00:00
|
|
|
#define ENUM_TO_WXANY( type ) \
|
|
|
|
DECLARE_ENUM_TO_WXANY( type ) \
|
|
|
|
IMPLEMENT_ENUM_TO_WXANY( type )
|
2020-02-02 18:29:33 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Macro to define read-only fields (no setter method available)
|
2020-12-21 15:17:52 +00:00
|
|
|
#define NO_SETTER( owner, type ) ( ( void ( owner::* )( type ) ) nullptr )
|
2020-02-02 18:29:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
#define DECLARE_PROPERTY(owner,type,name,getter,setter) \
|
|
|
|
namespace anonymous {\
|
|
|
|
static PROPERTY<owner,type> prop##_owner##_name_( "##name#", setter, getter );\
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
#endif /* PROPERTY_H */
|