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 (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
21#ifndef STRING_ANY_MAP_H_
22#define STRING_ANY_MAP_H_
23
24#include <string>
25#include <map>
26#include <optional>
27
28#include <wx/any.h>
29
30
35class STRING_ANY_MAP : public std::map<std::string, wxAny>
36{
37 double m_iuScale;
38
39public:
40
41 STRING_ANY_MAP( double aIUScale = 1.0 ) : m_iuScale( aIUScale ) {}
42
43 template <typename T>
44 bool get_to( const std::string& aKey, T& aVar ) const
45 {
46 if( !contains( aKey ) )
47 return false;
48
49 return at( aKey ).GetAs( &aVar );
50 }
51
52 template <typename T>
53 bool get_to_iu( const std::string& aKey, T& aVar ) const
54 {
55 if( !contains( aKey ) )
56 return false;
57
58 const wxAny& value = at( aKey );
59
60 if( value.CheckType<double>() || value.CheckType<int>() || value.CheckType<long>()
61 || value.CheckType<long long>() )
62 {
63 double number;
64
65 if( !value.GetAs( &number ) )
66 return false;
67
68 number *= m_iuScale;
69 aVar = number;
70 }
71 else
72 {
73 if( !value.GetAs( &aVar ) )
74 return false;
75 }
76
77 return true;
78 }
79
80 template <typename T>
81 void set( const std::string& aKey, const T& aVar )
82 {
83 emplace( aKey, aVar );
84 }
85
86 template <typename T>
87 void set_iu( const std::string& aKey, const T& aVar)
88 {
89 emplace( aKey, aVar / m_iuScale );
90 }
91
92 bool contains( const std::string& aKey ) const
93 { //
94 return find( aKey ) != end();
95 }
96
97 template <typename T>
98 std::optional<T> get_opt( const std::string& aKey ) const
99 {
100 if( contains( aKey ) )
101 {
102 T val;
103
104 if( !at( aKey ).GetAs( &val ) )
105 return std::nullopt;
106
107 return val;
108 }
109
110 return std::nullopt;
111 }
112};
113
114
115#endif // STRING_ANY_MAP_H_
A name/value tuple with unique names and wxAny values.
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