Move JSON_SETTINGS and PARAMS to kicommon

This commit is contained in:
Marek Roszko 2024-03-13 22:49:01 -04:00
parent 8e31759191
commit e32b26ebeb
25 changed files with 447 additions and 369 deletions

View File

@ -97,6 +97,13 @@ set( KICOMMON_SRCS
kicad_curl/kicad_curl.cpp
kicad_curl/kicad_curl_easy.cpp
settings/aui_settings.cpp
settings/bom_settings.cpp
settings/grid_settings.cpp
settings/json_settings.cpp
settings/nested_settings.cpp
settings/parameters.cpp
widgets/progress_reporter_base.cpp
widgets/std_bitmap_button.cpp
@ -135,6 +142,8 @@ add_library( kicommon SHARED
${KICOMMON_SRCS}
)
set_target_properties(kicommon PROPERTIES CXX_VISIBILITY_PRESET hidden)
target_link_libraries( kicommon
core
kimath
@ -564,15 +573,9 @@ set( COMMON_SRCS
tool/zoom_tool.cpp
settings/app_settings.cpp
settings/aui_settings.cpp
settings/bom_settings.cpp
settings/color_settings.cpp
settings/cvpcb_settings.cpp
settings/common_settings.cpp
settings/grid_settings.cpp
settings/json_settings.cpp
settings/nested_settings.cpp
settings/parameters.cpp
settings/settings_manager.cpp
settings/kicad_settings.cpp

View File

@ -22,7 +22,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <nlohmann/json.hpp>
#include <json_common.h>
#include <database/database_lib_settings.h>
#include <settings/parameters.h>

View File

@ -24,7 +24,7 @@
#include "easyeda_parser_structs.h"
#include <nlohmann/json.hpp>
#include <json_common.h>
#include <core/json_serializers.h>

View File

@ -29,7 +29,7 @@
#include <ki_exception.h>
#include <string_utils.h>
#include <nlohmann/json.hpp>
#include <json_common.h>
#include <core/json_serializers.h>
#include <wx/log.h>

View File

@ -26,7 +26,7 @@
#include <memory>
#include <nlohmann/json.hpp>
#include <json_common.h>
#include <core/json_serializers.h>
#include <string_utils.h>

View File

@ -36,13 +36,13 @@
#include <notifications_manager.h>
#include <widgets/kistatusbar.h>
#include <json_common.h>
#include "core/wx_stl_compat.h"
#include <algorithm>
#include <fstream>
#include <map>
#include <nlohmann/json.hpp>
#include <core/json_serializers.h>
#include <optional>
#include <string>

View File

@ -23,7 +23,6 @@
#include <project/net_settings.h>
#include <settings/parameters.h>
#include <settings/json_settings_internals.h>
#include <settings/settings_manager.h>
#include <string_utils.h>
#include <base_units.h>

View File

@ -27,7 +27,7 @@
#include <search_stack.h>
#include <settings/settings_manager.h>
#include <settings/common_settings.h>
#include <settings/json_settings_internals.h>
#include <settings/json_settings.h>
#include <settings/parameters.h>
#include <systemdirsappend.h>
#include <trace_helpers.h>

View File

@ -43,8 +43,6 @@
#include <wx/stdstream.h>
#include <wx/wfstream.h>
const wxChar* const traceSettings = wxT( "KICAD_SETTINGS" );
nlohmann::json::json_pointer JSON_SETTINGS_INTERNALS::PointerFromString( std::string aPath )
{
@ -591,23 +589,37 @@ std::optional<ValueType> JSON_SETTINGS::Get( const std::string& aPath ) const
// Instantiate all required templates here to allow reducing scope of json.hpp
template std::optional<bool> JSON_SETTINGS::Get<bool>( const std::string& aPath ) const;
template std::optional<double> JSON_SETTINGS::Get<double>( const std::string& aPath ) const;
template std::optional<float> JSON_SETTINGS::Get<float>( const std::string& aPath ) const;
template std::optional<int> JSON_SETTINGS::Get<int>( const std::string& aPath ) const;
template std::optional<unsigned int> JSON_SETTINGS::Get<unsigned int>( const std::string& aPath ) const;
template std::optional<unsigned long long> JSON_SETTINGS::Get<unsigned long long>( const std::string& aPath ) const;
template std::optional<std::string> JSON_SETTINGS::Get<std::string>( const std::string& aPath ) const;
template std::optional<nlohmann::json> JSON_SETTINGS::Get<nlohmann::json>( const std::string& aPath ) const;
template std::optional<KIGFX::COLOR4D> JSON_SETTINGS::Get<KIGFX::COLOR4D>( const std::string& aPath ) const;
template std::optional<BOM_FIELD> JSON_SETTINGS::Get<BOM_FIELD>( const std::string& aPath ) const;
template std::optional<BOM_PRESET> JSON_SETTINGS::Get<BOM_PRESET>( const std::string& aPath ) const;
template std::optional<BOM_FMT_PRESET> JSON_SETTINGS::Get<BOM_FMT_PRESET>( const std::string& aPath ) const;
template std::optional<GRID> JSON_SETTINGS::Get<GRID>( const std::string& aPath ) const;
template std::optional<wxPoint> JSON_SETTINGS::Get<wxPoint>( const std::string& aPath ) const;
template std::optional<wxSize> JSON_SETTINGS::Get<wxSize>( const std::string& aPath ) const;
template std::optional<wxRect> JSON_SETTINGS::Get<wxRect>( const std::string& aPath ) const;
template std::optional<wxAuiPaneInfo> JSON_SETTINGS::Get<wxAuiPaneInfo>( const std::string& aPath ) const;
template KICOMMON_API std::optional<bool> JSON_SETTINGS::Get<bool>( const std::string& aPath ) const;
template KICOMMON_API std::optional<double>
JSON_SETTINGS::Get<double>( const std::string& aPath ) const;
template KICOMMON_API std::optional<float>
JSON_SETTINGS::Get<float>( const std::string& aPath ) const;
template KICOMMON_API std::optional<int> JSON_SETTINGS::Get<int>( const std::string& aPath ) const;
template KICOMMON_API std::optional<unsigned int>
JSON_SETTINGS::Get<unsigned int>( const std::string& aPath ) const;
template KICOMMON_API std::optional<unsigned long long>
JSON_SETTINGS::Get<unsigned long long>( const std::string& aPath ) const;
template KICOMMON_API std::optional<std::string>
JSON_SETTINGS::Get<std::string>( const std::string& aPath ) const;
template KICOMMON_API std::optional<nlohmann::json>
JSON_SETTINGS::Get<nlohmann::json>( const std::string& aPath ) const;
template KICOMMON_API std::optional<KIGFX::COLOR4D>
JSON_SETTINGS::Get<KIGFX::COLOR4D>( const std::string& aPath ) const;
template KICOMMON_API std::optional<BOM_FIELD>
JSON_SETTINGS::Get<BOM_FIELD>( const std::string& aPath ) const;
template KICOMMON_API std::optional<BOM_PRESET>
JSON_SETTINGS::Get<BOM_PRESET>( const std::string& aPath ) const;
template KICOMMON_API std::optional<BOM_FMT_PRESET>
JSON_SETTINGS::Get<BOM_FMT_PRESET>( const std::string& aPath ) const;
template KICOMMON_API std::optional<GRID> JSON_SETTINGS::Get<GRID>( const std::string& aPath ) const;
template KICOMMON_API std::optional<wxPoint>
JSON_SETTINGS::Get<wxPoint>( const std::string& aPath ) const;
template KICOMMON_API std::optional<wxSize>
JSON_SETTINGS::Get<wxSize>( const std::string& aPath ) const;
template KICOMMON_API std::optional<wxRect>
JSON_SETTINGS::Get<wxRect>( const std::string& aPath ) const;
template KICOMMON_API std::optional<wxAuiPaneInfo>
JSON_SETTINGS::Get<wxAuiPaneInfo>( const std::string& aPath ) const;
template<typename ValueType>
void JSON_SETTINGS::Set( const std::string& aPath, ValueType aVal )
@ -617,24 +629,34 @@ void JSON_SETTINGS::Set( const std::string& aPath, ValueType aVal )
// Instantiate all required templates here to allow reducing scope of json.hpp
template void JSON_SETTINGS::Set<bool>( const std::string& aPath, bool aValue );
template void JSON_SETTINGS::Set<double>( const std::string& aPath, double aValue );
template void JSON_SETTINGS::Set<float>( const std::string& aPath, float aValue );
template void JSON_SETTINGS::Set<int>( const std::string& aPath, int aValue );
template void JSON_SETTINGS::Set<unsigned int>( const std::string& aPath, unsigned int aValue );
template void JSON_SETTINGS::Set<unsigned long long>( const std::string& aPath, unsigned long long aValue );
template void JSON_SETTINGS::Set<const char*>( const std::string& aPath, const char* aValue );
template void JSON_SETTINGS::Set<std::string>( const std::string& aPath, std::string aValue );
template void JSON_SETTINGS::Set<nlohmann::json>( const std::string& aPath, nlohmann::json aValue );
template void JSON_SETTINGS::Set<KIGFX::COLOR4D>( const std::string& aPath, KIGFX::COLOR4D aValue );
template void JSON_SETTINGS::Set<BOM_FIELD>( const std::string& aPath, BOM_FIELD aValue );
template void JSON_SETTINGS::Set<BOM_PRESET>( const std::string& aPath, BOM_PRESET aValue );
template void JSON_SETTINGS::Set<BOM_FMT_PRESET>( const std::string& aPath, BOM_FMT_PRESET aValue );
template void JSON_SETTINGS::Set<GRID>( const std::string& aPath, GRID aValue );
template void JSON_SETTINGS::Set<wxPoint>( const std::string& aPath, wxPoint aValue );
template void JSON_SETTINGS::Set<wxSize>( const std::string& aPath, wxSize aValue );
template void JSON_SETTINGS::Set<wxRect>( const std::string& aPath, wxRect aValue );
template void JSON_SETTINGS::Set<wxAuiPaneInfo>( const std::string& aPath, wxAuiPaneInfo aValue );
template KICOMMON_API void JSON_SETTINGS::Set<bool>( const std::string& aPath, bool aValue );
template KICOMMON_API void JSON_SETTINGS::Set<double>( const std::string& aPath, double aValue );
template KICOMMON_API void JSON_SETTINGS::Set<float>( const std::string& aPath, float aValue );
template KICOMMON_API void JSON_SETTINGS::Set<int>( const std::string& aPath, int aValue );
template KICOMMON_API void JSON_SETTINGS::Set<unsigned int>( const std::string& aPath,
unsigned int aValue );
template KICOMMON_API void JSON_SETTINGS::Set<unsigned long long>( const std::string& aPath,
unsigned long long aValue );
template KICOMMON_API void JSON_SETTINGS::Set<const char*>( const std::string& aPath,
const char* aValue );
template KICOMMON_API void JSON_SETTINGS::Set<std::string>( const std::string& aPath,
std::string aValue );
template KICOMMON_API void JSON_SETTINGS::Set<nlohmann::json>( const std::string& aPath,
nlohmann::json aValue );
template KICOMMON_API void JSON_SETTINGS::Set<KIGFX::COLOR4D>( const std::string& aPath,
KIGFX::COLOR4D aValue );
template KICOMMON_API void JSON_SETTINGS::Set<BOM_FIELD>( const std::string& aPath,
BOM_FIELD aValue );
template KICOMMON_API void JSON_SETTINGS::Set<BOM_PRESET>( const std::string& aPath,
BOM_PRESET aValue );
template KICOMMON_API void JSON_SETTINGS::Set<BOM_FMT_PRESET>( const std::string& aPath,
BOM_FMT_PRESET aValue );
template KICOMMON_API void JSON_SETTINGS::Set<GRID>( const std::string& aPath, GRID aValue );
template KICOMMON_API void JSON_SETTINGS::Set<wxPoint>( const std::string& aPath, wxPoint aValue );
template KICOMMON_API void JSON_SETTINGS::Set<wxSize>( const std::string& aPath, wxSize aValue );
template KICOMMON_API void JSON_SETTINGS::Set<wxRect>( const std::string& aPath, wxRect aValue );
template KICOMMON_API void JSON_SETTINGS::Set<wxAuiPaneInfo>( const std::string& aPath,
wxAuiPaneInfo aValue );
void JSON_SETTINGS::registerMigration( int aOldSchemaVersion, int aNewSchemaVersion,
@ -775,13 +797,13 @@ bool JSON_SETTINGS::fromLegacy( wxConfigBase* aConfig, const std::string& aKey,
// Explicitly declare these because we only support a few types anyway, and it means we can keep
// wxConfig detail out of the header file
template bool JSON_SETTINGS::fromLegacy<int>( wxConfigBase*, const std::string&,
template KICOMMON_API bool JSON_SETTINGS::fromLegacy<int>( wxConfigBase*, const std::string&,
const std::string& );
template bool JSON_SETTINGS::fromLegacy<double>( wxConfigBase*, const std::string&,
template KICOMMON_API bool JSON_SETTINGS::fromLegacy<double>( wxConfigBase*, const std::string&,
const std::string& );
template bool JSON_SETTINGS::fromLegacy<bool>( wxConfigBase*, const std::string&,
template KICOMMON_API bool JSON_SETTINGS::fromLegacy<bool>( wxConfigBase*, const std::string&,
const std::string& );
@ -914,9 +936,10 @@ ResultType JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson, const std
}
template std::string JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson,
template KICOMMON_API std::string JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson,
const std::string& aKey, std::string aDefault );
template bool JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson, const std::string& aKey,
template KICOMMON_API bool JSON_SETTINGS::fetchOrDefault( const nlohmann::json& aJson,
const std::string& aKey,
bool aDefault );

View File

@ -22,192 +22,8 @@
#include <nlohmann/json.hpp>
#include <gal/color4d.h>
#include <project/project_file.h>
#include <settings/parameters.h>
#include <settings/bom_settings.h>
#include <settings/grid_settings.h>
template <typename ValueType>
void PARAM_LAMBDA<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
{
if( m_readOnly )
return;
if( std::is_same<ValueType, nlohmann::json>::value )
{
if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
m_setter( *optval );
else
m_setter( m_default );
}
else
{
if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
m_setter( *optval );
else
m_setter( m_default );
}
}
template <typename ValueType>
bool PARAM_LAMBDA<ValueType>::MatchesFile( JSON_SETTINGS* aSettings ) const
{
if( std::is_same<ValueType, nlohmann::json>::value )
{
if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
return *optval == m_getter();
}
else
{
if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
return *optval == m_getter();
}
// Not in file
return false;
}
// Instantiate all required templates here to allow reducing scope of json.hpp
template class PARAM_LAMBDA<bool>;
template class PARAM_LAMBDA<int>;
template class PARAM_LAMBDA<nlohmann::json>;
template class PARAM_LAMBDA<std::string>;
template <typename ValueType>
void PARAM_LIST<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
{
if( m_readOnly )
return;
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
std::vector<ValueType> val;
if( js->is_array() )
{
for( const auto& el : js->items() )
val.push_back( el.value().get<ValueType>() );
}
*m_ptr = val;
}
else if( aResetIfMissing )
*m_ptr = m_default;
}
template <typename ValueType>
void PARAM_LIST<ValueType>::Store( JSON_SETTINGS* aSettings ) const
{
nlohmann::json js = nlohmann::json::array();
for( const auto& el : *m_ptr )
js.push_back( el );
aSettings->Set<nlohmann::json>( m_path, js );
}
template <typename ValueType>
bool PARAM_LIST<ValueType>::MatchesFile( JSON_SETTINGS* aSettings ) const
{
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_array() )
{
std::vector<ValueType> val;
for( const auto& el : js->items() )
{
try
{
val.emplace_back( el.value().get<ValueType>() );
}
catch( ... )
{
// Probably typecast didn't work; skip this element
}
}
return val == *m_ptr;
}
}
return false;
}
template class PARAM_LIST<bool>;
template class PARAM_LIST<int>;
template class PARAM_LIST<double>;
template class PARAM_LIST<wxString>;
template class PARAM_LIST<KIGFX::COLOR4D>;
template class PARAM_LIST<FILE_INFO_PAIR>;
template class PARAM_LIST<BOM_PRESET>;
template class PARAM_LIST<BOM_FMT_PRESET>;
template class PARAM_LIST<GRID>;
template <typename ValueType>
void PARAM_SET<ValueType>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
{
if( m_readOnly )
return;
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
std::set<ValueType> val;
if( js->is_array() )
{
for( const auto& el : js->items() )
val.insert( el.value().get<ValueType>() );
}
*m_ptr = val;
}
else if( aResetIfMissing )
*m_ptr = m_default;
}
template <typename ValueType>
void PARAM_SET<ValueType>::Store( JSON_SETTINGS* aSettings ) const
{
nlohmann::json js = nlohmann::json::array();
for( const auto& el : *m_ptr )
js.push_back( el );
aSettings->Set<nlohmann::json>( m_path, js );
}
template <typename ValueType>
bool PARAM_SET<ValueType>::MatchesFile( JSON_SETTINGS* aSettings ) const
{
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_array() )
{
std::set<ValueType> val;
for( const auto& el : js->items() )
val.insert( el.value().get<ValueType>() );
return val == *m_ptr;
}
}
return false;
}
template class PARAM_SET<wxString>;
void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const
{
@ -239,66 +55,6 @@ bool PARAM_PATH_LIST::MatchesFile( JSON_SETTINGS* aSettings ) const
}
template <typename Value>
void PARAM_MAP<Value>::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
{
if( m_readOnly )
return;
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_object() )
{
m_ptr->clear();
for( const auto& el : js->items() )
( *m_ptr )[el.key()] = el.value().get<Value>();
}
}
else if( aResetIfMissing )
*m_ptr = m_default;
}
template <typename Value>
void PARAM_MAP<Value>::Store( JSON_SETTINGS* aSettings ) const
{
nlohmann::json js( {} );
for( const auto& el : *m_ptr )
js[el.first] = el.second;
aSettings->Set<nlohmann::json>( m_path, js );
}
template <typename Value>
bool PARAM_MAP<Value>::MatchesFile( JSON_SETTINGS* aSettings ) const
{
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_object() )
{
if( m_ptr->size() != js->size() )
return false;
std::map<std::string, Value> val;
for( const auto& el : js->items() )
val[el.key()] = el.value().get<Value>();
return val == *m_ptr;
}
}
return false;
}
template class PARAM_MAP<int>;
template class PARAM_MAP<double>;
template class PARAM_MAP<bool>;
void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const
{
@ -359,3 +115,26 @@ bool PARAM_WXSTRING_MAP::MatchesFile( JSON_SETTINGS* aSettings ) const
return false;
}
// Instantiate all required templates here and export
template class KICOMMON_API PARAM_LAMBDA<bool>;
template class KICOMMON_API PARAM_LAMBDA<int>;
template class KICOMMON_API PARAM_LAMBDA<nlohmann::json>;
template class KICOMMON_API PARAM_LAMBDA<std::string>;
template class KICOMMON_API PARAM_LIST<bool>;
template class KICOMMON_API PARAM_LIST<int>;
template class KICOMMON_API PARAM_LIST<double>;
template class KICOMMON_API PARAM_LIST<wxString>;
template class KICOMMON_API PARAM_LIST<KIGFX::COLOR4D>;
//template KICOMMON_API class PARAM_LIST<FILE_INFO_PAIR>;
template class KICOMMON_API PARAM_LIST<struct BOM_PRESET>;
template class KICOMMON_API PARAM_LIST<struct BOM_FMT_PRESET>;
template class KICOMMON_API PARAM_LIST<GRID>;
template class KICOMMON_API PARAM_SET<wxString>;
template class KICOMMON_API PARAM_MAP<int>;
template class KICOMMON_API PARAM_MAP<double>;
template class KICOMMON_API PARAM_MAP<bool>;

