Remove unnecessary exception handling in settings
Use optionals instead where a value can be absent Fixes #4024
This commit is contained in:
parent
37befc5a42
commit
8cb8d55843
|
@ -300,20 +300,16 @@ bool COLOR_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
|
|||
|
||||
COLOR4D COLOR_SETTINGS::GetColor( int aLayer ) const
|
||||
{
|
||||
try
|
||||
if( m_color_context == COLOR_CONTEXT::FOOTPRINT && aLayer >= PCBNEW_LAYER_ID_START
|
||||
&& aLayer <= GAL_LAYER_ID_END )
|
||||
{
|
||||
if( m_color_context == COLOR_CONTEXT::FOOTPRINT && aLayer >= PCBNEW_LAYER_ID_START
|
||||
&& aLayer <= GAL_LAYER_ID_END )
|
||||
{
|
||||
aLayer += FPEDIT_LAYER_ID_START;
|
||||
}
|
||||
aLayer += FPEDIT_LAYER_ID_START;
|
||||
}
|
||||
|
||||
if( m_colors.count( aLayer ) )
|
||||
return m_colors.at( aLayer );
|
||||
}
|
||||
catch( std::out_of_range& )
|
||||
{
|
||||
return COLOR4D::UNSPECIFIED;
|
||||
}
|
||||
|
||||
return COLOR4D::UNSPECIFIED;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -237,20 +237,22 @@ void JSON_SETTINGS::SaveToFile( const std::string& aDirectory )
|
|||
}
|
||||
|
||||
|
||||
nlohmann::json JSON_SETTINGS::GetJson( std::string aPath ) const
|
||||
OPT<nlohmann::json> JSON_SETTINGS::GetJson( std::string aPath ) const
|
||||
{
|
||||
nlohmann::json ret( {} );
|
||||
nlohmann::json::json_pointer ptr = PointerFromString( std::move( aPath ) );
|
||||
|
||||
// Will throw an exception if the path is not found
|
||||
try
|
||||
{
|
||||
ret = this->at( PointerFromString( std::move( aPath ) ) );
|
||||
}
|
||||
catch( ... )
|
||||
if( this->contains( ptr ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
return OPT<nlohmann::json>{ this->at( ptr ) };
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return OPT<nlohmann::json>{};
|
||||
}
|
||||
|
||||
|
||||
|
@ -403,9 +405,12 @@ void JSON_SETTINGS::ReleaseNestedSettings( NESTED_SETTINGS* aSettings )
|
|||
|
||||
// Specializations to allow conversion between wxString and std::string via JSON_SETTINGS API
|
||||
|
||||
template<> wxString JSON_SETTINGS::Get( std::string aPath ) const
|
||||
template<> OPT<wxString> JSON_SETTINGS::Get( std::string aPath ) const
|
||||
{
|
||||
return wxString( GetJson( std::move( aPath ) ).get<std::string>().c_str(), wxConvUTF8 );
|
||||
if( OPT<nlohmann::json> opt_json = GetJson( std::move( aPath ) ) )
|
||||
return wxString( opt_json->get<std::string>().c_str(), wxConvUTF8 );
|
||||
|
||||
return NULLOPT;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -129,12 +129,8 @@ public:
|
|||
|
||||
COLOR4D val = m_default;
|
||||
|
||||
try
|
||||
{
|
||||
val = aSettings->Get<COLOR4D>( m_path );
|
||||
}
|
||||
catch( ... )
|
||||
{}
|
||||
if( OPT<COLOR4D> optval = aSettings->Get<COLOR4D>( m_path ) )
|
||||
val = *optval;
|
||||
|
||||
( *m_map )[ m_key ] = val;
|
||||
}
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
#include <utility>
|
||||
#include <wx/string.h>
|
||||
|
||||
#include <core/optional.h>
|
||||
|
||||
class wxConfigBase;
|
||||
class NESTED_SETTINGS;
|
||||
class PARAM_BASE;
|
||||
|
@ -90,19 +92,22 @@ public:
|
|||
* @param aPath is a string containing one or more keys separated by '.'
|
||||
* @return a JSON object from within this one
|
||||
*/
|
||||
nlohmann::json GetJson( std::string aPath ) const;
|
||||
OPT<nlohmann::json> GetJson( std::string aPath ) const;
|
||||
|
||||
/**
|
||||
* Fetches a value from within the JSON document.
|
||||
* Will throw an exception if the value is not found or a mismatching type.
|
||||
* Will return an empty optional if the value is not found or a mismatching type.
|
||||
* @tparam ValueType is the type to cast to
|
||||
* @param aPath is the path within the document to retrieve
|
||||
* @return a value from within this document
|
||||
*/
|
||||
template<typename ValueType>
|
||||
ValueType Get( std::string aPath ) const
|
||||
OPT<ValueType> Get( std::string aPath ) const
|
||||
{
|
||||
return GetJson( std::move( aPath ) ).get<ValueType>();
|
||||
if( OPT<nlohmann::json> ret = GetJson( std::move( aPath ) ) )
|
||||
return ret->get<ValueType>();
|
||||
|
||||
return NULLOPT;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -220,7 +225,7 @@ protected:
|
|||
|
||||
// Specializations to allow conversion between wxString and std::string via JSON_SETTINGS API
|
||||
|
||||
template<> wxString JSON_SETTINGS::Get( std::string aPath ) const;
|
||||
template<> OPT<wxString> JSON_SETTINGS::Get( std::string aPath ) const;
|
||||
|
||||
template<> void JSON_SETTINGS::Set<wxString>( std::string aPath, wxString aVal );
|
||||
|
||||
|
|
|
@ -22,10 +22,11 @@
|
|||
#define _PARAMETERS_H
|
||||
|
||||
#include <string>
|
||||
#include <boost/ptr_container/ptr_vector.hpp>
|
||||
#include <utility>
|
||||
#include <math/util.h>
|
||||
#include "json_settings.h"
|
||||
|
||||
#include <core/optional.h>
|
||||
#include <settings/json_settings.h>
|
||||
|
||||
|
||||
class PARAM_BASE
|
||||
|
@ -81,9 +82,9 @@ public:
|
|||
|
||||
ValueType val = m_default;
|
||||
|
||||
try
|
||||
if( OPT<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
|
||||
{
|
||||
val = aSettings->Get<ValueType>( m_path );
|
||||
val = *optval;
|
||||
|
||||
if( m_use_minmax )
|
||||
{
|
||||
|
@ -91,8 +92,6 @@ public:
|
|||
val = m_default;
|
||||
}
|
||||
}
|
||||
catch( ... )
|
||||
{}
|
||||
|
||||
*m_ptr = val;
|
||||
}
|
||||
|
@ -141,16 +140,10 @@ public:
|
|||
|
||||
ValueType val = m_default;
|
||||
|
||||
try
|
||||
{
|
||||
if( std::is_same<ValueType, nlohmann::json>::value )
|
||||
val = aSettings->GetJson( m_path );
|
||||
else
|
||||
val = aSettings->Get<ValueType>( m_path );
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
}
|
||||
if( std::is_same<ValueType, nlohmann::json>::value )
|
||||
val = aSettings->GetJson( m_path );
|
||||
else
|
||||
val = aSettings->Get<ValueType>( m_path );
|
||||
|
||||
m_setter( val );
|
||||
}
|
||||
|
@ -274,21 +267,16 @@ public:
|
|||
|
||||
std::vector<Type> val = m_default;
|
||||
|
||||
try
|
||||
if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
|
||||
{
|
||||
nlohmann::json js = aSettings->GetJson( m_path );
|
||||
|
||||
if( js.is_array() )
|
||||
if( js->is_array() )
|
||||
{
|
||||
val.clear();
|
||||
|
||||
for( const auto& el : js.items() )
|
||||
for( const auto& el : js->items() )
|
||||
val.push_back( el.value().get<Type>() );
|
||||
}
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
}
|
||||
|
||||
*m_ptr = val;
|
||||
}
|
||||
|
@ -342,21 +330,16 @@ public:
|
|||
|
||||
std::map<std::string, Value> val = m_default;
|
||||
|
||||
try
|
||||
if( OPT<nlohmann::json> js = aSettings->GetJson( m_path ) )
|
||||
{
|
||||
nlohmann::json js = aSettings->GetJson( m_path );
|
||||
|
||||
if( js.is_object() )
|
||||
if( js->is_object() )
|
||||
{
|
||||
val.clear();
|
||||
|
||||
for( const auto& el : js.items() )
|
||||
for( const auto& el : js->items() )
|
||||
val[ el.key() ] = el.value().get<Value>();
|
||||
}
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
}
|
||||
|
||||
*m_ptr = val;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue