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.

114 lines
2.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Alex Shvartzkop <dudesuchamazing@gmail.com>
  5. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef STRING_ANY_MAP_H_
  21. #define STRING_ANY_MAP_H_
  22. #include <string>
  23. #include <map>
  24. #include <optional>
  25. #include <wx/any.h>
  26. /**
  27. * A name/value tuple with unique names and wxAny values. The names
  28. * may be iterated alphabetically.
  29. */
  30. class STRING_ANY_MAP : public std::map<std::string, wxAny>
  31. {
  32. double m_iuScale;
  33. public:
  34. STRING_ANY_MAP( double aIUScale = 1.0 ) : m_iuScale( aIUScale ) {}
  35. template <typename T>
  36. bool get_to( const std::string& aKey, T& aVar ) const
  37. {
  38. if( !contains( aKey ) )
  39. return false;
  40. return at( aKey ).GetAs( &aVar );
  41. }
  42. template <typename T>
  43. bool get_to_iu( const std::string& aKey, T& aVar ) const
  44. {
  45. if( !contains( aKey ) )
  46. return false;
  47. const wxAny& value = at( aKey );
  48. if( value.CheckType<double>() || value.CheckType<int>() || value.CheckType<long>()
  49. || value.CheckType<long long>() )
  50. {
  51. double number;
  52. if( !value.GetAs( &number ) )
  53. return false;
  54. number *= m_iuScale;
  55. aVar = number;
  56. }
  57. else
  58. {
  59. if( !value.GetAs( &aVar ) )
  60. return false;
  61. }
  62. return true;
  63. }
  64. template <typename T>
  65. void set( const std::string& aKey, const T& aVar )
  66. {
  67. emplace( aKey, aVar );
  68. }
  69. template <typename T>
  70. void set_iu( const std::string& aKey, const T& aVar)
  71. {
  72. emplace( aKey, aVar / m_iuScale );
  73. }
  74. bool contains( const std::string& aKey ) const
  75. { //
  76. return find( aKey ) != end();
  77. }
  78. template <typename T>
  79. std::optional<T> get_opt( const std::string& aKey ) const
  80. {
  81. if( contains( aKey ) )
  82. {
  83. T val;
  84. if( !at( aKey ).GetAs( &val ) )
  85. return std::nullopt;
  86. return val;
  87. }
  88. return std::nullopt;
  89. }
  90. };
  91. #endif // STRING_ANY_MAP_H_