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 <jon@craftyjon.com>
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>
26 #include <project/project_file.h>
27 #include <settings/parameters.h>
28 
29 
30 template <typename ValueType>
31 void 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( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
39  m_setter( *optval );
40  else
41  m_setter( m_default );
42  }
43  else
44  {
45  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
46  m_setter( *optval );
47  else
48  m_setter( m_default );
49  }
50 }
51 
52 
53 template <typename ValueType>
55 {
56  if( std::is_same<ValueType, nlohmann::json>::value )
57  {
58  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
59  return *optval == m_getter();
60  }
61  else
62  {
63  if( OPT<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
73 template class PARAM_LAMBDA<bool>;
74 template class PARAM_LAMBDA<int>;
75 template class PARAM_LAMBDA<nlohmann::json>;
76 template class PARAM_LAMBDA<std::string>;
77 
78 
79 template <typename ValueType>
80 void PARAM_LIST<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
81 {
82  if( m_readOnly )
83  return;
84 
85  if( OPT<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 
102 template <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 
114 template <typename ValueType>
116 {
117  if( OPT<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 
134 template class PARAM_LIST<int>;
135 template class PARAM_LIST<double>;
136 template class PARAM_LIST<wxString>;
137 template class PARAM_LIST<KIGFX::COLOR4D>;
138 template class PARAM_LIST<FILE_INFO_PAIR>;
139 
140 
141 void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const
142 {
143  nlohmann::json js = nlohmann::json::array();
144 
145  for( const auto& el : *m_ptr )
146  js.push_back( toFileFormat( el ) );
147 
148  aSettings->Set<nlohmann::json>( m_path, js );
149 }
150 
151 
153 {
154  if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
155  {
156  if( js->is_array() )
157  {
158  std::vector<wxString> val;
159 
160  for( const auto& el : js->items() )
161  val.emplace_back( fromFileFormat( el.value().get<wxString>() ) );
162 
163  return val == *m_ptr;
164  }
165  }
166 
167  return false;
168 }
169 
170 
171 template <typename Value>
172 void PARAM_MAP<Value>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
173 {
174  if( m_readOnly )
175  return;
176 
177  if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
178  {
179  if( js->is_object() )
180  {
181  m_ptr->clear();
182 
183  for( const auto& el : js->items() )
184  ( *m_ptr )[el.key()] = el.value().get<Value>();
185  }
186  }
187  else if( aResetIfMissing )
188  *m_ptr = m_default;
189 }
190 
191 
192 template <typename Value>
193 void PARAM_MAP<Value>::Store( JSON_SETTINGS* aSettings ) const
194 {
195  nlohmann::json js( {} );
196 
197  for( const auto& el : *m_ptr )
198  js[el.first] = el.second;
199 
200  aSettings->Set<nlohmann::json>( m_path, js );
201 }
202 
203 
204 template <typename Value>
206 {
207  if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
208  {
209  if( js->is_object() )
210  {
211  if( m_ptr->size() != js->size() )
212  return false;
213 
214  std::map<std::string, Value> val;
215 
216  for( const auto& el : js->items() )
217  val[el.key()] = el.value().get<Value>();
218 
219  return val == *m_ptr;
220  }
221  }
222 
223  return false;
224 }
225 
226 
227 template class PARAM_MAP<int>;
228 template class PARAM_MAP<double>;
229 template class PARAM_MAP<bool>;
230 
231 
232 void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
233 {
234  if( m_readOnly )
235  return;
236 
237  if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
238  {
239  if( js->is_object() )
240  {
241  m_ptr->clear();
242 
243  for( const auto& el : js->items() )
244  {
245  ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
246  }
247  }
248  }
249  else if( aResetIfMissing )
250  *m_ptr = m_default;
251 }
252 
253 
254 void PARAM_WXSTRING_MAP::Store( JSON_SETTINGS* aSettings ) const
255 {
256  nlohmann::json js( {} );
257 
258  for( const auto& el : *m_ptr )
259  {
260  std::string key( el.first.ToUTF8() );
261  js[key] = el.second;
262  }
263 
264  aSettings->Set<nlohmann::json>( m_path, js );
265 }
266 
267 
269 {
270  if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
271  {
272  if( js->is_object() )
273  {
274  if( m_ptr->size() != js->size() )
275  return false;
276 
277  std::map<wxString, wxString> val;
278 
279  for( const auto& el : js->items() )
280  {
281  wxString key( el.key().c_str(), wxConvUTF8 );
282  val[key] = el.value().get<wxString>();
283  }
284 
285  return val == *m_ptr;
286  }
287  }
288 
289  return false;
290 }
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:172
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...
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:280
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:268
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
Represents a map of <std::string, Value>.
Definition: parameters.h:511
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:152
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:566
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:193
OPT< 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....
std::map< wxString, wxString > m_default
Definition: parameters.h:568
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
nlohmann::json json
Definition: gerbview.cpp:41
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:254
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
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:141
wxString fromFileFormat(const wxString &aString) const
Definition: parameters.h:488
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
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:232
wxString toFileFormat(const wxString &aString) const
Definition: parameters.h:481
std::vector< wxString > * m_ptr
Definition: parameters.h:443
bool MatchesFile(JSON_SETTINGS *aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:205
OPT< 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:77
boost::optional< T > OPT
Definition: optional.h:7
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