View File

@ -44,6 +44,16 @@
#define APILOCAL
#endif
// We use APIVISIBLE to mark extern template declarations where we cannot use APIEXPORT
// Because MSVC disallows mixing dllexport and extern templates, we can't just use APIEXPORT
// However MSVC is fine with the dllexport in the cpp file and extern in the header
// But we need the visibility declared on both instantiation and extern for GCC/Clang to make
// the symbol visible
#if defined( __GNUC__ ) || defined( __clang__ )
#define APIVISIBLE __attribute__ ((visibility("default")))
#else
#define APIVISIBLE
#endif
#if defined(COMPILING_DLL)
#define KIFACE_API APIEXPORT

35
include/json_common.h Normal file
View File

@ -0,0 +1,35 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef JSON_COMMON_H
#define JSON_COMMON_H
#include <nlohmann/json.hpp>
#include <kicommon.h>
/**
* This is simply a "stub" meant to inform MSVC when compiling shared libraries that it can find
* template instances in kicommon of nlohmann::json's various templates
*/
class KICOMMON_API JSON_COMMON_EXPORT_STUB final : public nlohmann::json
{
};
#endif

View File

@ -64,7 +64,7 @@ public:
/**
* Get a NETCLASS object from a given Netclass name string
*
*
* @param aNetClassName the Netclass name to resolve
* @return shared pointer to the requested NETCLASS object, or the default NETCLASS
*/

