You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 3
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef INSPECTABLE_H
  22. #define INSPECTABLE_H
  23. #include "property_mgr.h"
  24. #include "property.h"
  25. #include <boost/optional.hpp>
  26. /**
  27. * Class that other classes need to inherit from, in order to be inspectable.
  28. */
  29. class INSPECTABLE
  30. {
  31. public:
  32. virtual ~INSPECTABLE()
  33. {
  34. }
  35. bool Set( PROPERTY_BASE* aProperty, wxAny& aValue )
  36. {
  37. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  38. TYPE_ID thisType = TYPE_HASH( *this );
  39. void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
  40. if( object )
  41. aProperty->setter( object, aValue );
  42. return object != nullptr;
  43. }
  44. template<typename T>
  45. bool Set( PROPERTY_BASE* aProperty, T aValue )
  46. {
  47. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  48. TYPE_ID thisType = TYPE_HASH( *this );
  49. void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
  50. if( object )
  51. aProperty->set<T>( object, aValue );
  52. return object != nullptr;
  53. }
  54. template<typename T>
  55. bool Set( const wxString& aProperty, T aValue )
  56. {
  57. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  58. TYPE_ID thisType = TYPE_HASH( *this );
  59. PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
  60. void* object = nullptr;
  61. if( prop )
  62. {
  63. object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
  64. if( object )
  65. prop->set<T>( object, aValue );
  66. }
  67. return object != nullptr;
  68. }
  69. wxAny Get( PROPERTY_BASE* aProperty )
  70. {
  71. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  72. TYPE_ID thisType = TYPE_HASH( *this );
  73. void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
  74. return object ? aProperty->getter( object ) : wxAny();
  75. }
  76. template<typename T>
  77. T Get( PROPERTY_BASE* aProperty )
  78. {
  79. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  80. TYPE_ID thisType = TYPE_HASH( *this );
  81. void* object = propMgr.TypeCast( this, thisType, aProperty->OwnerHash() );
  82. return object ? aProperty->get<T>( object ) : T();
  83. }
  84. template<typename T>
  85. boost::optional<T> Get( const wxString& aProperty )
  86. {
  87. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  88. TYPE_ID thisType = TYPE_HASH( *this );
  89. PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
  90. boost::optional<T> ret;
  91. if( prop )
  92. {
  93. void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
  94. if( object )
  95. ret = prop->get<T>( object );
  96. }
  97. return ret;
  98. }
  99. };
  100. #endif /* INSPECTABLE_H */