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

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

323  :
324  PARAM_BASE( aJsonPath, aReadOnly ),
325  m_ptr( aPtr ),
326  m_default( aDefault ),
327  m_min(),
328  m_max(),
329  m_use_minmax( false ),
330  m_scale( aScale )
331  { }
ValueType m_default
Definition: parameters.h:397
double m_scale
Definition: parameters.h:401
bool m_use_minmax
Definition: parameters.h:400
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_min
Definition: parameters.h:398
ValueType m_max
Definition: parameters.h:399
ValueType * m_ptr
Definition: parameters.h:396

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

334  :
335  PARAM_BASE( aJsonPath, aReadOnly ),
336  m_ptr( aPtr ),
337  m_default( aDefault ),
338  m_min( aMin ),
339  m_max( aMax ),
340  m_use_minmax( true ),
341  m_scale( aScale )
342  { }
ValueType m_default
Definition: parameters.h:397
double m_scale
Definition: parameters.h:401
bool m_use_minmax
Definition: parameters.h:400
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35
ValueType m_min
Definition: parameters.h:398
ValueType m_max
Definition: parameters.h:399
ValueType * m_ptr
Definition: parameters.h:396

Member Function Documentation

◆ GetDefault()

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

Definition at line 372 of file parameters.h.

373  {
374  return m_default;
375  }
ValueType m_default
Definition: parameters.h:397

References PARAM_SCALED< 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_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 382 of file parameters.h.

383  {
384  return *m_ptr == m_default;
385  }
ValueType m_default
Definition: parameters.h:397
ValueType * m_ptr
Definition: parameters.h:396

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

345  {
346  if( m_readOnly )
347  return;
348 
349  double dval = m_default * m_scale;
350 
351  if( OPT<double> optval = aSettings->Get<double>( m_path ) )
352  dval = *optval;
353  else if( !aResetIfMissing )
354  return;
355 
356  ValueType val = KiROUND<ValueType>( dval / m_scale );
357 
358  if( m_use_minmax )
359  {
360  if( val > m_max || val < m_min )
361  val = m_default;
362  }
363 
364  *m_ptr = val;
365  }
ValueType m_default
Definition: parameters.h:397
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
double m_scale
Definition: parameters.h:401
OPT< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
bool m_use_minmax
Definition: parameters.h:400
ValueType m_min
Definition: parameters.h:398
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:399
ValueType * m_ptr
Definition: parameters.h:396

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

388  {
389  if( OPT<double> optval = aSettings->Get<double>( m_path ) )
390  return *optval == ( *m_ptr * m_scale );
391 
392  return false;
393  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
double m_scale
Definition: parameters.h:401
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:396

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

378  {
379  *m_ptr = m_default;
380  }
ValueType m_default
Definition: parameters.h:397
ValueType * m_ptr
Definition: parameters.h:396

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

368  {
369  aSettings->Set<double>( m_path, *m_ptr * m_scale );
370  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
double m_scale
Definition: parameters.h:401
ValueType * m_ptr
Definition: parameters.h:396
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 399 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 398 of file parameters.h.

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

◆ m_path

◆ m_ptr

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

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


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