KiCad PCB EDA Suite
json_settings_internals.h
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 #ifndef KICAD_JSON_SETTINGS_INTERNALS_H
22 #define KICAD_JSON_SETTINGS_INTERNALS_H
23 
24 // This is a pretty heavy file. Try to use json_fwd.hpp most places.
25 #include <nlohmann/json.hpp>
26 
28 {
29  friend class JSON_SETTINGS;
30 
31 public:
33  nlohmann::json()
34  {}
35 
41  static nlohmann::json::json_pointer PointerFromString( std::string aPath );
42 
43  template<typename ValueType>
44  void SetFromString( const std::string& aPath, ValueType aVal )
45  {
46  // Calls the overload below, which will convert from dotted string to JSON pointer
47  ( *this )[aPath] = aVal;
48  }
49 
50  template<typename ValueType>
51  ValueType Get( const std::string& aPath ) const
52  {
53  return at( PointerFromString( aPath ) ).get<ValueType>();
54  }
55 
56  nlohmann::json& At( const std::string& aPath )
57  {
58  return at( PointerFromString( aPath ) );
59  }
60 
61  nlohmann::json& operator[]( const nlohmann::json::json_pointer& aPointer )
62  {
63  return nlohmann::json::operator[]( aPointer );
64  }
65 
66  nlohmann::json& operator[]( const std::string& aPath )
67  {
68  return nlohmann::json::operator[]( PointerFromString( aPath ) );
69  }
70 
71  void CloneFrom( const JSON_SETTINGS_INTERNALS& aOther )
72  {
73  nlohmann::json::json_pointer root( "" );
74  this->nlohmann::json::operator[]( root ) = aOther.nlohmann::json::operator[]( root );
75  }
76 };
77 
78 #endif // KICAD_JSON_SETTINGS_INTERNALS_H
static nlohmann::json::json_pointer PointerFromString(std::string aPath)
Builds a JSON pointer based on a given string.
nlohmann::json json
Definition: gerbview.cpp:41
nlohmann::json & operator[](const nlohmann::json::json_pointer &aPointer)
void SetFromString(const std::string &aPath, ValueType aVal)
nlohmann::json & At(const std::string &aPath)
ValueType Get(const std::string &aPath) const
void CloneFrom(const JSON_SETTINGS_INTERNALS &aOther)
nlohmann::json & operator[](const std::string &aPath)