KiCad PCB EDA Suite
PARAM_WXSTRING_MAP Class Reference

A helper for <wxString, wxString> maps. More...

#include <parameters.h>

Inheritance diagram for PARAM_WXSTRING_MAP:
PARAM_BASE

Public Member Functions

 PARAM_WXSTRING_MAP (const std::string &aJsonPath, std::map< wxString, wxString > *aPtr, std::initializer_list< std::pair< const wxString, wxString >> 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...
 
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

std::map< wxString, wxString > * m_ptr
 
std::map< wxString, wxString > m_default
 

Detailed Description

A helper for <wxString, wxString> maps.

Definition at line 724 of file parameters.h.

Constructor & Destructor Documentation

◆ PARAM_WXSTRING_MAP()

PARAM_WXSTRING_MAP::PARAM_WXSTRING_MAP ( const std::string &  aJsonPath,
std::map< wxString, wxString > *  aPtr,
std::initializer_list< std::pair< const wxString, wxString >>  aDefault,
bool  aReadOnly = false 
)
inline

Definition at line 727 of file parameters.h.

729  :
730  PARAM_BASE( aJsonPath, aReadOnly ),
731  m_ptr( aPtr ),
732  m_default( aDefault )
733  { }
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:805
std::map< wxString, wxString > m_default
Definition: parameters.h:807
PARAM_BASE(std::string aJsonPath, bool aReadOnly)
Definition: parameters.h:35

Member Function Documentation

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

bool PARAM_WXSTRING_MAP::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 775 of file parameters.h.

776  {
777  return *m_ptr == m_default;
778  }
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:805
std::map< wxString, wxString > m_default
Definition: parameters.h:807

References m_default, and m_ptr.

◆ Load()

void PARAM_WXSTRING_MAP::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 735 of file parameters.h.

736  {
737  if( m_readOnly )
738  return;
739 
740  if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
741  {
742  if( js->is_object() )
743  {
744  m_ptr->clear();
745 
746  for( const auto& el : js->items() )
747  {
748  ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] =
749  el.value().get<wxString>();
750  }
751  }
752  }
753  else if( aResetIfMissing )
754  *m_ptr = m_default;
755  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:805
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::map< wxString, wxString > m_default
Definition: parameters.h:807
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::GetJson(), m_default, PARAM_BASE::m_path, m_ptr, and PARAM_BASE::m_readOnly.

◆ MatchesFile()

bool PARAM_WXSTRING_MAP::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 780 of file parameters.h.

781  {
782  if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
783  {
784  if( js->is_object() )
785  {
786  if( m_ptr->size() != js->size() )
787  return false;
788 
789  std::map<wxString, wxString> val;
790 
791  for( const auto& el : js->items() )
792  {
793  wxString key( el.key().c_str(), wxConvUTF8 );
794  val[key] = el.value().get<wxString>();
795  }
796 
797  return val == *m_ptr;
798  }
799  }
800 
801  return false;
802  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:805
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....
boost::optional< T > OPT
Definition: optional.h:7

References JSON_SETTINGS::GetJson(), PARAM_BASE::m_path, and m_ptr.

◆ SetDefault()

virtual void PARAM_WXSTRING_MAP::SetDefault ( )
inlineoverridevirtual

Implements PARAM_BASE.

Definition at line 770 of file parameters.h.

771  {
772  *m_ptr = m_default;
773  }
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:805
std::map< wxString, wxString > m_default
Definition: parameters.h:807

References m_default, and m_ptr.

◆ Store()

void PARAM_WXSTRING_MAP::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 757 of file parameters.h.

758  {
759  nlohmann::json js( {} );
760 
761  for( const auto& el : *m_ptr )
762  {
763  std::string key( el.first.ToUTF8() );
764  js[ key ] = el.second;
765  }
766 
767  aSettings->Set<nlohmann::json>( m_path, js );
768  }
std::string m_path
the string used to store the param in json files
Definition: parameters.h:80
std::map< wxString, wxString > * m_ptr
Definition: parameters.h:805
nlohmann::json json
Definition: gerbview.cpp:39
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, m_ptr, and JSON_SETTINGS::Set().

Member Data Documentation

◆ m_default

std::map<wxString, wxString> PARAM_WXSTRING_MAP::m_default
private

Definition at line 807 of file parameters.h.

Referenced by IsDefault(), Load(), and SetDefault().

◆ m_path

◆ m_ptr

std::map<wxString, wxString>* PARAM_WXSTRING_MAP::m_ptr
private

Definition at line 805 of file parameters.h.

Referenced by IsDefault(), Load(), MatchesFile(), SetDefault(), and Store().

◆ m_readOnly


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