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

285  :
286  PARAM_BASE( aJsonPath, aReadOnly ),
287  m_default( aDefault ),
288  m_getter( aGetter ),
289  m_setter( aSetter )
290  { }
std::function< ValueType()> m_getter
Definition: parameters.h:320
std::function< void(ValueType)> m_setter
Definition: parameters.h:322
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_default
Definition: parameters.h:318

Member Function Documentation

◆ GetDefault()

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

Definition at line 305 of file parameters.h.

306  {
307  return m_default;
308  }
ValueType m_default
Definition: parameters.h:318

◆ 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 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: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::function< void(ValueType)> m_setter
Definition: parameters.h:322
ValueType m_default
Definition: parameters.h:318
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:77
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: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::function< ValueType()> m_getter
Definition: parameters.h:320
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 310 of file parameters.h.

311  {
312  m_setter( m_default );
313  }
std::function< void(ValueType)> m_setter
Definition: parameters.h:322
ValueType m_default
Definition: parameters.h:318

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

295  {
296  try
297  {
298  aSettings->Set<ValueType>( m_path, m_getter() );
299  }
300  catch( ... )
301  {
302  }
303  }
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
std::function< ValueType()> m_getter
Definition: parameters.h:320

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

Definition at line 320 of file parameters.h.

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

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

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


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