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

232  :
233  PARAM_BASE( aJsonPath, aReadOnly ),
234  m_default( aDefault ),
235  m_getter( aGetter ),
236  m_setter( aSetter )
237  { }
std::function< ValueType()> m_getter
Definition: parameters.h:306
std::function< void(ValueType)> m_setter
Definition: parameters.h:308
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_default
Definition: parameters.h:304

Member Function Documentation

◆ GetDefault()

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

Definition at line 271 of file parameters.h.

272  {
273  return m_default;
274  }
ValueType m_default
Definition: parameters.h:304

References PARAM_LAMBDA< ValueType >::m_default.

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

282  {
283  return m_getter() == m_default;
284  }
std::function< ValueType()> m_getter
Definition: parameters.h:306
ValueType m_default
Definition: parameters.h:304

References PARAM_LAMBDA< ValueType >::m_default, and PARAM_LAMBDA< ValueType >::m_getter.

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

240  {
241  if( m_readOnly )
242  return;
243 
244  if( std::is_same<ValueType, nlohmann::json>::value )
245  {
246  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
247  m_setter( *optval );
248  else
249  m_setter( m_default );
250  }
251  else
252  {
253  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
254  m_setter( *optval );
255  else
256  m_setter( m_default );
257  }
258  }
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:308
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
ValueType m_default
Definition: parameters.h:304
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:83
boost::optional< T > OPT
Definition: optional.h:7

References JSON_SETTINGS::Get(), JSON_SETTINGS::GetJson(), PARAM_LAMBDA< ValueType >::m_default, PARAM_BASE::m_path, PARAM_BASE::m_readOnly, and PARAM_LAMBDA< ValueType >::m_setter.

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

287  {
288  if( std::is_same<ValueType, nlohmann::json>::value )
289  {
290  if( OPT<nlohmann::json> optval = aSettings->GetJson( m_path ) )
291  return *optval == m_default;
292  }
293  else
294  {
295  if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
296  return *optval == m_default;
297  }
298 
299  // Not in file
300  return false;
301  }
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:304
boost::optional< T > OPT
Definition: optional.h:7

References JSON_SETTINGS::Get(), JSON_SETTINGS::GetJson(), PARAM_LAMBDA< ValueType >::m_default, and PARAM_BASE::m_path.

◆ SetDefault()

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

Implements PARAM_BASE.

Definition at line 276 of file parameters.h.

277  {
278  m_setter( m_default );
279  }
std::function< void(ValueType)> m_setter
Definition: parameters.h:308
ValueType m_default
Definition: parameters.h:304

References PARAM_LAMBDA< ValueType >::m_default, and PARAM_LAMBDA< ValueType >::m_setter.

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

261  {
262  try
263  {
264  aSettings->Set<ValueType>( m_path, m_getter() );
265  }
266  catch( ... )
267  {
268  }
269  }
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:306
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...

References PARAM_LAMBDA< ValueType >::m_getter, PARAM_BASE::m_path, and JSON_SETTINGS::Set().

Member Data Documentation

◆ m_default

◆ 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

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