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>
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
78template <typename ValueType>
79void PARAM_LIST<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
80{
81 if( m_readOnly )
82 return;
83
84 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
85 {
86 std::vector<ValueType> val;
87
88 if( js->is_array() )
89 {
90 for( const auto& el : js->items() )
91 val.push_back( el.value().get<ValueType>() );
92 }
93
94 *m_ptr = val;
95 }
96 else if( aResetIfMissing )
97 *m_ptr = m_default;
98}
99
100
101template <typename ValueType>
103{
104 nlohmann::json js = nlohmann::json::array();
105
106 for( const auto& el : *m_ptr )
107 js.push_back( el );
108
109 aSettings->Set<nlohmann::json>( m_path, js );
110}
111
112
113template <typename ValueType>
115{
116 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
117 {
118 if( js->is_array() )
119 {
120 std::vector<ValueType> val;
121
122 for( const auto& el : js->items() )
123 val.emplace_back( el.value().get<ValueType>() );
124
125 return val == *m_ptr;
126 }
127 }
128
129 return false;
130}
131
132
133template class PARAM_LIST<int>;
134template class PARAM_LIST<double>;
135template class PARAM_LIST<wxString>;
136template class PARAM_LIST<KIGFX::COLOR4D>;
137template class PARAM_LIST<FILE_INFO_PAIR>;
138template class PARAM_LIST<BOM_PRESET>;
139template class PARAM_LIST<BOM_FMT_PRESET>;
140
141
142template <typename ValueType>
143void PARAM_SET<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
144{
145 if( m_readOnly )
146 return;
147
148 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
149 {
150 std::set<ValueType> val;
151
152 if( js->is_array() )
153 {
154 for( const auto& el : js->items() )
155 val.insert( el.value().get<ValueType>() );
156 }
157
158 *m_ptr = val;
159 }
160 else if( aResetIfMissing )
161 *m_ptr = m_default;
162}
163
164
165template <typename ValueType>
167{
168 nlohmann::json js = nlohmann::json::array();
169
170 for( const auto& el : *m_ptr )
171 js.push_back( el );
172
173 aSettings->Set<nlohmann::json>( m_path, js );
174}
175
176
177template <typename ValueType>
179{
180 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
181 {
182 if( js->is_array() )
183 {
184 std::set<ValueType> val;
185
186 for( const auto& el : js->items() )
187 val.insert( el.value().get<ValueType>() );
188
189 return val == *m_ptr;
190 }
191 }
192
193 return false;
194}
195
196
197template class PARAM_SET<wxString>;
198
199
200void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const
201{
202 nlohmann::json js = nlohmann::json::array();
203
204 for( const auto& el : *m_ptr )
205 js.push_back( toFileFormat( el ) );
206
207 aSettings->Set<nlohmann::json>( m_path, js );
208}
209
210
212{
213 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
214 {
215 if( js->is_array() )
216 {
217 std::vector<wxString> val;
218
219 for( const auto& el : js->items() )
220 val.emplace_back( fromFileFormat( el.value().get<wxString>() ) );
221
222 return val == *m_ptr;
223 }
224 }
225
226 return false;
227}
228
229
230template <typename Value>
231void PARAM_MAP<Value>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
232{
233 if( m_readOnly )
234 return;
235
236 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
237 {
238 if( js->is_object() )
239 {
240 m_ptr->clear();
241
242 for( const auto& el : js->items() )
243 ( *m_ptr )[el.key()] = el.value().get<Value>();
244 }
245 }
246 else if( aResetIfMissing )
247 *m_ptr = m_default;
248}
249
250
251template <typename Value>
253{
254 nlohmann::json js( {} );
255
256 for( const auto& el : *m_ptr )
257 js[el.first] = el.second;
258
259 aSettings->Set<nlohmann::json>( m_path, js );
260}
261
262
263template <typename Value>
265{
266 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
267 {
268 if( js->is_object() )
269 {
270 if( m_ptr->size() != js->size() )
271 return false;
272
273 std::map<std::string, Value> val;
274
275 for( const auto& el : js->items() )
276 val[el.key()] = el.value().get<Value>();
277
278 return val == *m_ptr;
279 }
280 }
281
282 return false;
283}
284
285
286template class PARAM_MAP<int>;
287template class PARAM_MAP<double>;
288template class PARAM_MAP<bool>;
289
290
291void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
292{
294 return;
295
296 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
297 {
298 if( js->is_object() )
299 {
300 m_ptr->clear();
301
302 for( const auto& el : js->items() )
303 {
304 ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
305 }
306 }
307 }
308 else if( aResetIfMissing )
309 *m_ptr = m_default;
310}
311
312
314{
315 nlohmann::json js( {} );
317 for( const auto& el : *m_ptr )
318 {
319 std::string key( el.first.ToUTF8() );
320 js[key] = el.second;
321 }
322
323 aSettings->Set<nlohmann::json>( m_path, js );
324}
325
326
328{
329 if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
330 {
331 if( js->is_object() )
332 {
333 if( m_ptr->size() != js->size() )
334 return false;
335
336 std::map<wxString, wxString> val;
337
338 for( const auto& el : js->items() )
339 {
340 wxString key( el.key().c_str(), wxConvUTF8 );
341 val[key] = el.value().get<wxString>();
342 }
343
344 return val == *m_ptr;
345 }
346 }
347
348 return false;
349}
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: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:114
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:102
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:79
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:264
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:252
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:231
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:211
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:200
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:166
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:143
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:178
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:313
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:327
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:291
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:602
std::map< wxString, wxString > m_default
Definition: parameters.h:604