View File

@ -30,21 +30,21 @@ class wxPoint;
class wxRect;
class wxSize;
extern void to_json( nlohmann::json& aJson, const wxPoint& aPoint );
extern void from_json( const nlohmann::json& aJson, wxPoint& aPoint );
extern bool operator<( const wxPoint& aLhs, const wxPoint& aRhs );
KICOMMON_API void to_json( nlohmann::json& aJson, const wxPoint& aPoint );
KICOMMON_API void from_json( const nlohmann::json& aJson, wxPoint& aPoint );
KICOMMON_API bool operator<( const wxPoint& aLhs, const wxPoint& aRhs );
extern void to_json( nlohmann::json& aJson, const wxSize& aPoint );
extern void from_json( const nlohmann::json& aJson, wxSize& aPoint );
extern bool operator<( const wxSize& aLhs, const wxSize& aRhs );
KICOMMON_API void to_json( nlohmann::json& aJson, const wxSize& aPoint );
KICOMMON_API void from_json( const nlohmann::json& aJson, wxSize& aPoint );
KICOMMON_API bool operator<( const wxSize& aLhs, const wxSize& aRhs );
extern void to_json( nlohmann::json& aJson, const wxRect& aRect );
extern void from_json( const nlohmann::json& aJson, wxRect& aRect );
extern bool operator<( const wxRect& aLhs, const wxRect& aRhs );
KICOMMON_API void to_json( nlohmann::json& aJson, const wxRect& aRect );
KICOMMON_API void from_json( const nlohmann::json& aJson, wxRect& aRect );
KICOMMON_API bool operator<( const wxRect& aLhs, const wxRect& aRhs );
extern void to_json( nlohmann::json& aJson, const wxAuiPaneInfo& aPaneInfo );
extern void from_json( const nlohmann::json& aJson, wxAuiPaneInfo& aPaneInfo );
extern bool operator<( const wxAuiPaneInfo& aLhs, const wxAuiPaneInfo& aRhs );
extern bool operator==( const wxAuiPaneInfo& aLhs, const wxAuiPaneInfo& aRhs );
KICOMMON_API void to_json( nlohmann::json& aJson, const wxAuiPaneInfo& aPaneInfo );
KICOMMON_API void from_json( const nlohmann::json& aJson, wxAuiPaneInfo& aPaneInfo );
KICOMMON_API bool operator<( const wxAuiPaneInfo& aLhs, const wxAuiPaneInfo& aRhs );
KICOMMON_API bool operator==( const wxAuiPaneInfo& aLhs, const wxAuiPaneInfo& aRhs );
#endif // _AUI_SETTINGS_H_

