KiCad PCB EDA Suite
PARAM_MAP< Value > Class Template Reference

Represents a map of <std::string, Value>. More...

#include <parameters.h>

Inheritance diagram for PARAM_MAP< Value >:
PARAM_BASE

Public Member Functions

 PARAM_MAP (const std::string &aJsonPath, std::map< std::string, Value > *aPtr, std::initializer_list< std::pair< const std::string, Value >> aDefault, bool aReadOnly=false)
 
void Load (JSON_SETTINGS *aSettings, bool aResetIfMissing=true) const override
 Loads the value of this parameter from JSON to the underlying storage. More...
 
void Store (JSON_SETTINGS *aSettings) const override
 Stores the value of this parameter to the given JSON_SETTINGS object. More...
 
virtual void SetDefault () override
 
bool MatchesFile (JSON_SETTINGS *aSettings) const override
 Checks whether the parameter in memory matches the one in a given JSON file. More...
 
const std::string & GetJsonPath () const
 

Protected Attributes

std::string m_path
 the string used to store the param in json files More...
 
bool m_readOnly
 ! True if the parameter pointer should never be overwritten More...
 

Private Attributes

std::map< std::string, Value > * m_ptr
 
std::map< std::string, Value > m_default
 

Detailed Description

template<typename Value>
class PARAM_MAP< Value >

Represents a map of <std::string, Value>.

The key parameter has to be a string in JSON.

The key must be stored in UTF-8 format, so any translated strings or strings provided by the user as a key must be converted to UTF-8 at the site where they are placed in the underlying map that this PARAM_MAP points to.

Values must also be in UTF-8, but if you use wxString as the value type, this conversion will be done automatically by the to_json and from_json helpers defined in json_settings.cpp

Template Parameters
Valueis the value type of the map

Definition at line 511 of file parameters.h.

Constructor & Destructor Documentation

◆ PARAM_MAP()

template<typename Value >
PARAM_MAP< Value >::PARAM_MAP ( const std::string &  aJsonPath,
std::map< std::string, Value > *  aPtr,
std::initializer_list< std::pair< const std::string, Value >>  aDefault,
bool  aReadOnly = false 
)
inline

Definition at line 514 of file parameters.h.

516  :
517  PARAM_BASE( aJsonPath, aReadOnly ),
518  m_ptr( aPtr ),
519  m_default( aDefault )
520  { }
std::map< std::string, Value > m_default
Definition: parameters.h:536
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
std::map< std::string, Value > * m_ptr
Definition: parameters.h:534

Member Function Documentation

◆ GetJsonPath()

const std::string& PARAM_BASE::GetJsonPath ( ) const
inlineinherited
Returns
the path name of the parameter used to store it in the json file mainly useful in error messages

Definition at line 68 of file parameters.h.

68 { return m_path; }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74

References PARAM_BASE::m_path.

◆ Load()

template<typename Value >
void PARAM_MAP< Value >::Load ( JSON_SETTINGS aSettings,
bool  aResetIfMissing = true 
) const
overridevirtual

Loads the value of this parameter from JSON to the underlying storage.

Parameters
aSettingsis the JSON_SETTINGS object to load from.
aResetIfMissingif true will set the parameter to its default value if load fails

Implements PARAM_BASE.

Definition at line 172 of file parameters.cpp.

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 }
std::map< std::string, Value > m_default
Definition: parameters.h:536
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
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< std::string, Value > * m_ptr
Definition: parameters.h:534
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:77
boost::optional< T > OPT
Definition: optional.h:7

References JSON_SETTINGS::GetJson().

◆ MatchesFile()

template<typename Value >
bool PARAM_MAP< Value >::MatchesFile ( JSON_SETTINGS aSettings) const
overridevirtual

Checks whether the parameter in memory matches the one in a given JSON file.

Parameters
aSettingsis a JSON_SETTINGS to check the JSON file contents of
Returns
true if the parameter in memory matches its value in the file

Implements PARAM_BASE.

Definition at line 205 of file parameters.cpp.

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 }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
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< std::string, Value > * m_ptr
Definition: parameters.h:534
boost::optional< T > OPT
Definition: optional.h:7

References JSON_SETTINGS::GetJson().

◆ SetDefault()

template<typename Value >
virtual void PARAM_MAP< Value >::SetDefault ( )
inlineoverridevirtual

Implements PARAM_BASE.

Definition at line 526 of file parameters.h.

527  {
528  *m_ptr = m_default;
529  }
std::map< std::string, Value > m_default
Definition: parameters.h:536
std::map< std::string, Value > * m_ptr
Definition: parameters.h:534

References PARAM_MAP< Value >::m_default, and PARAM_MAP< Value >::m_ptr.

◆ Store()

template<typename Value >
void PARAM_MAP< Value >::Store ( JSON_SETTINGS aSettings) const
overridevirtual

Stores the value of this parameter to the given JSON_SETTINGS object.

Parameters
aSettingsis the JSON_SETTINGS object to store into.

Implements PARAM_BASE.

Definition at line 193 of file parameters.cpp.

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 }
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::string m_path
the string used to store the param in json files
Definition: parameters.h:74
nlohmann::json json
Definition: gerbview.cpp:41
std::map< std::string, Value > * m_ptr
Definition: parameters.h:534

References JSON_SETTINGS::Set().

Member Data Documentation

◆ m_default

template<typename Value >
std::map<std::string, Value> PARAM_MAP< Value >::m_default
private

Definition at line 536 of file parameters.h.

Referenced by PARAM_MAP< Value >::SetDefault().

◆ m_path

◆ m_ptr

template<typename Value >
std::map<std::string, Value>* PARAM_MAP< Value >::m_ptr
private

Definition at line 534 of file parameters.h.

Referenced by PARAM_MAP< Value >::SetDefault().

◆ m_readOnly

bool PARAM_BASE::m_readOnly
protectedinherited

The documentation for this class was generated from the following files: