2022-02-21 03:58:31 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 Mikolaj Wielgus
|
|
|
|
* Copyright (C) 2022 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, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sim/sim_value.h>
|
2022-03-09 01:40:59 +00:00
|
|
|
#include <wx/translation.h>
|
|
|
|
#include <ki_exception.h>
|
2022-02-21 03:58:31 +00:00
|
|
|
#include <locale_io.h>
|
2022-03-09 01:40:59 +00:00
|
|
|
#include <pegtl/contrib/parse_tree.hpp>
|
2022-09-08 11:04:31 +00:00
|
|
|
#include <fmt/core.h>
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
#define CALL_INSTANCE( ValueType, Notation, func, ... ) \
|
|
|
|
switch( ValueType ) \
|
|
|
|
{ \
|
|
|
|
case SIM_VALUE::TYPE_INT: \
|
|
|
|
switch( Notation ) \
|
|
|
|
{ \
|
|
|
|
case NOTATION::SI: \
|
|
|
|
func<SIM_VALUE::TYPE_INT, NOTATION::SI>( __VA_ARGS__ ); \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
case NOTATION::SPICE: \
|
|
|
|
func<SIM_VALUE::TYPE_INT, NOTATION::SPICE>( __VA_ARGS__ ); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
case SIM_VALUE::TYPE_FLOAT: \
|
|
|
|
switch( Notation ) \
|
|
|
|
{ \
|
|
|
|
case NOTATION::SI: \
|
|
|
|
func<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>( __VA_ARGS__ ); \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
case NOTATION::SPICE: \
|
|
|
|
func<SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE>( __VA_ARGS__ ); \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
break; \
|
|
|
|
\
|
|
|
|
case SIM_VALUE::TYPE_BOOL: \
|
|
|
|
case SIM_VALUE::TYPE_COMPLEX: \
|
|
|
|
case SIM_VALUE::TYPE_STRING: \
|
|
|
|
case SIM_VALUE::TYPE_BOOL_VECTOR: \
|
|
|
|
case SIM_VALUE::TYPE_INT_VECTOR: \
|
|
|
|
case SIM_VALUE::TYPE_FLOAT_VECTOR: \
|
|
|
|
case SIM_VALUE::TYPE_COMPLEX_VECTOR: \
|
|
|
|
wxFAIL_MSG( "Unhandled SIM_VALUE type" ); \
|
|
|
|
break; \
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-01 04:30:50 +00:00
|
|
|
namespace SIM_VALUE_PARSER
|
|
|
|
{
|
|
|
|
using namespace SIM_VALUE_GRAMMAR;
|
|
|
|
|
|
|
|
template <typename Rule>
|
|
|
|
struct numberSelector : std::false_type {};
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
// TODO: Reorder. NOTATION should be before TYPE.
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct numberSelector<SIM_VALUE_GRAMMAR::significand<SIM_VALUE::TYPE_INT>>
|
2022-04-01 04:30:50 +00:00
|
|
|
: std::true_type {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct numberSelector<SIM_VALUE_GRAMMAR::significand<SIM_VALUE::TYPE_FLOAT>>
|
2022-04-01 04:30:50 +00:00
|
|
|
: std::true_type {};
|
|
|
|
template <> struct numberSelector<intPart> : std::true_type {};
|
|
|
|
template <> struct numberSelector<fracPart> : std::true_type {};
|
|
|
|
template <> struct numberSelector<exponent> : std::true_type {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct numberSelector<metricSuffix<SIM_VALUE::TYPE_INT, NOTATION::SI>>
|
2022-04-01 04:30:50 +00:00
|
|
|
: std::true_type {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct numberSelector<metricSuffix<SIM_VALUE::TYPE_INT, NOTATION::SPICE>>
|
2022-04-01 04:30:50 +00:00
|
|
|
: std::true_type {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct numberSelector<metricSuffix<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>>
|
2022-04-01 04:30:50 +00:00
|
|
|
: std::true_type {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct numberSelector<metricSuffix<SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE>>
|
2022-04-01 04:30:50 +00:00
|
|
|
: std::true_type {};
|
|
|
|
|
|
|
|
struct PARSE_RESULT
|
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
bool isOk = true;
|
2022-04-01 04:30:50 +00:00
|
|
|
bool isEmpty = true;
|
|
|
|
std::string significand;
|
2022-09-18 00:22:51 +00:00
|
|
|
std::optional<int64_t> intPart;
|
|
|
|
std::optional<int64_t> fracPart;
|
|
|
|
std::optional<int> exponent;
|
|
|
|
std::optional<int> metricSuffixExponent;
|
2022-04-01 04:30:50 +00:00
|
|
|
};
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
PARSE_RESULT Parse( const std::string& aString,
|
2022-04-12 14:37:06 +00:00
|
|
|
NOTATION aNotation = NOTATION::SI,
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE::TYPE aValueType = SIM_VALUE::TYPE_FLOAT );
|
2022-04-01 04:30:50 +00:00
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
int MetricSuffixToExponent( std::string aMetricSuffix, NOTATION aNotation = NOTATION::SI );
|
|
|
|
std::string ExponentToMetricSuffix( double aExponent, int& aReductionExponent,
|
2022-09-08 11:04:31 +00:00
|
|
|
NOTATION aNotation = NOTATION::SI );
|
|
|
|
}
|
2022-04-01 04:30:50 +00:00
|
|
|
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
template <SIM_VALUE::TYPE ValueType, SIM_VALUE_PARSER::NOTATION Notation>
|
2022-03-09 01:40:59 +00:00
|
|
|
static inline void doIsValid( tao::pegtl::string_input<>& aIn )
|
|
|
|
{
|
|
|
|
tao::pegtl::parse<SIM_VALUE_PARSER::numberGrammar<ValueType, Notation>>( aIn );
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
bool SIM_VALUE_GRAMMAR::IsValid( const std::string& aString,
|
2022-04-12 14:37:06 +00:00
|
|
|
SIM_VALUE::TYPE aValueType,
|
2022-03-09 01:40:59 +00:00
|
|
|
NOTATION aNotation )
|
|
|
|
{
|
2022-09-16 04:11:14 +00:00
|
|
|
tao::pegtl::string_input<> in( aString, "from_content" );
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
CALL_INSTANCE( aValueType, aNotation, doIsValid, in );
|
|
|
|
}
|
2022-08-15 19:45:54 +00:00
|
|
|
catch( const tao::pegtl::parse_error& )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
template <SIM_VALUE::TYPE ValueType, SIM_VALUE_PARSER::NOTATION Notation>
|
2022-03-09 01:40:59 +00:00
|
|
|
static inline std::unique_ptr<tao::pegtl::parse_tree::node> doParse(
|
|
|
|
tao::pegtl::string_input<>& aIn )
|
|
|
|
{
|
|
|
|
return tao::pegtl::parse_tree::parse<SIM_VALUE_PARSER::numberGrammar<ValueType, Notation>,
|
|
|
|
SIM_VALUE_PARSER::numberSelector>
|
|
|
|
( aIn );
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
|
|
|
|
template <SIM_VALUE::TYPE ValueType, SIM_VALUE_PARSER::NOTATION Notation>
|
2022-03-09 01:40:59 +00:00
|
|
|
static inline void handleNodeForParse( tao::pegtl::parse_tree::node& aNode,
|
|
|
|
SIM_VALUE_PARSER::PARSE_RESULT& aParseResult )
|
|
|
|
{
|
|
|
|
if( aNode.is_type<SIM_VALUE_PARSER::significand<ValueType>>() )
|
|
|
|
{
|
|
|
|
aParseResult.significand = aNode.string();
|
|
|
|
aParseResult.isEmpty = false;
|
|
|
|
|
|
|
|
for( const auto& subnode : aNode.children )
|
|
|
|
{
|
|
|
|
if( subnode->is_type<SIM_VALUE_PARSER::intPart>() )
|
|
|
|
aParseResult.intPart = std::stol( subnode->string() );
|
|
|
|
else if( subnode->is_type<SIM_VALUE_PARSER::fracPart>() )
|
|
|
|
aParseResult.fracPart = std::stol( subnode->string() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( aNode.is_type<SIM_VALUE_PARSER::exponent>() )
|
|
|
|
{
|
2022-09-18 00:22:51 +00:00
|
|
|
aParseResult.exponent = std::stoi( aNode.string() );
|
2022-03-09 01:40:59 +00:00
|
|
|
aParseResult.isEmpty = false;
|
|
|
|
}
|
|
|
|
else if( aNode.is_type<SIM_VALUE_PARSER::metricSuffix<ValueType, Notation>>() )
|
|
|
|
{
|
|
|
|
aParseResult.metricSuffixExponent =
|
|
|
|
SIM_VALUE_PARSER::MetricSuffixToExponent( aNode.string(), Notation );
|
|
|
|
aParseResult.isEmpty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wxFAIL_MSG( "Unhandled parse tree node" );
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
SIM_VALUE_PARSER::PARSE_RESULT SIM_VALUE_PARSER::Parse( const std::string& aString,
|
2022-04-12 14:37:06 +00:00
|
|
|
NOTATION aNotation,
|
|
|
|
SIM_VALUE::TYPE aValueType )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
LOCALE_IO toggle;
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
tao::pegtl::string_input<> in( aString, "from_content" );
|
2022-03-09 01:40:59 +00:00
|
|
|
std::unique_ptr<tao::pegtl::parse_tree::node> root;
|
2022-04-12 14:37:06 +00:00
|
|
|
PARSE_RESULT result;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
CALL_INSTANCE( aValueType, aNotation, root = doParse, in );
|
|
|
|
}
|
2022-08-15 19:45:54 +00:00
|
|
|
catch( tao::pegtl::parse_error& )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
result.isOk = false;
|
|
|
|
return result;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( root );
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
for( const auto& node : root->children )
|
2022-04-01 04:30:50 +00:00
|
|
|
{
|
2022-03-09 01:40:59 +00:00
|
|
|
CALL_INSTANCE( aValueType, aNotation, handleNodeForParse, *node, result );
|
2022-04-01 04:30:50 +00:00
|
|
|
}
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
2022-05-31 02:55:48 +00:00
|
|
|
catch( const std::invalid_argument& e )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-09-16 04:11:14 +00:00
|
|
|
wxFAIL_MSG( fmt::format( "Parsing simulator value failed: {:s}", e.what() ) );
|
2022-04-12 14:37:06 +00:00
|
|
|
result.isOk = false;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
int SIM_VALUE_PARSER::MetricSuffixToExponent( std::string aMetricSuffix, NOTATION aNotation )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
switch( aNotation )
|
|
|
|
{
|
|
|
|
case NOTATION::SI:
|
|
|
|
if( aMetricSuffix.empty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch( aMetricSuffix[0] )
|
|
|
|
{
|
|
|
|
case 'a': return -18;
|
|
|
|
case 'f': return -15;
|
|
|
|
case 'p': return -12;
|
|
|
|
case 'n': return -9;
|
|
|
|
case 'u': return -6;
|
|
|
|
case 'm': return -3;
|
|
|
|
case 'k':
|
|
|
|
case 'K': return 3;
|
|
|
|
case 'M': return 6;
|
|
|
|
case 'G': return 9;
|
|
|
|
case 'T': return 12;
|
|
|
|
case 'P': return 15;
|
|
|
|
case 'E': return 18;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NOTATION::SPICE:
|
|
|
|
std::transform( aMetricSuffix.begin(), aMetricSuffix.end(), aMetricSuffix.begin(),
|
|
|
|
::tolower );
|
|
|
|
|
|
|
|
if( aMetricSuffix == "f" )
|
|
|
|
return -15;
|
|
|
|
else if( aMetricSuffix == "p" )
|
|
|
|
return -12;
|
|
|
|
else if( aMetricSuffix == "n" )
|
|
|
|
return -9;
|
|
|
|
else if( aMetricSuffix == "u" )
|
|
|
|
return -6;
|
|
|
|
else if( aMetricSuffix == "m" )
|
|
|
|
return -3;
|
|
|
|
else if( aMetricSuffix == "" )
|
|
|
|
return 0;
|
|
|
|
else if( aMetricSuffix == "k" )
|
|
|
|
return 3;
|
|
|
|
else if( aMetricSuffix == "meg" )
|
|
|
|
return 6;
|
|
|
|
else if( aMetricSuffix == "g" )
|
|
|
|
return 9;
|
|
|
|
else if( aMetricSuffix == "t" )
|
|
|
|
return 12;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
wxFAIL_MSG( fmt::format( "Unknown simulator value suffix: '{:s}'", aMetricSuffix ) );
|
2022-04-12 14:37:06 +00:00
|
|
|
return 0;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
std::string SIM_VALUE_PARSER::ExponentToMetricSuffix( double aExponent, int& aReductionExponent,
|
|
|
|
NOTATION aNotation )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
if( aNotation == NOTATION::SI && aExponent >= -18 && aExponent <= -15 )
|
|
|
|
{
|
|
|
|
aReductionExponent = -18;
|
|
|
|
return "a";
|
|
|
|
}
|
2022-05-08 09:54:35 +00:00
|
|
|
else if( aExponent >= -15 && aExponent < -12 )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
aReductionExponent = -15;
|
|
|
|
return "f";
|
|
|
|
}
|
2022-05-08 09:54:35 +00:00
|
|
|
else if( aExponent >= -12 && aExponent < -9 )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
aReductionExponent = -12;
|
|
|
|
return "p";
|
|
|
|
}
|
2022-05-08 09:54:35 +00:00
|
|
|
else if( aExponent >= -9 && aExponent < -6 )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
aReductionExponent = -9;
|
|
|
|
return "n";
|
|
|
|
}
|
2022-05-08 09:54:35 +00:00
|
|
|
else if( aExponent >= -6 && aExponent < -3 )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
aReductionExponent = -6;
|
|
|
|
return "u";
|
|
|
|
}
|
2022-05-08 09:54:35 +00:00
|
|
|
else if( aExponent >= -3 && aExponent < 0 )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
aReductionExponent = -3;
|
|
|
|
return "m";
|
|
|
|
}
|
2022-05-08 09:54:35 +00:00
|
|
|
else if( aExponent >= 0 && aExponent < 3 )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
aReductionExponent = 0;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
else if( aExponent >= 3 && aExponent < 6 )
|
|
|
|
{
|
|
|
|
aReductionExponent = 3;
|
|
|
|
return "k";
|
|
|
|
}
|
|
|
|
else if( aExponent >= 6 && aExponent < 9 )
|
|
|
|
{
|
|
|
|
aReductionExponent = 6;
|
|
|
|
return ( aNotation == NOTATION::SI ) ? "M" : "Meg";
|
|
|
|
}
|
|
|
|
else if( aExponent >= 9 && aExponent < 12 )
|
|
|
|
{
|
|
|
|
aReductionExponent = 9;
|
|
|
|
return "G";
|
|
|
|
}
|
|
|
|
else if( aExponent >= 12 && aExponent < 15 )
|
|
|
|
{
|
|
|
|
aReductionExponent = 12;
|
|
|
|
return "T";
|
|
|
|
}
|
|
|
|
else if( aNotation == NOTATION::SI && aExponent >= 15 && aExponent < 18 )
|
|
|
|
{
|
|
|
|
aReductionExponent = 15;
|
|
|
|
return "P";
|
|
|
|
}
|
|
|
|
else if( aNotation == NOTATION::SI && aExponent >= 18 && aExponent <= 21 )
|
|
|
|
{
|
|
|
|
aReductionExponent = 18;
|
|
|
|
return "E";
|
|
|
|
}
|
2022-08-15 19:45:54 +00:00
|
|
|
|
2022-03-09 01:40:59 +00:00
|
|
|
aReductionExponent = 0;
|
|
|
|
return "";
|
|
|
|
}
|
2022-02-21 03:58:31 +00:00
|
|
|
|
|
|
|
|
2022-10-04 15:17:41 +00:00
|
|
|
std::unique_ptr<SIM_VALUE> SIM_VALUE::Create( TYPE aType, std::string aString, NOTATION aNotation )
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
std::unique_ptr<SIM_VALUE> value = SIM_VALUE::Create( aType );
|
2022-10-04 15:17:41 +00:00
|
|
|
value->FromString( aString, aNotation );
|
2022-02-21 03:58:31 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
std::unique_ptr<SIM_VALUE> SIM_VALUE::Create( TYPE aType )
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
|
|
|
switch( aType )
|
|
|
|
{
|
2022-09-03 17:39:49 +00:00
|
|
|
case TYPE_BOOL: return std::make_unique<SIM_VALUE_BOOL>();
|
|
|
|
case TYPE_INT: return std::make_unique<SIM_VALUE_INT>();
|
|
|
|
case TYPE_FLOAT: return std::make_unique<SIM_VALUE_FLOAT>();
|
|
|
|
case TYPE_COMPLEX: return std::make_unique<SIM_VALUE_COMPLEX>();
|
|
|
|
case TYPE_STRING: return std::make_unique<SIM_VALUE_STRING>();
|
|
|
|
case TYPE_BOOL_VECTOR: return std::make_unique<SIM_VALUE_BOOL>();
|
|
|
|
case TYPE_INT_VECTOR: return std::make_unique<SIM_VALUE_INT>();
|
|
|
|
case TYPE_FLOAT_VECTOR: return std::make_unique<SIM_VALUE_FLOAT>();
|
|
|
|
case TYPE_COMPLEX_VECTOR: return std::make_unique<SIM_VALUE_COMPLEX>();
|
2022-02-21 03:58:31 +00:00
|
|
|
}
|
2022-08-15 19:45:54 +00:00
|
|
|
|
2022-03-09 01:40:59 +00:00
|
|
|
wxFAIL_MSG( _( "Unknown SIM_VALUE type" ) );
|
2022-02-21 03:58:31 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
void SIM_VALUE::operator=( const std::string& aString )
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
|
|
|
FromString( aString );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
bool SIM_VALUE::operator!=( const SIM_VALUE& aOther ) const
|
|
|
|
{
|
|
|
|
return !( *this == aOther );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-21 03:58:31 +00:00
|
|
|
template <typename T>
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE_INST<T>::SIM_VALUE_INST( const T& aValue ) : m_value( aValue )
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template SIM_VALUE_BOOL::SIM_VALUE_INST( const bool& aValue );
|
2022-09-18 00:22:51 +00:00
|
|
|
template SIM_VALUE_INT::SIM_VALUE_INST( const int& aValue );
|
2022-09-03 17:39:49 +00:00
|
|
|
template SIM_VALUE_FLOAT::SIM_VALUE_INST( const double& aValue );
|
|
|
|
template SIM_VALUE_COMPLEX::SIM_VALUE_INST( const std::complex<double>& aValue );
|
2022-09-16 04:11:14 +00:00
|
|
|
template SIM_VALUE_STRING::SIM_VALUE_INST( const std::string& aValue );
|
2022-09-03 17:39:49 +00:00
|
|
|
|
|
|
|
|
2022-10-04 15:17:41 +00:00
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_BOOL::GetType() const { return TYPE_BOOL; }
|
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_INT::GetType() const { return TYPE_INT; }
|
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_FLOAT::GetType() const { return TYPE_FLOAT; }
|
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_COMPLEX::GetType() const { return TYPE_FLOAT; }
|
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_STRING::GetType() const { return TYPE_STRING; }
|
|
|
|
// TODO
|
|
|
|
/*template <> SIM_VALUE::TYPE SIM_VALUE_BOOL_VECTOR::GetType() const { return TYPE_BOOL; }
|
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_INT_VECTOR::GetType() const { return TYPE_INT; }
|
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_FLOAT_VECTOR::GetType() const { return TYPE_FLOAT; }
|
|
|
|
template <> SIM_VALUE::TYPE SIM_VALUE_COMPLEX_VECTOR::GetType() const { return TYPE_COMPLEX; }*/
|
|
|
|
|
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template <typename T>
|
|
|
|
bool SIM_VALUE_INST<T>::HasValue() const
|
|
|
|
{
|
|
|
|
return m_value.has_value();
|
|
|
|
}
|
2022-06-21 02:22:52 +00:00
|
|
|
|
2022-02-21 03:58:31 +00:00
|
|
|
|
2022-03-09 01:40:59 +00:00
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
bool SIM_VALUE_BOOL::FromString( const std::string& aString, NOTATION aNotation )
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
SIM_VALUE_PARSER::PARSE_RESULT parseResult = SIM_VALUE_PARSER::Parse( aString, aNotation );
|
2022-08-25 22:50:47 +00:00
|
|
|
m_value = std::nullopt;
|
2022-04-12 14:37:06 +00:00
|
|
|
|
|
|
|
if( !parseResult.isOk )
|
|
|
|
return false;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
if( parseResult.isEmpty )
|
2022-04-12 14:37:06 +00:00
|
|
|
return true;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
if( !parseResult.intPart
|
|
|
|
|| ( *parseResult.intPart != 0 && *parseResult.intPart != 1 )
|
|
|
|
|| parseResult.fracPart
|
|
|
|
|| parseResult.exponent
|
|
|
|
|| parseResult.metricSuffixExponent )
|
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
return false;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_value = *parseResult.intPart;
|
2022-04-12 14:37:06 +00:00
|
|
|
return true;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
bool SIM_VALUE_INT::FromString( const std::string& aString, NOTATION aNotation )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
SIM_VALUE_PARSER::PARSE_RESULT parseResult = SIM_VALUE_PARSER::Parse( aString, aNotation );
|
2022-08-25 22:50:47 +00:00
|
|
|
m_value = std::nullopt;
|
2022-04-12 14:37:06 +00:00
|
|
|
|
|
|
|
if( !parseResult.isOk )
|
|
|
|
return false;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
if( parseResult.isEmpty )
|
2022-04-12 14:37:06 +00:00
|
|
|
return true;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-05-31 02:55:48 +00:00
|
|
|
if( !parseResult.intPart || ( parseResult.fracPart && *parseResult.fracPart != 0 ) )
|
2022-04-12 14:37:06 +00:00
|
|
|
return false;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
int exponent = parseResult.exponent ? *parseResult.exponent : 0;
|
2022-03-09 01:40:59 +00:00
|
|
|
exponent += parseResult.metricSuffixExponent ? *parseResult.metricSuffixExponent : 0;
|
|
|
|
|
|
|
|
m_value = static_cast<double>( *parseResult.intPart ) * std::pow( 10, exponent );
|
2022-04-12 14:37:06 +00:00
|
|
|
return true;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
bool SIM_VALUE_FLOAT::FromString( const std::string& aString, NOTATION aNotation )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-04-12 14:37:06 +00:00
|
|
|
SIM_VALUE_PARSER::PARSE_RESULT parseResult = SIM_VALUE_PARSER::Parse( aString, aNotation );
|
2022-08-25 22:50:47 +00:00
|
|
|
m_value = std::nullopt;
|
2022-04-12 14:37:06 +00:00
|
|
|
|
|
|
|
if( !parseResult.isOk )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( parseResult.isEmpty )
|
|
|
|
return true;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
// Single dot should be allowed in fields.
|
|
|
|
// TODO: disallow single dot in models.
|
2022-04-12 14:37:06 +00:00
|
|
|
if( parseResult.significand.empty() || parseResult.significand == "." )
|
|
|
|
return false;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
int exponent = parseResult.exponent ? *parseResult.exponent : 0;
|
2022-03-09 01:40:59 +00:00
|
|
|
exponent += parseResult.metricSuffixExponent ? *parseResult.metricSuffixExponent : 0;
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_value = std::stod( parseResult.significand ) * std::pow( 10, exponent );
|
|
|
|
}
|
2022-08-15 19:45:54 +00:00
|
|
|
catch( const std::invalid_argument& )
|
2022-04-12 14:37:06 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
bool SIM_VALUE_COMPLEX::FromString( const std::string& aString,
|
2022-04-12 14:37:06 +00:00
|
|
|
NOTATION aNotation )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
/*LOCALE_IO toggle;
|
|
|
|
|
|
|
|
double value = 0;
|
|
|
|
|
|
|
|
if( !aString.ToDouble( &value ) )
|
|
|
|
throw KI_PARAM_ERROR( _( "Invalid complex sim value string" ) );
|
|
|
|
|
|
|
|
m_value = value;*/
|
2022-04-12 14:37:06 +00:00
|
|
|
return true;
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
bool SIM_VALUE_STRING::FromString( const std::string& aString, NOTATION aNotation )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
m_value = aString;
|
2022-04-12 14:37:06 +00:00
|
|
|
return true;
|
2022-02-21 03:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_INST<T>::ToString( NOTATION aNotation ) const
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
|
|
|
static_assert( std::is_same<T, std::vector<T>>::value );
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string string = "";
|
2022-02-21 03:58:31 +00:00
|
|
|
|
|
|
|
for( auto it = m_value.cbegin(); it != m_value.cend(); it++ )
|
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
string += SIM_VALUE_INST<T>( *it ).ToString();
|
2022-02-21 03:58:31 +00:00
|
|
|
string += ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_BOOL::ToString( NOTATION aNotation ) const
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-05-08 09:54:35 +00:00
|
|
|
if( m_value )
|
2022-09-08 11:17:09 +00:00
|
|
|
return fmt::format( "{:d}", *m_value );
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
return "";
|
2022-02-21 03:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_INT::ToString( NOTATION aNotation ) const
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-05-08 09:54:35 +00:00
|
|
|
if( m_value )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-09-18 00:22:51 +00:00
|
|
|
int value = std::abs( *m_value );
|
|
|
|
int exponent = 0;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-05-08 09:54:35 +00:00
|
|
|
while( value != 0 && value % 1000 == 0 )
|
2022-04-01 04:30:50 +00:00
|
|
|
{
|
|
|
|
exponent += 3;
|
|
|
|
value /= 1000;
|
|
|
|
}
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
int dummy = 0;
|
2022-09-08 11:04:31 +00:00
|
|
|
std::string metricSuffix = SIM_VALUE_PARSER::ExponentToMetricSuffix(
|
2022-04-12 14:37:06 +00:00
|
|
|
static_cast<double>( exponent ), dummy, aNotation );
|
2022-09-18 00:22:51 +00:00
|
|
|
return fmt::format( "{:d}{:s}", value, metricSuffix );
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
2022-02-21 03:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_FLOAT::ToString( NOTATION aNotation ) const
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-05-08 09:54:35 +00:00
|
|
|
if( m_value )
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-05-31 02:55:48 +00:00
|
|
|
double exponent = std::log10( std::abs( *m_value ) );
|
2022-09-18 00:22:51 +00:00
|
|
|
int reductionExponent = 0;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-09-08 11:04:31 +00:00
|
|
|
std::string metricSuffix =
|
|
|
|
SIM_VALUE_PARSER::ExponentToMetricSuffix( exponent, reductionExponent, aNotation );
|
2022-03-09 01:40:59 +00:00
|
|
|
double reducedValue = *m_value / std::pow( 10, reductionExponent );
|
|
|
|
|
2022-09-08 11:17:09 +00:00
|
|
|
return fmt::format( "{:g}{}", reducedValue, metricSuffix );
|
2022-03-09 01:40:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return "";
|
2022-02-21 03:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_COMPLEX::ToString( NOTATION aNotation ) const
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-05-08 09:54:35 +00:00
|
|
|
if( m_value )
|
2022-09-08 11:17:09 +00:00
|
|
|
return fmt::format( "{:g}+{:g}i", m_value->real(), m_value->imag() );
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
return "";
|
2022-02-21 03:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_STRING::ToString( NOTATION aNotation ) const
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-05-08 09:54:35 +00:00
|
|
|
if( m_value )
|
2022-03-09 01:40:59 +00:00
|
|
|
return *m_value;
|
|
|
|
|
|
|
|
return ""; // Empty string is completely equivalent to null string.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_INST<T>::ToSimpleString() const
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
2022-05-08 09:54:35 +00:00
|
|
|
if( m_value )
|
2022-09-16 04:11:14 +00:00
|
|
|
return fmt::format( "{}", *m_value );
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string SIM_VALUE_COMPLEX::ToSimpleString() const
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
/*std::string result = "";
|
2022-03-09 01:40:59 +00:00
|
|
|
result << *m_value;
|
|
|
|
|
|
|
|
return result;*/
|
|
|
|
return "";
|
2022-02-21 03:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-04 15:17:41 +00:00
|
|
|
template <typename T>
|
|
|
|
void SIM_VALUE_INST<T>::operator=( const SIM_VALUE& aOther )
|
|
|
|
{
|
|
|
|
auto other = dynamic_cast<const SIM_VALUE_INST<T>*>( &aOther );
|
|
|
|
m_value = other->m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-21 03:58:31 +00:00
|
|
|
template <typename T>
|
2022-06-21 02:22:52 +00:00
|
|
|
bool SIM_VALUE_INST<T>::operator==( const T& aOther ) const
|
|
|
|
{
|
|
|
|
return m_value == aOther;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
2022-09-03 17:39:49 +00:00
|
|
|
bool SIM_VALUE_BOOL::operator==( const bool& aOther ) const
|
2022-06-21 02:22:52 +00:00
|
|
|
{
|
2022-08-28 12:51:03 +00:00
|
|
|
// Note that we take nullopt as the same as false here.
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
if( !m_value )
|
|
|
|
return false == aOther;
|
|
|
|
|
|
|
|
return m_value == aOther;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
template bool SIM_VALUE_INT::operator==( const int& aOther ) const;
|
2022-09-03 17:39:49 +00:00
|
|
|
template bool SIM_VALUE_FLOAT::operator==( const double& aOther ) const;
|
|
|
|
template bool SIM_VALUE_COMPLEX::operator==( const std::complex<double>& aOther ) const;
|
2022-09-16 04:11:14 +00:00
|
|
|
template bool SIM_VALUE_STRING::operator==( const std::string& aOther ) const;
|
2022-06-21 02:22:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool SIM_VALUE_INST<T>::operator==( const SIM_VALUE& aOther ) const
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
const SIM_VALUE_INST<T>* otherValue = dynamic_cast<const SIM_VALUE_INST<T>*>( &aOther );
|
2022-02-21 03:58:31 +00:00
|
|
|
|
2022-05-31 02:55:48 +00:00
|
|
|
if( otherValue )
|
|
|
|
return m_value == otherValue->m_value;
|
2022-02-21 03:58:31 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-21 02:22:52 +00:00
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
template <typename T>
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE_INST<T> operator+( const SIM_VALUE_INST<T>& aLeft, const SIM_VALUE_INST<T>& aRight )
|
|
|
|
{
|
2022-09-03 17:39:49 +00:00
|
|
|
return SIM_VALUE_INST( aLeft.m_value.value() + aRight.m_value.value() );
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template SIM_VALUE_INT operator+( const SIM_VALUE_INT& aLeft,
|
|
|
|
const SIM_VALUE_INT& aRight );
|
|
|
|
template SIM_VALUE_FLOAT operator+( const SIM_VALUE_FLOAT& aLeft,
|
|
|
|
const SIM_VALUE_FLOAT& aRight );
|
2022-06-21 02:22:52 +00:00
|
|
|
|
|
|
|
|
2022-09-18 00:22:51 +00:00
|
|
|
template <typename T>
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE_INST<T> operator-( const SIM_VALUE_INST<T>& aLeft, const SIM_VALUE_INST<T>& aRight )
|
|
|
|
{
|
2022-09-03 17:39:49 +00:00
|
|
|
return SIM_VALUE_INST( aLeft.m_value.value() - aRight.m_value.value() );
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template SIM_VALUE_INT operator-( const SIM_VALUE_INT& aLeft,
|
|
|
|
const SIM_VALUE_INT& aRight );
|
|
|
|
template SIM_VALUE_FLOAT operator-( const SIM_VALUE_FLOAT& aLeft,
|
|
|
|
const SIM_VALUE_FLOAT& aRight );
|
2022-06-21 02:22:52 +00:00
|
|
|
|
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template <typename T>
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE_INST<T> operator*( const SIM_VALUE_INST<T>& aLeft, const SIM_VALUE_INST<T>& aRight )
|
|
|
|
{
|
2022-09-03 17:39:49 +00:00
|
|
|
return SIM_VALUE_INST( aLeft.m_value.value() * aRight.m_value.value() );
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template SIM_VALUE_INT operator*( const SIM_VALUE_INT& aLeft,
|
|
|
|
const SIM_VALUE_INT& aRight );
|
|
|
|
template SIM_VALUE_FLOAT operator*( const SIM_VALUE_FLOAT& aLeft,
|
|
|
|
const SIM_VALUE_FLOAT& aRight );
|
2022-06-21 02:22:52 +00:00
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template <typename T>
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE_INST<T> operator/( const SIM_VALUE_INST<T>& aLeft, const SIM_VALUE_INST<T>& aRight )
|
|
|
|
{
|
2022-09-03 17:39:49 +00:00
|
|
|
return SIM_VALUE_INST( aLeft.m_value.value() / aRight.m_value.value() );
|
2022-06-21 02:22:52 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
template SIM_VALUE_INT operator/( const SIM_VALUE_INT& aLeft,
|
|
|
|
const SIM_VALUE_INT& aRight );
|
|
|
|
template SIM_VALUE_FLOAT operator/( const SIM_VALUE_FLOAT& aLeft,
|
|
|
|
const SIM_VALUE_FLOAT& aRight );
|