KiCad PCB EDA Suite
parameters.cpp
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) 2021 Jon Evans <[email protected]>
5 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * 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#include <wx/string.h>
22
23#include <nlohmann/json.hpp>
24
25#include <gal/color4d.h>
27#include <settings/parameters.h>
28
29
30template <typename ValueType>
31void PARAM_LAMBDA<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
32{
33 if( m_readOnly )
34 return;
35
36 if( std::is_same<ValueType, nlohmann::json>::value )
37 {
38 if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
39 m_setter( *optval );
40 else
41 m_setter( m_default );
42 }
43 else
44 {
45 if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
46 m_setter( *optval );
47 else
48 m_setter( m_default );
49 }
50}
51
52
53template <typename ValueType>
55{
56 if( std::is_same<ValueType, nlohmann::json>::value )
57 {
58 if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
59 return *optval == m_getter();
60 }
61 else
62 {
63 if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
64 return *optval == m_getter();
65 }
66
67 // Not in file
68 return false;
69}
70
71
72// Instantiate all required templates here to allow reducing scope of json.hpp
73template class PARAM_LAMBDA<bool>;
74template class PARAM_LAMBDA<int>;
75template class PARAM_LAMBDA<nlohmann::json>;
76template class PARAM_LAMBDA<std::string>;
77
78
79template <typename ValueType>
80void PARAM_LIST<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
81{
82 if( m_readOnly )
83 return;
84
85 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
86 {
87 std::vector<ValueType> val;
88
89 if( js->is_array() )
90 {
91 for( const auto& el : js->items() )
92 val.push_back( el.value().get<ValueType>() );
93 }
94
95 *m_ptr = val;
96 }
97 else if( aResetIfMissing )
98 *m_ptr = m_default;
99}
100
101
102template <typename ValueType>
104{
105 nlohmann::json js = nlohmann::json::array();
106
107 for( const auto& el : *m_ptr )
108 js.push_back( el );
109
110 aSettings->Set<nlohmann::json>( m_path, js );
111}
112
113
114template <typename ValueType>
116{
117 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
118 {
119 if( js->is_array() )
120 {
121 std::vector<ValueType> val;
122
123 for( const auto& el : js->items() )
124 val.emplace_back( el.value().get<ValueType>() );
125
126 return val == *m_ptr;
127 }
128 }
129
130 return false;
131}
132
133
134template class PARAM_LIST<int>;
135template class PARAM_LIST<double>;
136template class PARAM_LIST<wxString>;
137template class PARAM_LIST<KIGFX::COLOR4D>;
138template class PARAM_LIST<FILE_INFO_PAIR>;
139
140
141template <typename ValueType>
142void PARAM_SET<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
143{
144 if( m_readOnly )
145 return;
146
147 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
148 {
149 std::set<ValueType> val;
150
151 if( js->is_array() )
152 {
153 for( const auto& el : js->items() )
154 val.insert( el.value().get<ValueType>() );
155 }
156
157 *m_ptr = val;
158 }
159 else if( aResetIfMissing )
160 *m_ptr = m_default;
161}
162
163
164template <typename ValueType>
166{
167 nlohmann::json js = nlohmann::json::array();
168
169 for( const auto& el : *m_ptr )
170 js.push_back( el );
171
172 aSettings->Set<nlohmann::json>( m_path, js );
173}
174
175
176template <typename ValueType>
178{
179 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
180 {
181 if( js->is_array() )
182 {
183 std::set<ValueType> val;
184
185 for( const auto& el : js->items() )
186 val.insert( el.value().get<ValueType>() );
187
188 return val == *m_ptr;
189 }
190 }
191
192 return false;
193}
194
195
196template class PARAM_SET<wxString>;
197
198
199void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const
200{
201 nlohmann::json js = nlohmann::json::array();
202
203 for( const auto& el : *m_ptr )
204 js.push_back( toFileFormat( el ) );
205
206 aSettings->Set<nlohmann::json>( m_path, js );
207}
208
209
211{
212 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
213 {
214 if( js->is_array() )
215 {
216 std::vector<wxString> val;
217
218 for( const auto& el : js->items() )
219 val.emplace_back( fromFileFormat( el.value().get<wxString>() ) );
220
221 return val == *m_ptr;
222 }
223 }
224
225 return false;
226}
227
228
229template <typename Value>
230void PARAM_MAP<Value>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
231{
232 if( m_readOnly )
233 return;
234
235 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
236 {
237 if( js->is_object() )
238 {
239 m_ptr->clear();
240
241 for( const auto& el : js->items() )
242 ( *m_ptr )[el.key()] = el.value().get<Value>();
243 }
244 }
245 else if( aResetIfMissing )
246 *m_ptr = m_default;
247}
248
249
250template <typename Value>
252{
253 nlohmann::json js( {} );
254
255 for( const auto& el : *m_ptr )
256 js[el.first] = el.second;
257
258 aSettings->Set<nlohmann::json>( m_path, js );
259}
260
261
262template <typename Value>
264{
265 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
266 {
267 if( js->is_object() )
268 {
269 if( m_ptr->size() != js->size() )
270 return false;
271
272 std::map<std::string, Value> val;
273
274 for( const auto& el : js->items() )
275 val[el.key()] = el.value().get<Value>();
276
277 return val == *m_ptr;
278 }
279 }
280
281 return false;
282}
283
284
285template class PARAM_MAP<int>;
286template class PARAM_MAP<double>;
287template class PARAM_MAP<bool>;
288
289
290void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
291{
292 if( m_readOnly )
293 return;
294
295 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
296 {
297 if( js->is_object() )
298 {
299 m_ptr->clear();
300
301 for( const auto& el : js->items() )
302 {
303 ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
304 }
305 }
306 }
307 else if( aResetIfMissing )
308 *m_ptr = m_default;
309}
310
311
313{
314 nlohmann::json js( {} );
315
316 for( const auto& el : *m_ptr )
317 {
318 std::string key( el.first.ToUTF8() );
319 js[key] = el.second;
320 }
321
322 aSettings->Set<nlohmann::json>( m_path, js );
323}
324
325
327{
328 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
329 {
330 if( js->is_object() )
331 {
332 if( m_ptr->size() != js->size() )
333 return false;
334
335 std::map<wxString, wxString> val;
336
337 for( const auto& el : js->items() )
338 {
339 wxString key( el.key().c_str(), wxConvUTF8 );
340 val[key] = el.value().get<wxString>();
341 }
342
343 return val == *m_ptr;
344 }
345 }
346
347 return false;
348}
std::optional< nlohmann::json > GetJson(const std::string &aPath) const
Fetches a JSON object that is a subset of this JSON_SETTINGS object, using a path of the form "key1....
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:78
std::string m_path
the string used to store the param in json files
Definition: parameters.h:75
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:282
void Load(JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.
Definition: parameters.cpp:31
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:54
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:115
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:103
std::vector< wxString > * m_ptr
Definition: parameters.h:444
void Load(JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.
Definition: parameters.cpp:80
Represents a map of <std::string, Value>.
Definition: parameters.h:548
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:263
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:251
void Load(JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.
Definition: parameters.cpp:230
wxString toFileFormat(const wxString &aString) const
Definition: parameters.h:517
wxString fromFileFormat(const wxString &aString) const
Definition: parameters.h:524
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:210
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:199
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:165
void Load(JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.
Definition: parameters.cpp:142
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:177
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:312
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:326
void Load(JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.
Definition: parameters.cpp:290
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:602
std::map< wxString, wxString > m_default
Definition: parameters.h:604
nlohmann::json json
Definition: gerbview.cpp:44