KiCad PCB EDA Suite
Loading...
Searching...
No Matches
map_helpers.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
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#ifndef MAP_HELPERS_H_
26#define MAP_HELPERS_H_
27
28#include <optional>
29#include <map>
30#include <wx/string.h>
31
32
33template <typename V>
34inline std::optional<V> get_opt( const std::map<wxString, V>& aMap, const wxString& aKey )
35{
36 auto it = aMap.find( aKey );
37
38 if( it == aMap.end() )
39 return std::nullopt;
40
41 return it->second;
42}
43
44
45template <typename V>
46inline std::optional<V> get_opt( const std::map<wxString, V>& aMap, const char* aKey )
47{
48 return get_opt( aMap, wxString::FromUTF8( aKey ) );
49}
50
51
52template <typename K, typename V>
53inline std::optional<V> get_opt( const std::map<K, V>& aMap, const K& aKey )
54{
55 auto it = aMap.find( aKey );
56
57 if( it == aMap.end() )
58 return std::nullopt;
59
60 return it->second;
61}
62
63
64inline wxString get_def( const std::map<wxString, wxString>& aMap, const char* aKey,
65 const char* aDefval = "" )
66{
67 typename std::map<wxString, wxString>::const_iterator it =
68 aMap.find( wxString::FromUTF8( aKey ) );
69 if( it == aMap.end() )
70 {
71 return wxString::FromUTF8( aDefval );
72 }
73 else
74 {
75 return it->second;
76 }
77}
78
79
80inline wxString get_def( const std::map<wxString, wxString>& aMap, const char* aKey,
81 const wxString& aDefval = wxString() )
82{
83 typename std::map<wxString, wxString>::const_iterator it =
84 aMap.find( wxString::FromUTF8( aKey ) );
85 if( it == aMap.end() )
86 {
87 return aDefval;
88 }
89 else
90 {
91 return it->second;
92 }
93}
94
95
96inline wxString get_def( const std::map<wxString, wxString>& aMap, const wxString& aKey,
97 const wxString& aDefval = wxString() )
98{
99 typename std::map<wxString, wxString>::const_iterator it = aMap.find( aKey );
100 if( it == aMap.end() )
101 {
102 return aDefval;
103 }
104 else
105 {
106 return it->second;
107 }
108}
109
110
111template <typename K, typename V>
112inline V get_def( const std::map<K, V>& aMap, const K& aKey, const V& aDefval = V() )
113{
114 typename std::map<K, V>::const_iterator it = aMap.find( aKey );
115 if( it == aMap.end() )
116 {
117 return aDefval;
118 }
119 else
120 {
121 return it->second;
122 }
123}
124
125
126#endif // MAP_HELPERS_H_
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
Definition: map_helpers.h:64
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
Definition: map_helpers.h:34