/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2021 Jon Evans * Copyright (C) 2021 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 . */ #include #include #include #include #include #include #include template void PARAM_LAMBDA::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const { if( m_readOnly ) return; if( std::is_same::value ) { if( std::optional optval = aSettings->GetJson( m_path ) ) m_setter( *optval ); else m_setter( m_default ); } else { if( std::optional optval = aSettings->Get( m_path ) ) m_setter( *optval ); else m_setter( m_default ); } } template bool PARAM_LAMBDA::MatchesFile( JSON_SETTINGS* aSettings ) const { if( std::is_same::value ) { if( std::optional optval = aSettings->GetJson( m_path ) ) return *optval == m_getter(); } else { if( std::optional optval = aSettings->Get( 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; template class PARAM_LAMBDA; template class PARAM_LAMBDA; template class PARAM_LAMBDA; template void PARAM_LIST::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const { if( m_readOnly ) return; if( std::optional js = aSettings->GetJson( m_path ) ) { std::vector val; if( js->is_array() ) { for( const auto& el : js->items() ) val.push_back( el.value().get() ); } *m_ptr = val; } else if( aResetIfMissing ) *m_ptr = m_default; } template void PARAM_LIST::Store( JSON_SETTINGS* aSettings ) const { nlohmann::json js = nlohmann::json::array(); for( const auto& el : *m_ptr ) js.push_back( el ); aSettings->Set( m_path, js ); } template bool PARAM_LIST::MatchesFile( JSON_SETTINGS* aSettings ) const { if( std::optional js = aSettings->GetJson( m_path ) ) { if( js->is_array() ) { std::vector val; for( const auto& el : js->items() ) { try { val.emplace_back( el.value().get() ); } catch( ... ) { // Probably typecast didn't work; skip this element } } return val == *m_ptr; } } return false; } template class PARAM_LIST; template class PARAM_LIST; template class PARAM_LIST; template class PARAM_LIST; template class PARAM_LIST; template class PARAM_LIST; template class PARAM_LIST; template class PARAM_LIST; template void PARAM_SET::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const { if( m_readOnly ) return; if( std::optional js = aSettings->GetJson( m_path ) ) { std::set val; if( js->is_array() ) { for( const auto& el : js->items() ) val.insert( el.value().get() ); } *m_ptr = val; } else if( aResetIfMissing ) *m_ptr = m_default; } template void PARAM_SET::Store( JSON_SETTINGS* aSettings ) const { nlohmann::json js = nlohmann::json::array(); for( const auto& el : *m_ptr ) js.push_back( el ); aSettings->Set( m_path, js ); } template bool PARAM_SET::MatchesFile( JSON_SETTINGS* aSettings ) const { if( std::optional js = aSettings->GetJson( m_path ) ) { if( js->is_array() ) { std::set val; for( const auto& el : js->items() ) val.insert( el.value().get() ); return val == *m_ptr; } } return false; } template class PARAM_SET; void PARAM_PATH_LIST::Store( JSON_SETTINGS* aSettings ) const { nlohmann::json js = nlohmann::json::array(); for( const auto& el : *m_ptr ) js.push_back( toFileFormat( el ) ); aSettings->Set( m_path, js ); } bool PARAM_PATH_LIST::MatchesFile( JSON_SETTINGS* aSettings ) const { if( std::optional js = aSettings->GetJson( m_path ) ) { if( js->is_array() ) { std::vector val; for( const auto& el : js->items() ) val.emplace_back( fromFileFormat( el.value().get() ) ); return val == *m_ptr; } } return false; } template void PARAM_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const { if( m_readOnly ) return; if( std::optional 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(); } } else if( aResetIfMissing ) *m_ptr = m_default; } template void PARAM_MAP::Store( JSON_SETTINGS* aSettings ) const { nlohmann::json js( {} ); for( const auto& el : *m_ptr ) js[el.first] = el.second; aSettings->Set( m_path, js ); } template bool PARAM_MAP::MatchesFile( JSON_SETTINGS* aSettings ) const { if( std::optional js = aSettings->GetJson( m_path ) ) { if( js->is_object() ) { if( m_ptr->size() != js->size() ) return false; std::map val; for( const auto& el : js->items() ) val[el.key()] = el.value().get(); return val == *m_ptr; } } return false; } template class PARAM_MAP; template class PARAM_MAP; template class PARAM_MAP; void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) const { if( m_readOnly ) return; if( std::optional js = aSettings->GetJson( m_path ) ) { if( js->is_object() ) { m_ptr->clear(); for( const auto& el : js->items() ) { ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get(); } } } else if( aResetIfMissing ) *m_ptr = m_default; } void PARAM_WXSTRING_MAP::Store( JSON_SETTINGS* aSettings ) const { nlohmann::json js( {} ); for( const auto& el : *m_ptr ) { std::string key( el.first.ToUTF8() ); js[key] = el.second; } aSettings->Set( m_path, js ); } bool PARAM_WXSTRING_MAP::MatchesFile( JSON_SETTINGS* aSettings ) const { if( std::optional js = aSettings->GetJson( m_path ) ) { if( js->is_object() ) { if( m_ptr->size() != js->size() ) return false; std::map val; for( const auto& el : js->items() ) { wxString key( el.key().c_str(), wxConvUTF8 ); val[key] = el.value().get(); } return val == *m_ptr; } } return false; }