KiCad PCB EDA Suite
Loading...
Searching...
No Matches
string_any_map.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2023 Alex Shvartzkop <[email protected]>
5 * Copyright The 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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef STRING_ANY_MAP_H_
22#define STRING_ANY_MAP_H_
23
24#include <string>
25#include <map>
26#include <optional>
27#include <type_traits>
28
29#include <math/util.h>
30#include <wx/any.h>
31
32
37class STRING_ANY_MAP : public std::map<std::string, wxAny>
38{
39 double m_iuScale;
40
41public:
42
43 STRING_ANY_MAP( double aIUScale = 1.0 ) : m_iuScale( aIUScale ) {}
44
45 template <typename T>
46 bool get_to( const std::string& aKey, T& aVar ) const
47 {
48 if( !contains( aKey ) )
49 return false;
50
51 return at( aKey ).GetAs( &aVar );
52 }
53
54 template <typename T>
55 bool get_to_iu( const std::string& aKey, T& aVar ) const
56 {
57 if( !contains( aKey ) )
58 return false;
59
60 const wxAny& value = at( aKey );
61
62 if( value.CheckType<double>() || value.CheckType<int>() || value.CheckType<long>()
63 || value.CheckType<long long>() )
64 {
65 double number;
66
67 if( !value.GetAs( &number ) )
68 return false;
69
70 number *= m_iuScale;
71
72 // Most millimetre values are not exact doubles, so truncating would load them 1 IU short
73 if constexpr( std::is_integral_v<T> )
74 aVar = KiROUND<double, T>( number );
75 else
76 aVar = number;
77 }
78 else
79 {
80 if( !value.GetAs( &aVar ) )
81 return false;
82 }
83
84 return true;
85 }
86
87 template <typename T>
88 void set( const std::string& aKey, const T& aVar )
89 {
90 emplace( aKey, aVar );
91 }
92
93 template <typename T>
94 void set_iu( const std::string& aKey, const T& aVar)
95 {
96 emplace( aKey, aVar / m_iuScale );
97 }
98
99 bool contains( const std::string& aKey ) const
100 { //
101 return find( aKey ) != end();
102 }
103
104 template <typename T>
105 std::optional<T> get_opt( const std::string& aKey ) const
106 {
107 if( contains( aKey ) )
108 {
109 T val;
110
111 if( !at( aKey ).GetAs( &val ) )
112 return std::nullopt;
113
114 return val;
115 }
116
117 return std::nullopt;
118 }
119};
120
121
122#endif // STRING_ANY_MAP_H_
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:982
void set_iu(const std::string &aKey, const T &aVar)
bool get_to(const std::string &aKey, T &aVar) const
bool contains(const std::string &aKey) const
std::optional< T > get_opt(const std::string &aKey) const
STRING_ANY_MAP(double aIUScale=1.0)
void set(const std::string &aKey, const T &aVar)
bool get_to_iu(const std::string &aKey, T &aVar) const
VECTOR2I end