KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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>
30
31template <typename ValueType>
32void PARAM_LAMBDA<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
33{
34 if( m_readOnly )
35 return;
36
37 if( std::is_same<ValueType, nlohmann::json>::value )
38 {
39 if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
40 m_setter( *optval );
41 else
42 m_setter( m_default );
43 }
44 else
45 {
46 if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
47 m_setter( *optval );
48 else
49 m_setter( m_default );
50 }
51}
52
53
54template <typename ValueType>
56{
57 if( std::is_same<ValueType, nlohmann::json>::value )
58 {
59 if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
60 return *optval == m_getter();
61 }
62 else
63 {
64 if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
65 return *optval == m_getter();
66 }
67
68 // Not in file
69 return false;
70}
71
72
73// Instantiate all required templates here to allow reducing scope of json.hpp
74template class PARAM_LAMBDA<bool>;
75template class PARAM_LAMBDA<int>;
76template class PARAM_LAMBDA<nlohmann::json>;
77template class PARAM_LAMBDA<std::string>;
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 {
125 try
126 {
127 val.emplace_back( el.value().get<ValueType>() );
128 }
129 catch( ... )
130 {
131 // Probably typecast didn't work; skip this element
132 }
133 }
134
135 return val == *m_ptr;
136 }
137 }
138
139 return false;
140}
141
142
143template class PARAM_LIST<int>;
144template class PARAM_LIST<double>;
145template class PARAM_LIST<wxString>;
146template class PARAM_LIST<KIGFX::COLOR4D>;
147template class PARAM_LIST<FILE_INFO_PAIR>;
148template class PARAM_LIST<BOM_PRESET>;
149template class PARAM_LIST<BOM_FMT_PRESET>;
150template class PARAM_LIST<GRID>;
151
152
153template <typename ValueType>
154void PARAM_SET<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
155{
156 if( m_readOnly )
157 return;
158
159 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
160 {
161 std::set<ValueType> val;
162
163 if( js->is_array() )
164 {
165 for( const auto& el : js->items() )
166 val.insert( el.value().get<ValueType>() );
167 }
168
169 *m_ptr = val;
170 }
171 else if( aResetIfMissing )
172 *m_ptr = m_default;
173}
174
175
176template <typename ValueType>
178{
179 nlohmann::json js = nlohmann::json::array();
180
181 for( const auto& el : *m_ptr )
182 js.push_back( el );
183
184 aSettings->Set<nlohmann::json>( m_path, js );
185}
186
187
188template <typename ValueType>
190{
191 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
192 {
193 if( js->is_array() )
194 {
195 std::set<ValueType> val;
196
197 for( const auto& el : js->items() )
198 val.insert( el.value().get<ValueType>() );
199
200 return val == *m_ptr;
201 }
202 }
203
204 return false;
205}
206
207
208template class PARAM_SET<wxString>;
209
210
211void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const
212{
213 nlohmann::json js = nlohmann::json::array();
214
215 for( const auto& el : *m_ptr )
216 js.push_back( toFileFormat( el ) );
217
218 aSettings->Set<nlohmann::json>( m_path, js );
219}
220
221
223{
224 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
225 {
226 if( js->is_array() )
227 {
228 std::vector<wxString> val;
229
230 for( const auto& el : js->items() )
231 val.emplace_back( fromFileFormat( el.value().get<wxString>() ) );
232
233 return val == *m_ptr;
234 }
235 }
236
237 return false;
238}
239
240
241template <typename Value>
242void PARAM_MAP<Value>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
243{
244 if( m_readOnly )
245 return;
246
247 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
248 {
249 if( js->is_object() )
250 {
251 m_ptr->clear();
252
253 for( const auto& el : js->items() )
254 ( *m_ptr )[el.key()] = el.value().get<Value>();
255 }
256 }
257 else if( aResetIfMissing )
258 *m_ptr = m_default;
259}
260
261
262template <typename Value>
264{
265 nlohmann::json js( {} );
266
267 for( const auto& el : *m_ptr )
268 js[el.first] = el.second;
269
270 aSettings->Set<nlohmann::json>( m_path, js );
271}
272
273
274template <typename Value>
276{
277 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
278 {
279 if( js->is_object() )
280 {
281 if( m_ptr->size() != js->size() )
282 return false;
283
284 std::map<std::string, Value> val;
285
286 for( const auto& el : js->items() )
287 val[el.key()] = el.value().get<Value>();
288
289 return val == *m_ptr;
290 }
291 }
292
293 return false;
294}
295
296
297template class PARAM_MAP<int>;
298template class PARAM_MAP<double>;
299template class PARAM_MAP<bool>;
300
301
302void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
303{
304 if( m_readOnly )
305 return;
306
307 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
308 {
309 if( js->is_object() )
310 {
311 m_ptr->clear();
312
313 for( const auto& el : js->items() )
314 {
315 ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
317 }
318 }
319 else if( aResetIfMissing )
320 *m_ptr = m_default;
321}
322
323
325{
326 nlohmann::json js( {} );
327
328 for( const auto& el : *m_ptr )
329 {
330 std::string key( el.first.ToUTF8() );
331 js[key] = el.second;
332 }
333
334 aSettings->Set<nlohmann::json>( m_path, js );
335}
336
337
339{
340 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
341 {
342 if( js->is_object() )
343 {
344 if( m_ptr->size() != js->size() )
345 return false;
346
347 std::map<wxString, wxString> val;
348
349 for( const auto& el : js->items() )
350 {
351 wxString key( el.key().c_str(), wxConvUTF8 );
352 val[key] = el.value().get<wxString>();
353 }
354
355 return val == *m_ptr;
356 }
357 }
358
359 return false;
360}
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
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...
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:32
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:55
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:275
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:263
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:242
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:222
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:211
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:177
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:154
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:189
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:324
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:338
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:302
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:602
std::map< wxString, wxString > m_default
Definition: parameters.h:604