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.

123 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 <core/wx_stl_compat.h>
  24. #include <properties/property_mgr.h>
  25. #include <properties/property.h>
  26. #include <optional>
  27. /**
  28. * Class that other classes need to inherit from, in order to be inspectable.
  29. */
  30. class INSPECTABLE
  31. {
  32. public:
  33. virtual ~INSPECTABLE()
  34. {
  35. }
  36. bool Set( PROPERTY_BASE* aProperty, wxAny& aValue )
  37. {
  38. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  39. void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), 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. void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
  49. if( object )
  50. aProperty->set<T>( object, aValue );
  51. return object != nullptr;
  52. }
  53. template<typename T>
  54. bool Set( const wxString& aProperty, T aValue )
  55. {
  56. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  57. TYPE_ID thisType = TYPE_HASH( *this );
  58. PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
  59. void* object = nullptr;
  60. if( prop )
  61. {
  62. object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
  63. if( object )
  64. prop->set<T>( object, aValue );
  65. }
  66. return object != nullptr;
  67. }
  68. wxAny Get( PROPERTY_BASE* aProperty ) const
  69. {
  70. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  71. const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
  72. return object ? aProperty->getter( object ) : wxAny();
  73. }
  74. template<typename T>
  75. T Get( PROPERTY_BASE* aProperty ) const
  76. {
  77. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  78. const void* object = propMgr.TypeCast( this, TYPE_HASH( *this ), aProperty->OwnerHash() );
  79. if( !object )
  80. throw std::runtime_error( "Could not cast INSPECTABLE to the requested type" );
  81. return aProperty->get<T>( object );
  82. }
  83. template<typename T>
  84. std::optional<T> Get( const wxString& aProperty ) const
  85. {
  86. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  87. TYPE_ID thisType = TYPE_HASH( *this );
  88. PROPERTY_BASE* prop = propMgr.GetProperty( thisType, aProperty );
  89. std::optional<T> ret;
  90. if( prop )
  91. {
  92. const void* object = propMgr.TypeCast( this, thisType, prop->OwnerHash() );
  93. if( object )
  94. ret = prop->get<T>( object );
  95. }
  96. return ret;
  97. }
  98. };
  99. #endif /* INSPECTABLE_H */