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

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

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

349 :
350 PARAM_BASE( aJsonPath, aReadOnly ),
351 m_ptr( aPtr ),
352 m_default( aDefault ),
353 m_min( aMin ),
354 m_max( aMax ),
355 m_use_minmax( true ),
356 m_scale( aScale )
357 { }

Member Function Documentation

◆ GetDefault()

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

Definition at line 387 of file parameters.h.

388 {
389 return m_default;
390 }

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

69{ return m_path; }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:75

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

360 {
361 if( m_readOnly )
362 return;
363
364 double dval = m_default * m_scale;
365
366 if( std::optional<double> optval = aSettings->Get<double>( m_path ) )
367 dval = *optval;
368 else if( !aResetIfMissing )
369 return;
370
371 ValueType val = KiROUND<ValueType>( dval / m_scale );
372
373 if( m_use_minmax )
374 {
375 if( val > m_max || val < m_min )
376 val = m_default;
377 }
378
379 *m_ptr = val;
380 }
std::optional< ValueType > Get(const std::string &aPath) const
Fetches a value from within the JSON document.
bool m_readOnly
! True if the parameter pointer should never be overwritten
Definition: parameters.h:78

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

398 {
399 if( std::optional<double> optval = aSettings->Get<double>( m_path ) )
400 return *optval == ( *m_ptr * m_scale );
401
402 return false;
403 }

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

393 {
394 *m_ptr = m_default;
395 }

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

383 {
384 aSettings->Set<double>( m_path, *m_ptr * m_scale );
385 }
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 409 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 408 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 410 of file parameters.h.

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


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