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 ()
 

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:375
std::function< void(ValueType)> m_setter
Definition: parameters.h:377
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_default
Definition: parameters.h:373

Member Function Documentation

◆ GetDefault()

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

Definition at line 340 of file parameters.h.

341  {
342  return m_default;
343  }
ValueType m_default
Definition: parameters.h:373

◆ GetJsonPath()

const std::string& PARAM_BASE::GetJsonPath ( )
inlineinherited
Returns
the path name of the parameter used to store it in the json file mainly usefull 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 350 of file parameters.h.

351  {
352  return m_getter() == m_default;
353  }
std::function< ValueType()> m_getter
Definition: parameters.h:375
ValueType m_default
Definition: parameters.h:373

◆ Load()

template<typename ValueType>
void PARAM_LAMBDA< ValueType >::Load ( JSON_SETTINGS aSettings,
bool  aResetIfMissing = true 
) const
inlineoverridevirtual

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

309  {
310  if( m_readOnly )
311  return;
312 
313  if( std::is_same<ValueType, nlohmann::json>::value )
314  {
315  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
316  m_setter( *optval );
317  else
318  m_setter( m_default );
319  }
320  else
321  {
322  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
323  m_setter( *optval );
324  else
325  m_setter( m_default );
326  }
327  }
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:377
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
ValueType m_default
Definition: parameters.h:373
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
inlineoverridevirtual

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

356  {
357  if( std::is_same<ValueType, nlohmann::json>::value )
358  {
359  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
360  return *optval == m_default;
361  }
362  else
363  {
364  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
365  return *optval == m_default;
366  }
367 
368  // Not in file
369  return false;
370  }
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....
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
ValueType m_default
Definition: parameters.h:373
boost::optional< T > OPT
Definition: optional.h:7

◆ SetDefault()

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

Implements PARAM_BASE.

Definition at line 345 of file parameters.h.

346  {
347  m_setter( m_default );
348  }
std::function< void(ValueType)> m_setter
Definition: parameters.h:377
ValueType m_default
Definition: parameters.h:373

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

330  {
331  try
332  {
333  aSettings->Set<ValueType>( m_path, m_getter() );
334  }
335  catch( ... )
336  {
337  }
338  }
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:375
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...

Member Data Documentation

◆ m_default

◆ m_getter

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

◆ m_path

◆ m_readOnly

◆ m_setter

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

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