KiCad PCB EDA Suite
PARAM_SCALED< ValueType > Class Template Reference

Represents a parameter that has a scaling factor between the value in the file and the value used internally (i.e. More...

#include <parameters.h>

Inheritance diagram for PARAM_SCALED< ValueType >:
PARAM_BASE

Public Member Functions

 PARAM_SCALED (const std::string &aJsonPath, ValueType *aPtr, ValueType aDefault, double aScale=1.0, bool aReadOnly=false)
 
 PARAM_SCALED (const std::string &aJsonPath, ValueType *aPtr, ValueType aDefault, ValueType aMin, ValueType aMax, double aScale=1.0, 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
 
virtual 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_ptr
 
ValueType m_default
 
ValueType m_min
 
ValueType m_max
 
bool m_use_minmax
 
double m_scale
 

Detailed Description

template<typename ValueType>
class PARAM_SCALED< ValueType >

Represents a parameter that has a scaling factor between the value in the file and the value used internally (i.e.

the value pointer). This basically only makes sense to use with int or double as ValueType.

Template Parameters
ValueTypeis the internal type: the file always stores a double.

Definition at line 388 of file parameters.h.

Constructor & Destructor Documentation

◆ PARAM_SCALED() [1/2]

template<typename ValueType >
PARAM_SCALED< ValueType >::PARAM_SCALED ( const std::string &  aJsonPath,
ValueType *  aPtr,
ValueType  aDefault,
double  aScale = 1.0,
bool  aReadOnly = false 
)
inline

Definition at line 391 of file parameters.h.

392  :
393  PARAM_BASE( aJsonPath, aReadOnly ),
394  m_ptr( aPtr ),
395  m_default( aDefault ),
396  m_min(),
397  m_max(),
398  m_use_minmax( false ),
399  m_scale( aScale )
400  { }
ValueType m_default
Definition: parameters.h:466
double m_scale
Definition: parameters.h:470
bool m_use_minmax
Definition: parameters.h:469
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_min
Definition: parameters.h:467
ValueType m_max
Definition: parameters.h:468
ValueType * m_ptr
Definition: parameters.h:465

◆ PARAM_SCALED() [2/2]

template<typename ValueType >
PARAM_SCALED< ValueType >::PARAM_SCALED ( const std::string &  aJsonPath,
ValueType *  aPtr,
ValueType  aDefault,
ValueType  aMin,
ValueType  aMax,
double  aScale = 1.0,
bool  aReadOnly = false 
)
inline

Definition at line 402 of file parameters.h.

403  :
404  PARAM_BASE( aJsonPath, aReadOnly ),
405  m_ptr( aPtr ),
406  m_default( aDefault ),
407  m_min( aMin ),
408  m_max( aMax ),
409  m_use_minmax( true ),
410  m_scale( aScale )
411  { }
ValueType m_default
Definition: parameters.h:466
double m_scale
Definition: parameters.h:470
bool m_use_minmax
Definition: parameters.h:469
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_min
Definition: parameters.h:467
ValueType m_max
Definition: parameters.h:468
ValueType * m_ptr
Definition: parameters.h:465

Member Function Documentation

◆ GetDefault()

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

Definition at line 441 of file parameters.h.

442  {
443  return m_default;
444  }
ValueType m_default
Definition: parameters.h:466

References PARAM_SCALED< ValueType >::m_default.

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

452  {
453  return *m_ptr == m_default;
454  }
ValueType m_default
Definition: parameters.h:466
ValueType * m_ptr
Definition: parameters.h:465

References PARAM_SCALED< ValueType >::m_default, and PARAM_SCALED< ValueType >::m_ptr.

◆ Load()

template<typename ValueType >
void PARAM_SCALED< 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 413 of file parameters.h.

414  {
415  if( m_readOnly )
416  return;
417 
418  double dval = m_default * m_scale;
419 
420  if( OPT<double> optval = aSettings->Get<double>( m_path ) )
421  dval = *optval;
422  else if( !aResetIfMissing )
423  return;
424 
425  ValueType val = KiROUND<ValueType>( dval / m_scale );
426 
427  if( m_use_minmax )
428  {
429  if( val > m_max || val < m_min )
430  val = m_default;
431  }
432 
433  *m_ptr = val;
434  }
ValueType m_default
Definition: parameters.h:466
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
double m_scale
Definition: parameters.h:470
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
bool m_use_minmax
Definition: parameters.h:469
ValueType m_min
Definition: parameters.h:467
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:83
boost::optional< T > OPT
Definition: optional.h:7
ValueType m_max
Definition: parameters.h:468
ValueType * m_ptr
Definition: parameters.h:465

References JSON_SETTINGS::Get(), PARAM_SCALED< ValueType >::m_default, PARAM_SCALED< ValueType >::m_max, PARAM_SCALED< ValueType >::m_min, PARAM_BASE::m_path, PARAM_SCALED< ValueType >::m_ptr, PARAM_BASE::m_readOnly, PARAM_SCALED< ValueType >::m_scale, and PARAM_SCALED< ValueType >::m_use_minmax.

◆ MatchesFile()

template<typename ValueType >
bool PARAM_SCALED< 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 456 of file parameters.h.

457  {
458  if( OPT<double> optval = aSettings->Get<double>( m_path ) )
459  return *optval == ( *m_ptr * m_scale );
460 
461  return false;
462  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
double m_scale
Definition: parameters.h:470
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
boost::optional< T > OPT
Definition: optional.h:7
ValueType * m_ptr
Definition: parameters.h:465

References JSON_SETTINGS::Get(), PARAM_BASE::m_path, PARAM_SCALED< ValueType >::m_ptr, and PARAM_SCALED< ValueType >::m_scale.

◆ SetDefault()

template<typename ValueType >
virtual void PARAM_SCALED< ValueType >::SetDefault ( )
inlineoverridevirtual

Implements PARAM_BASE.

Definition at line 446 of file parameters.h.

447  {
448  *m_ptr = m_default;
449  }
ValueType m_default
Definition: parameters.h:466
ValueType * m_ptr
Definition: parameters.h:465

References PARAM_SCALED< ValueType >::m_default, and PARAM_SCALED< ValueType >::m_ptr.

◆ Store()

template<typename ValueType >
void PARAM_SCALED< 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 436 of file parameters.h.

437  {
438  aSettings->Set<double>( m_path, *m_ptr * m_scale );
439  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
double m_scale
Definition: parameters.h:470
ValueType * m_ptr
Definition: parameters.h:465
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_BASE::m_path, PARAM_SCALED< ValueType >::m_ptr, PARAM_SCALED< ValueType >::m_scale, and JSON_SETTINGS::Set().

Member Data Documentation

◆ m_default

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

◆ m_max

template<typename ValueType >
ValueType PARAM_SCALED< ValueType >::m_max
private

Definition at line 468 of file parameters.h.

Referenced by PARAM_SCALED< ValueType >::Load().

◆ m_min

template<typename ValueType >
ValueType PARAM_SCALED< ValueType >::m_min
private

Definition at line 467 of file parameters.h.

Referenced by PARAM_SCALED< ValueType >::Load().

◆ m_path

◆ m_ptr

◆ m_readOnly

◆ m_scale

template<typename ValueType >
double PARAM_SCALED< ValueType >::m_scale
private

◆ m_use_minmax

template<typename ValueType >
bool PARAM_SCALED< ValueType >::m_use_minmax
private

Definition at line 469 of file parameters.h.

Referenced by PARAM_SCALED< ValueType >::Load().


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