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
26#include <settings/parameters.h>
27
28void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const
29{
30 nlohmann::json js = nlohmann::json::array();
31
32 for( const auto& el : *m_ptr )
33 js.push_back( toFileFormat( el ) );
34
35 aSettings->Set<nlohmann::json>( m_path, js );
36}
37
38
39bool PARAM_PATH_LIST::MatchesFile( const JSON_SETTINGS& aSettings ) const
40{
41 if( std::optional<nlohmann::json> js = aSettings.GetJson( m_path ) )
42 {
43 if( js->is_array() )
44 {
45 std::vector<wxString> val;
46
47 for( const auto& el : js->items() )
48 val.emplace_back( fromFileFormat( el.value().get<wxString>() ) );
49
50 return val == *m_ptr;
51 }
52 }
53
54 return false;
55}
56
57
58void PARAM_WXSTRING_MAP::Load( const JSON_SETTINGS& aSettings, bool aResetIfMissing ) const
59{
60 if( m_readOnly )
61 return;
62
63 if( std::optional<nlohmann::json> js = aSettings.GetJson( m_path ) )
64 {
65 if( js->is_object() )
66 {
67 m_ptr->clear();
68
69 for( const auto& el : js->items() )
70 ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
71 }
72 }
73 else if( aResetIfMissing )
74 {
76 }
77}
78
79
81{
82 nlohmann::json js( {} );
83
84 for( const auto& el : *m_ptr )
85 {
86 std::string key( el.first.ToUTF8() );
87 js[key] = el.second;
88 }
89
90 aSettings->Set<nlohmann::json>( m_path, js );
91}
92
93
94bool PARAM_WXSTRING_MAP::MatchesFile( const JSON_SETTINGS& aSettings ) const
95{
96 if( std::optional<nlohmann::json> js = aSettings.GetJson( m_path ) )
97 {
98 if( js->is_object() )
99 {
100 if( m_ptr->size() != js->size() )
101 return false;
102
103 std::map<wxString, wxString> val;
104
105 for( const auto& el : js->items() )
106 {
107 wxString key( el.key().c_str(), wxConvUTF8 );
108 val[key] = el.value().get<wxString>();
109 }
110
111 return val == *m_ptr;
112 }
113 }
114
115 return false;
116}
117
118#if !defined( __MINGW32__ )
119// Instantiate all required templates here and export
124
130//template KICOMMON_API class PARAM_LIST<FILE_INFO_PAIR>;
132
134
138#endif
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< 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....
bool m_readOnly
Indicates param pointer should never be overwritten.
Definition: parameters.h:84
std::string m_path
Address of the param in the json files.
Definition: parameters.h:83
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:295
std::vector< wxString > * m_ptr
Definition: parameters.h:556
Represents a map of <std::string, Value>.
Definition: parameters.h:726
wxString toFileFormat(const wxString &aString) const
Definition: parameters.h:695
wxString fromFileFormat(const wxString &aString) const
Definition: parameters.h:702
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:28
bool MatchesFile(const JSON_SETTINGS &aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:39
void Store(JSON_SETTINGS *aSettings) const override
Stores the value of this parameter to the given JSON_SETTINGS object.
Definition: parameters.cpp:80
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:836
void Load(const JSON_SETTINGS &aSettings, bool aResetIfMissing=true) const override
Loads the value of this parameter from JSON to the underlying storage.
Definition: parameters.cpp:58
bool MatchesFile(const JSON_SETTINGS &aSettings) const override
Checks whether the parameter in memory matches the one in a given JSON file.
Definition: parameters.cpp:94
std::map< wxString, wxString > m_default
Definition: parameters.h:837
#define KICOMMON_API
Definition: kicommon.h:28