KiCad PCB EDA Suite
PARAM_LAMBDA< ValueType > Class Template Reference

Like a normal param, but with custom getter and setter functions. More...

#include <parameters.h>

Inheritance diagram for PARAM_LAMBDA< ValueType >:
PARAM_BASE

Public Member Functions

 PARAM_LAMBDA (const std::string &aJsonPath, std::function< ValueType()> aGetter, std::function< void(ValueType)> aSetter, ValueType 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...
 
ValueType GetDefault () const
 
void SetDefault () override
 
bool IsDefault () const override
 Checks whether or not this param has been changed from its default value. More...
 
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

ValueType m_default
 
std::function< ValueType()> m_getter
 
std::function< void(ValueType)> m_setter
 

Detailed Description

template<typename ValueType>
class PARAM_LAMBDA< ValueType >

Like a normal param, but with custom getter and setter functions.

Template Parameters
ValueTypeis the value to store

Definition at line 296 of file parameters.h.

Constructor & Destructor Documentation

◆ PARAM_LAMBDA()

template<typename ValueType>
PARAM_LAMBDA< ValueType >::PARAM_LAMBDA ( const std::string &  aJsonPath,
std::function< ValueType()>  aGetter,
std::function< void(ValueType)>  aSetter,
ValueType  aDefault,
bool  aReadOnly = false 
)
inline

Definition at line 299 of file parameters.h.

301  :
302  PARAM_BASE( aJsonPath, aReadOnly ),
303  m_default( aDefault ),
304  m_getter( aGetter ),
305  m_setter( aSetter )
306  { }
std::function< ValueType()> m_getter
Definition: parameters.h:341
std::function< void(ValueType)> m_setter
Definition: parameters.h:343
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_default
Definition: parameters.h:339

Member Function Documentation

◆ GetDefault()

template<typename ValueType>
ValueType PARAM_LAMBDA< ValueType >::GetDefault ( ) const
inline

Definition at line 321 of file parameters.h.

322  {
323  return m_default;
324  }
ValueType m_default
Definition: parameters.h:339

◆ 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 74 of file parameters.h.

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

References PARAM_BASE::m_path.

◆ IsDefault()

template<typename ValueType>
bool PARAM_LAMBDA< ValueType >::IsDefault ( ) const
inlineoverridevirtual

Checks whether or not this param has been changed from its default value.

Returns
true if the parameter in memory matches its default value

Implements PARAM_BASE.

Definition at line 331 of file parameters.h.

332  {
333  return m_getter() == m_default;
334  }
std::function< ValueType()> m_getter
Definition: parameters.h:341
ValueType m_default
Definition: parameters.h:339

◆ Load()

template<typename ValueType >
void PARAM_LAMBDA< ValueType >::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 31 of file parameters.cpp.

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
42  }
43  else
44  {
45  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
46  m_setter( *optval );
47  else
49  }
50 }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
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::function< void(ValueType)> m_setter
Definition: parameters.h:343
ValueType m_default
Definition: parameters.h:339
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:83
boost::optional< T > OPT
Definition: optional.h:7

◆ MatchesFile()

template<typename ValueType >
bool PARAM_LAMBDA< ValueType >::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 54 of file parameters.cpp.

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 }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
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::function< ValueType()> m_getter
Definition: parameters.h:341
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
boost::optional< T > OPT
Definition: optional.h:7

◆ SetDefault()

template<typename ValueType>
void PARAM_LAMBDA< ValueType >::SetDefault ( )
inlineoverridevirtual

Implements PARAM_BASE.

Definition at line 326 of file parameters.h.

327  {
328  m_setter( m_default );
329  }
std::function< void(ValueType)> m_setter
Definition: parameters.h:343
ValueType m_default
Definition: parameters.h:339

◆ Store()

template<typename ValueType>
void PARAM_LAMBDA< ValueType >::Store ( JSON_SETTINGS aSettings) const
inlineoverridevirtual

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 310 of file parameters.h.

311  {
312  try
313  {
314  aSettings->Set<ValueType>( m_path, m_getter() );
315  }
316  catch( ... )
317  {
318  }
319  }
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:80
std::function< ValueType()> m_getter
Definition: parameters.h:341

Member Data Documentation

◆ m_default

template<typename ValueType>
ValueType PARAM_LAMBDA< ValueType >::m_default
private

◆ m_getter

template<typename ValueType>
std::function<ValueType()> PARAM_LAMBDA< ValueType >::m_getter
private

◆ m_path

◆ m_readOnly

bool PARAM_BASE::m_readOnly
protectedinherited

◆ m_setter

template<typename ValueType>
std::function<void( ValueType )> PARAM_LAMBDA< ValueType >::m_setter
private

Definition at line 343 of file parameters.h.

Referenced by PARAM_LAMBDA< nlohmann::json >::SetDefault().


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