KiCad PCB EDA Suite
nested_settings.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) 2020 Jon Evans <jon@craftyjon.com>
5  * Copyright (C) 2020 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/log.h>
22 
24 
25 extern const char* traceSettings;
26 
27 
28 NESTED_SETTINGS::NESTED_SETTINGS( const std::string& aName, int aVersion, JSON_SETTINGS* aParent,
29  const std::string& aPath ) :
30  JSON_SETTINGS( aName, SETTINGS_LOC::NESTED, aVersion ),
31  m_parent( aParent ), m_path( aPath )
32 {
33  SetParent( aParent );
34 }
35 
36 
38 {
39  if( m_parent )
41 }
42 
43 
44 bool NESTED_SETTINGS::LoadFromFile( const std::string& aDirectory )
45 {
46  clear();
47  bool success = false;
48 
49  if( m_parent )
50  {
51  nlohmann::json::json_pointer ptr = PointerFromString( m_path );
52 
53  if( m_parent->contains( ptr ) )
54  {
55  try
56  {
57  update( ( *m_parent )[ptr] );
58 
59  wxLogTrace( traceSettings, "Loaded NESTED_SETTINGS %s with schema %d",
61 
62  success = true;
63  }
64  catch( ... )
65  {
66  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not load from %s at %s",
68  }
69  }
70  }
71 
72  Load();
73 
74  return success;
75 }
76 
77 
78 bool NESTED_SETTINGS::SaveToFile( const std::string& aDirectory, bool aForce )
79 {
80  if( !m_parent )
81  return false;
82 
83  bool modified = Store();
84 
85  try
86  {
87  nlohmann::json patch =
88  nlohmann::json::diff( *this, ( *m_parent )[PointerFromString( m_path )] );
89  modified |= !patch.empty();
90  }
91  catch( ... )
92  {
93  modified = true;
94  }
95 
96  if( !modified && !aForce )
97  return false;
98 
99  try
100  {
101  ( *m_parent )[PointerFromString( m_path ) ].update( *this );
102 
103  wxLogTrace( traceSettings, "Stored NESTED_SETTINGS %s with schema %d",
105  }
106  catch( ... )
107  {
108  wxLogTrace( traceSettings, "NESTED_SETTINGS %s: Could not store to %s at %s",
110  }
111 
112  return modified;
113 }
114 
115 
117 {
118  m_parent = aParent;
119 
120  if( m_parent )
121  {
122  m_parent->AddNestedSettings( this );
123 
124  // In case we were created after the parent's ctor
125  LoadFromFile();
126  }
127 }
virtual bool Store()
Stores the current parameters into the JSON document represented by this object Note: this doesn't do...
std::string m_filename
The filename (not including path) of this settings file.
void clear(int aType=UNDEFINED_TYPE)
Definition: multivector.h:207
SETTINGS_LOC
Definition: json_settings.h:36
nlohmann::json json
Definition: gerbview.cpp:40
void AddNestedSettings(NESTED_SETTINGS *aSettings)
Transfers ownership of a given NESTED_SETTINGS to this object.
bool LoadFromFile(const std::string &aDirectory="") override
Loads the JSON document from the parent and then calls Load()
JSON_SETTINGS * m_parent
A pointer to the parent object to load and store from.
virtual ~NESTED_SETTINGS()
Not stored in a file, but inside another JSON_SETTINGS.
bool SaveToFile(const std::string &aDirectory="", bool aForce=false) override
Calls Store() and then saves the JSON document contents into the parent JSON_SETTINGS.
NESTED_SETTINGS(const std::string &aName, int aSchemaVersion, JSON_SETTINGS *aParent, const std::string &aPath)
std::string m_path
The path (in pointer format) of where to store this document in the parent.
std::string GetFilename() const
Definition: json_settings.h:56
void ReleaseNestedSettings(NESTED_SETTINGS *aSettings)
Saves and frees a nested settings object, if it exists within this one.
const char * traceSettings
Flag to enable settings tracing.
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
int m_schemaVersion
Version of this settings schema.
virtual void Load()
Updates the parameters of this object based on the current JSON document contents.
void SetParent(JSON_SETTINGS *aParent)