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.

94 lines
2.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef STD_OPTIONAL_VARIANT_H
  20. #define STD_OPTIONAL_VARIANT_H
  21. #include <optional>
  22. #include <wx/variant.h>
  23. /*
  24. * Wrappers to allow use of std::optional<int> and std::optional<double> with the wxVariant
  25. * system.
  26. */
  27. class STD_OPTIONAL_INT_VARIANT_DATA : public wxVariantData
  28. {
  29. public:
  30. STD_OPTIONAL_INT_VARIANT_DATA();
  31. STD_OPTIONAL_INT_VARIANT_DATA( std::optional<int> aValue );
  32. bool Eq( wxVariantData& aOther ) const override;
  33. wxString GetType() const override { return wxT( "std::optional<int>" ); }
  34. bool GetAsAny( wxAny* aAny ) const override
  35. {
  36. *aAny = m_value;
  37. return true;
  38. }
  39. std::optional<int> Value() const
  40. {
  41. return m_value;
  42. }
  43. static wxVariantData* VariantDataFactory( const wxAny& aAny )
  44. {
  45. return new STD_OPTIONAL_INT_VARIANT_DATA( aAny.As<std::optional<int>>() );
  46. }
  47. protected:
  48. std::optional<int> m_value;
  49. };
  50. class STD_OPTIONAL_DOUBLE_VARIANT_DATA : public wxVariantData
  51. {
  52. public:
  53. STD_OPTIONAL_DOUBLE_VARIANT_DATA();
  54. STD_OPTIONAL_DOUBLE_VARIANT_DATA( std::optional<double> aValue );
  55. bool Eq( wxVariantData& aOther ) const override;
  56. wxString GetType() const override { return wxT( "std::optional<double>" ); }
  57. bool GetAsAny( wxAny* aAny ) const override
  58. {
  59. *aAny = m_value;
  60. return true;
  61. }
  62. std::optional<double> Value() const
  63. {
  64. return m_value;
  65. }
  66. static wxVariantData* VariantDataFactory( const wxAny& aAny )
  67. {
  68. return new STD_OPTIONAL_DOUBLE_VARIANT_DATA( aAny.As<std::optional<double>>() );
  69. }
  70. protected:
  71. std::optional<double> m_value;
  72. };
  73. #endif //STD_OPTIONAL_VARIANT_H