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.

139 lines
4.1 KiB

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