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

337  :
338  PARAM_BASE( aJsonPath, aReadOnly ),
339  m_ptr( aPtr ),
340  m_default( aDefault ),
341  m_min(),
342  m_max(),
343  m_use_minmax( false ),
344  m_scale( aScale )
345  { }
ValueType m_default
Definition: parameters.h:406
double m_scale
Definition: parameters.h:410
bool m_use_minmax
Definition: parameters.h:409
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_min
Definition: parameters.h:407
ValueType m_max
Definition: parameters.h:408
ValueType * m_ptr
Definition: parameters.h:405

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

348  :
349  PARAM_BASE( aJsonPath, aReadOnly ),
350  m_ptr( aPtr ),
351  m_default( aDefault ),
352  m_min( aMin ),
353  m_max( aMax ),
354  m_use_minmax( true ),
355  m_scale( aScale )
356  { }
ValueType m_default
Definition: parameters.h:406
double m_scale
Definition: parameters.h:410
bool m_use_minmax
Definition: parameters.h:409
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_min
Definition: parameters.h:407
ValueType m_max
Definition: parameters.h:408
ValueType * m_ptr
Definition: parameters.h:405

Member Function Documentation

◆ GetDefault()

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

Definition at line 386 of file parameters.h.

387  {
388  return m_default;
389  }
ValueType m_default
Definition: parameters.h:406

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

359  {
360  if( m_readOnly )
361  return;
362 
363  double dval = m_default * m_scale;
364 
365  if( OPT<double> optval = aSettings->Get<double>( m_path ) )
366  dval = *optval;
367  else if( !aResetIfMissing )
368  return;
369 
370  ValueType val = KiROUND<ValueType>( dval / m_scale );
371 
372  if( m_use_minmax )
373  {
374  if( val > m_max || val < m_min )
375  val = m_default;
376  }
377 
378  *m_ptr = val;
379  }
ValueType m_default
Definition: parameters.h:406
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
double m_scale
Definition: parameters.h:410
bool m_use_minmax
Definition: parameters.h:409
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
ValueType m_min
Definition: parameters.h:407
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:77
boost::optional< T > OPT
Definition: optional.h:7
ValueType m_max
Definition: parameters.h:408
ValueType * m_ptr
Definition: parameters.h:405

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

397  {
398  if( OPT<double> optval = aSettings->Get<double>( m_path ) )
399  return *optval == ( *m_ptr * m_scale );
400 
401  return false;
402  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:74
double m_scale
Definition: parameters.h:410
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:405

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

392  {
393  *m_ptr = m_default;
394  }
ValueType m_default
Definition: parameters.h:406
ValueType * m_ptr
Definition: parameters.h:405

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

382  {
383  aSettings->Set<double>( m_path, *m_ptr * m_scale );
384  }
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
double m_scale
Definition: parameters.h:410
ValueType * m_ptr
Definition: parameters.h:405

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

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

◆ m_path

◆ m_ptr

template<typename ValueType >
ValueType* PARAM_SCALED< ValueType >::m_ptr
private

◆ m_readOnly

bool PARAM_BASE::m_readOnly
protectedinherited

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

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


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