View File

@ -27,7 +27,7 @@
#include <i18n_utility.h>
// A single field within a BOM, e.g. Reference, Value, Footprint
struct BOM_FIELD
struct KICOMMON_API BOM_FIELD
{
wxString name;
wxString label;
@ -37,16 +37,16 @@ struct BOM_FIELD
bool operator==( const BOM_FIELD& rhs ) const;
};
bool operator!=( const BOM_FIELD& lhs, const BOM_FIELD& rhs );
bool operator<( const BOM_FIELD& lhs, const BOM_FIELD& rhs );
KICOMMON_API bool operator!=( const BOM_FIELD& lhs, const BOM_FIELD& rhs );
KICOMMON_API bool operator<( const BOM_FIELD& lhs, const BOM_FIELD& rhs );
void to_json( nlohmann::json& j, const BOM_FIELD& f );
void from_json( const nlohmann::json& j, BOM_FIELD& f );
KICOMMON_API void to_json( nlohmann::json& j, const BOM_FIELD& f );
KICOMMON_API void from_json( const nlohmann::json& j, BOM_FIELD& f );
// A complete preset defining a BOM "View" with a list of all the fields to show,
// group by, order, filtering settings, etc.
struct BOM_PRESET
struct KICOMMON_API BOM_PRESET
{
wxString name;
bool readOnly = false;
@ -66,15 +66,15 @@ struct BOM_PRESET
static std::vector<BOM_PRESET> BuiltInPresets();
};
bool operator!=( const BOM_PRESET& lhs, const BOM_PRESET& rhs );
bool operator<( const BOM_PRESET& lhs, const BOM_PRESET& rhs );
KICOMMON_API bool operator!=( const BOM_PRESET& lhs, const BOM_PRESET& rhs );
KICOMMON_API bool operator<( const BOM_PRESET& lhs, const BOM_PRESET& rhs );
void to_json( nlohmann::json& j, const BOM_PRESET& f );
void from_json( const nlohmann::json& j, BOM_PRESET& f );
KICOMMON_API void to_json( nlohmann::json& j, const BOM_PRESET& f );
KICOMMON_API void from_json( const nlohmann::json& j, BOM_PRESET& f );
// A formatting preset, like CSV (Comma Separated Values)
struct BOM_FMT_PRESET
struct KICOMMON_API BOM_FMT_PRESET
{
wxString name;
bool readOnly = false;
@ -94,11 +94,11 @@ struct BOM_FMT_PRESET
static std::vector<BOM_FMT_PRESET> BuiltInPresets();
};
bool operator!=( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs );
bool operator<( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs );
KICOMMON_API bool operator!=( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs );
KICOMMON_API bool operator<( const BOM_FMT_PRESET& lhs, const BOM_FMT_PRESET& rhs );
void to_json( nlohmann::json& j, const BOM_FMT_PRESET& f );
void from_json( const nlohmann::json& j, BOM_FMT_PRESET& f );
KICOMMON_API void to_json( nlohmann::json& j, const BOM_FMT_PRESET& f );
KICOMMON_API void from_json( const nlohmann::json& j, BOM_FMT_PRESET& f );
#endif

