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 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 MAP_HELPERS_H_
22#define MAP_HELPERS_H_
23
24#include <optional>
25#include <map>
26#include <wx/string.h>
27
28
29template <typename V>
30inline std::optional<V> get_opt( const std::map<wxString, V>& aMap, const wxString& aKey )
31{
32 auto it = aMap.find( aKey );
33
34 if( it == aMap.end() )
35 return std::nullopt;
36
37 return it->second;
38}
39
40
41template <typename V>
42inline std::optional<V> get_opt( const std::map<wxString, V>& aMap, const char* aKey )
43{
44 return get_opt( aMap, wxString::FromUTF8( aKey ) );
45}
46
47
48template <typename K, typename V>
49inline std::optional<V> get_opt( const std::map<K, V>& aMap, const K& aKey )
50{
51 auto it = aMap.find( aKey );
52
53 if( it == aMap.end() )
54 return std::nullopt;
55
56 return it->second;
57}
58
59
60inline wxString get_def( const std::map<wxString, wxString>& aMap, const char* aKey,
61 const char* aDefval = "" )
62{
63 typename std::map<wxString, wxString>::const_iterator it =
64 aMap.find( wxString::FromUTF8( aKey ) );
65 if( it == aMap.end() )
66 {
67 return wxString::FromUTF8( aDefval );
68 }
69 else
70 {
71 return it->second;
72 }
73}
74
75
76inline wxString get_def( const std::map<wxString, wxString>& aMap, const char* aKey,
77 const wxString& aDefval = wxString() )
78{
79 typename std::map<wxString, wxString>::const_iterator it =
80 aMap.find( wxString::FromUTF8( aKey ) );
81 if( it == aMap.end() )
82 {
83 return aDefval;
84 }
85 else
86 {
87 return it->second;
88 }
89}
90
91
92inline wxString get_def( const std::map<wxString, wxString>& aMap, const wxString& aKey,
93 const wxString& aDefval = wxString() )
94{
95 typename std::map<wxString, wxString>::const_iterator it = aMap.find( aKey );
96 if( it == aMap.end() )
97 {
98 return aDefval;
99 }
100 else
101 {
102 return it->second;
103 }
104}
105
106
107template <typename K, typename V>
108inline V get_def( const std::map<K, V>& aMap, const K& aKey, const V& aDefval = V() )
109{
110 typename std::map<K, V>::const_iterator it = aMap.find( aKey );
111 if( it == aMap.end() )
112 {
113 return aDefval;
114 }
115 else
116 {
117 return it->second;
118 }
119}
120
121
122#endif // MAP_HELPERS_H_
wxString get_def(const std::map< wxString, wxString > &aMap, const char *aKey, const char *aDefval="")
Definition map_helpers.h:60
std::optional< V > get_opt(const std::map< wxString, V > &aMap, const wxString &aKey)
Definition map_helpers.h:30