View File

@ -30,7 +30,7 @@ class UNITS_PROVIDER;
/**
* Common grid settings, available to every frame
*/
struct GRID
struct KICOMMON_API GRID
{
bool operator==( const GRID& aOther ) const;
@ -53,11 +53,11 @@ struct GRID
wxString y;
};
bool operator!=( const GRID& lhs, const GRID& rhs );
bool operator<( const GRID& lhs, const GRID& rhs );
KICOMMON_API bool operator!=( const GRID& lhs, const GRID& rhs );
KICOMMON_API bool operator<( const GRID& lhs, const GRID& rhs );
void to_json( nlohmann::json& j, const GRID& g );
void from_json( const nlohmann::json& j, GRID& g );
KICOMMON_API void to_json( nlohmann::json& j, const GRID& g );
KICOMMON_API void from_json( const nlohmann::json& j, GRID& g );
struct GRID_SETTINGS

View File

@ -28,21 +28,27 @@
#include <functional>
#include <optional>
#include <nlohmann/json_fwd.hpp>
#include <settings/json_settings_internals.h>
#include <kicommon.h>
class wxConfigBase;
class NESTED_SETTINGS;
class PARAM_BASE;
class SETTINGS_MANAGER;
/**
* Flag to enable debug output of settings operations and management.
*
* Use "KICAD_SETTINGS" to enable.
*
* @ingroup trace_env_vars
*/
extern const wxChar* const traceSettings;
class wxAuiPaneInfo;
struct BOM_FIELD;
struct BOM_PRESET;
struct BOM_FMT_PRESET;
struct GRID;
namespace KIGFX
{
class COLOR4D;
};
#define traceSettings wxT( "KICAD_SETTINGS" )
enum class SETTINGS_LOC {
USER, ///< The main config directory (e.g. ~/.config/kicad/)
@ -56,7 +62,7 @@ enum class SETTINGS_LOC {
/// pimpl to allow hiding json.hpp
class JSON_SETTINGS_INTERNALS;
class JSON_SETTINGS
class KICOMMON_API JSON_SETTINGS
{
public:
friend class NESTED_SETTINGS;
@ -340,14 +346,32 @@ protected:
// Specializations to allow conversion between wxString and std::string via JSON_SETTINGS API
template<> std::optional<wxString> JSON_SETTINGS::Get( const std::string& aPath ) const;
template<> KICOMMON_API std::optional<wxString> JSON_SETTINGS::Get( const std::string& aPath ) const;
template<> void JSON_SETTINGS::Set<wxString>( const std::string& aPath, wxString aVal );
template<> KICOMMON_API void JSON_SETTINGS::Set<wxString>( const std::string& aPath, wxString aVal );
// Specializations to allow directly reading/writing wxStrings from JSON
void to_json( nlohmann::json& aJson, const wxString& aString );
KICOMMON_API void to_json( nlohmann::json& aJson, const wxString& aString );
void from_json( const nlohmann::json& aJson, wxString& aString );
KICOMMON_API void from_json( const nlohmann::json& aJson, wxString& aString );
extern template std::optional<bool> JSON_SETTINGS::Get<bool>( const std::string& aPath ) const;
extern template std::optional<double> JSON_SETTINGS::Get<double>( const std::string& aPath ) const;
extern template std::optional<float> JSON_SETTINGS::Get<float>( const std::string& aPath ) const;
extern template std::optional<int> JSON_SETTINGS::Get<int>( const std::string& aPath ) const;
extern template std::optional<unsigned int> JSON_SETTINGS::Get<unsigned int>( const std::string& aPath ) const;
extern template std::optional<unsigned long long> JSON_SETTINGS::Get<unsigned long long>( const std::string& aPath ) const;
extern template std::optional<std::string> JSON_SETTINGS::Get<std::string>( const std::string& aPath ) const;
extern template std::optional<nlohmann::json> JSON_SETTINGS::Get<nlohmann::json>( const std::string& aPath ) const;
extern template std::optional<KIGFX::COLOR4D> JSON_SETTINGS::Get<KIGFX::COLOR4D>( const std::string& aPath ) const;
extern template std::optional<BOM_FIELD> JSON_SETTINGS::Get<BOM_FIELD>( const std::string& aPath ) const;
extern template std::optional<BOM_PRESET> JSON_SETTINGS::Get<BOM_PRESET>( const std::string& aPath ) const;
extern template std::optional<BOM_FMT_PRESET> JSON_SETTINGS::Get<BOM_FMT_PRESET>( const std::string& aPath ) const;
extern template std::optional<GRID> JSON_SETTINGS::Get<GRID>( const std::string& aPath ) const;
extern template std::optional<wxPoint> JSON_SETTINGS::Get<wxPoint>( const std::string& aPath ) const;
extern template std::optional<wxSize> JSON_SETTINGS::Get<wxSize>( const std::string& aPath ) const;
extern template std::optional<wxRect> JSON_SETTINGS::Get<wxRect>( const std::string& aPath ) const;
extern template std::optional<wxAuiPaneInfo> JSON_SETTINGS::Get<wxAuiPaneInfo>( const std::string& aPath ) const;
#endif

View File

@ -21,10 +21,9 @@
#ifndef KICAD_JSON_SETTINGS_INTERNALS_H
#define KICAD_JSON_SETTINGS_INTERNALS_H
// This is a pretty heavy file. Try to use json_fwd.hpp most places.
#include <nlohmann/json.hpp>
#include <json_common.h>
class JSON_SETTINGS_INTERNALS : public nlohmann::json
class KICOMMON_API JSON_SETTINGS_INTERNALS : public nlohmann::json
{
friend class JSON_SETTINGS;

View File

@ -28,7 +28,7 @@
* NESTED_SETTINGS is a JSON_SETTINGS that lives inside a JSON_SETTINGS.
* Instead of being backed by a JSON file on disk, it loads and stores to its parent.
*/
class NESTED_SETTINGS : public JSON_SETTINGS
class KICOMMON_API NESTED_SETTINGS : public JSON_SETTINGS
{
public:
NESTED_SETTINGS( const std::string& aName, int aSchemaVersion, JSON_SETTINGS* aParent,

View File

@ -27,10 +27,13 @@
#include <math/util.h>
#include <optional>
#include <gal/color4d.h>
#include <settings/json_settings.h>
#include <settings/bom_settings.h>
#include <settings/grid_settings.h>
#include <kicommon.h>
class PARAM_BASE
class KICOMMON_API PARAM_BASE
{
public:
PARAM_BASE( std::string aJsonPath, bool aReadOnly ) :
@ -167,7 +170,7 @@ protected:
/**
* Stores a path as a string with directory separators normalized to unix-style
*/
class PARAM_PATH : public PARAM<wxString>
class KICOMMON_API PARAM_PATH : public PARAM<wxString>
{
public:
PARAM_PATH( const std::string& aJsonPath, wxString* aPtr, const wxString& aDefault,
@ -299,7 +302,26 @@ public:
m_setter( std::move( aSetter ) )
{ }
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override;
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
{
if( m_readOnly )
return;
if( std::is_same<ValueType, nlohmann::json>::value )
{
if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
m_setter( *optval );
else
m_setter( m_default );
}
else
{
if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
m_setter( *optval );
else
m_setter( m_default );
}
}
void Store( JSON_SETTINGS* aSettings ) const override
{
@ -322,7 +344,22 @@ public:
m_setter( m_default );
}
bool MatchesFile( JSON_SETTINGS* aSettings ) const override;
bool MatchesFile( JSON_SETTINGS* aSettings ) const override
{
if( std::is_same<ValueType, nlohmann::json>::value )
{
if( std::optional<nlohmann::json> optval = aSettings->GetJson( m_path ) )
return *optval == m_getter();
}
else
{
if( std::optional<ValueType> optval = aSettings->Get<ValueType>( m_path ) )
return *optval == m_getter();
}
// Not in file
return false;
}
private:
ValueType m_default;
@ -331,6 +368,11 @@ private:
};
extern template class APIVISIBLE PARAM_LAMBDA<bool>;
extern template class APIVISIBLE PARAM_LAMBDA<int>;
extern template class APIVISIBLE PARAM_LAMBDA<nlohmann::json>;
extern template class APIVISIBLE PARAM_LAMBDA<std::string>;
/**
* 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
@ -439,22 +481,86 @@ public:
m_default( std::move( aDefault ) )
{ }
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override;
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
{
if( m_readOnly )
return;
void Store( JSON_SETTINGS* aSettings) const override;
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
std::vector<Type> val;
if( js->is_array() )
{
for( const auto& el : js->items() )
val.push_back( el.value().get<Type>() );
}
*m_ptr = val;
}
else if( aResetIfMissing )
*m_ptr = m_default;
}
void Store( JSON_SETTINGS* aSettings ) const override
{
nlohmann::json js = nlohmann::json::array();
for( const auto& el : *m_ptr )
js.push_back( el );
aSettings->Set<nlohmann::json>( m_path, js );
}
void SetDefault() override
{
*m_ptr = m_default;
}
bool MatchesFile( JSON_SETTINGS* aSettings ) const override;
bool MatchesFile( JSON_SETTINGS* aSettings ) const override
{
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_array() )
{
std::vector<Type> val;
for( const auto& el : js->items() )
{
try
{
val.emplace_back( el.value().get<Type>() );
}
catch( ... )
{
// Probably typecast didn't work; skip this element
}
}
return val == *m_ptr;
}
}
return false;
}
protected:
std::vector<Type>* m_ptr;
std::vector<Type> m_default;
};
extern template class APIVISIBLE PARAM_LIST<bool>;
extern template class APIVISIBLE PARAM_LIST<int>;
extern template class APIVISIBLE PARAM_LIST<double>;
extern template class APIVISIBLE PARAM_LIST<KIGFX::COLOR4D>;
//template KICOMMON_EXTERN_DECL class PARAM_LIST<FILE_INFO_PAIR>;
extern template class APIVISIBLE PARAM_LIST<struct BOM_PRESET>;
extern template class APIVISIBLE PARAM_LIST<struct BOM_FMT_PRESET>;
extern template class APIVISIBLE PARAM_LIST<GRID>;
extern template class APIVISIBLE PARAM_LIST<wxString>;
template<typename Type>
class PARAM_SET : public PARAM_BASE
{
@ -473,27 +579,73 @@ public:
m_default( aDefault )
{ }
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override;
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
{
if( m_readOnly )
return;
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
std::set<Type> val;
if( js->is_array() )
{
for( const auto& el : js->items() )
val.insert( el.value().get<Type>() );
}
*m_ptr = val;
}
else if( aResetIfMissing )
*m_ptr = m_default;
}
void Store( JSON_SETTINGS* aSettings) const override
{
nlohmann::json js = nlohmann::json::array();
for( const auto& el : *m_ptr )
js.push_back( el );
aSettings->Set<nlohmann::json>( m_path, js );
}
void Store( JSON_SETTINGS* aSettings) const override;
void SetDefault() override
{
*m_ptr = m_default;
}
bool MatchesFile( JSON_SETTINGS* aSettings ) const override;
bool MatchesFile( JSON_SETTINGS* aSettings ) const override
{
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_array() )
{
std::set<Type> val;
for( const auto& el : js->items() )
val.insert( el.value().get<Type>() );
return val == *m_ptr;
}
}
return false;
}
protected:
std::set<Type>* m_ptr;
std::set<Type> m_default;
};
extern template class APIVISIBLE PARAM_SET<wxString>;
/**
* Represents a list of strings holding directory paths.
* Normalizes paths to unix directory separator style in the file.
*/
class PARAM_PATH_LIST : public PARAM_LIST<wxString>
class KICOMMON_API PARAM_PATH_LIST : public PARAM_LIST<wxString>
{
public:
PARAM_PATH_LIST( const std::string& aJsonPath, std::vector<wxString>* aPtr,
@ -563,16 +715,60 @@ public:
m_default( aDefault )
{ }
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override;
void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override
{
if( m_readOnly )
return;
void Store( JSON_SETTINGS* aSettings) const override;
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_object() )
{
m_ptr->clear();
for( const auto& el : js->items() )
( *m_ptr )[el.key()] = el.value().get<Value>();
}
}
else if( aResetIfMissing )
*m_ptr = m_default;
}
void Store( JSON_SETTINGS* aSettings) const override
{
nlohmann::json js( {} );
for( const auto& el : *m_ptr )
js[el.first] = el.second;
aSettings->Set<nlohmann::json>( m_path, js );
}
virtual void SetDefault() override
{
*m_ptr = m_default;
}
bool MatchesFile( JSON_SETTINGS* aSettings ) const override;
bool MatchesFile( JSON_SETTINGS* aSettings ) const override
{
if( std::optional<nlohmann::json> js = aSettings->GetJson( m_path ) )
{
if( js->is_object() )
{
if( m_ptr->size() != js->size() )
return false;
std::map<std::string, Value> val;
for( const auto& el : js->items() )
val[el.key()] = el.value().get<Value>();
return val == *m_ptr;
}
}
return false;
}
private:
std::map<std::string, Value>* m_ptr;
@ -580,10 +776,15 @@ private:
};
extern template class APIVISIBLE PARAM_MAP<int>;
extern template class APIVISIBLE PARAM_MAP<double>;
extern template class APIVISIBLE PARAM_MAP<bool>;
/**
* A helper for <wxString, wxString> maps
*/
class PARAM_WXSTRING_MAP : public PARAM_BASE
class KICOMMON_API PARAM_WXSTRING_MAP : public PARAM_BASE
{
public:
PARAM_WXSTRING_MAP( const std::string& aJsonPath, std::map<wxString, wxString>* aPtr,

View File

@ -27,6 +27,7 @@
#include <functional>
#include <iostream>
#include <map>
#include <json_common.h>
#include <nlohmann/json-schema.hpp>
#include <thread>
#include <tuple>

View File

@ -24,7 +24,7 @@
#include "core/wx_stl_compat.h"
#include <map>
#include <nlohmann/json.hpp>
#include <json_common.h>
#include <core/json_serializers.h>
#include <optional>
#include <string>

View File

@ -41,7 +41,7 @@
#include <wx/zipstrm.h>
#include <wx/log.h>
#include <nlohmann/json.hpp>
#include <json_common.h>
#include <core/json_serializers.h>
#include <core/map_helpers.h>
#include <string_utf8_map.h>

View File

@ -6,6 +6,8 @@ add_library( nlohmann_json_schema_validator STATIC
string-format-check.cpp
)
add_dependencies( nlohmann_json_schema_validator kicommon )
target_include_directories( nlohmann_json_schema_validator
PUBLIC
$<INSTALL_INTERFACE:include>
@ -14,4 +16,5 @@ target_include_directories( nlohmann_json_schema_validator
target_link_libraries( nlohmann_json_schema_validator
PUBLIC nlohmann_json
PRIVATE kicommon
)

View File

@ -21,6 +21,7 @@
# define JSON_SCHEMA_VALIDATOR_API
#endif
#include <json_common.h>
#include <nlohmann/json.hpp>
#ifdef NLOHMANN_JSON_VERSION_MAJOR