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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SIM_VALUE_H
|
|
|
|
#define SIM_VALUE_H
|
|
|
|
|
|
|
|
#include <wx/string.h>
|
2022-08-25 22:50:47 +00:00
|
|
|
#include <optional>
|
2022-09-03 17:39:49 +00:00
|
|
|
#include <complex>
|
2022-03-09 01:40:59 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <pegtl.hpp>
|
|
|
|
|
2022-02-21 03:58:31 +00:00
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
namespace SIM_VALUE_GRAMMAR
|
|
|
|
{
|
|
|
|
using namespace tao::pegtl;
|
|
|
|
|
|
|
|
enum class NOTATION
|
|
|
|
{
|
|
|
|
SI,
|
|
|
|
SPICE
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class SIM_VALUE
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-04-12 14:37:06 +00:00
|
|
|
using NOTATION = SIM_VALUE_GRAMMAR::NOTATION;
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
// Names like BOOL, INT, FLOAT need to be prefixed or MS Windows compilers will complain (enum
|
|
|
|
// class doesn't help).
|
|
|
|
enum TYPE
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
TYPE_BOOL,
|
|
|
|
TYPE_INT,
|
|
|
|
TYPE_FLOAT,
|
|
|
|
TYPE_COMPLEX,
|
|
|
|
TYPE_STRING,
|
|
|
|
|
|
|
|
TYPE_BOOL_VECTOR,
|
|
|
|
TYPE_INT_VECTOR,
|
|
|
|
TYPE_FLOAT_VECTOR,
|
|
|
|
TYPE_COMPLEX_VECTOR
|
2022-02-21 03:58:31 +00:00
|
|
|
};
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
static std::unique_ptr<SIM_VALUE> Create( TYPE aType, std::string aString );
|
2022-04-12 14:37:06 +00:00
|
|
|
static std::unique_ptr<SIM_VALUE> Create( TYPE aType );
|
2022-02-21 03:58:31 +00:00
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
virtual ~SIM_VALUE() = default;
|
|
|
|
SIM_VALUE() = default;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
virtual bool HasValue() const = 0;
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
void operator=( const std::string& aString );
|
2022-04-12 14:37:06 +00:00
|
|
|
virtual bool operator==( const SIM_VALUE& aOther ) const = 0;
|
2022-06-21 02:22:52 +00:00
|
|
|
bool operator!=( const SIM_VALUE& aOther ) const;
|
2022-02-21 03:58:31 +00:00
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
virtual bool FromString( const std::string& aString, NOTATION aNotation = NOTATION::SI ) = 0;
|
|
|
|
virtual std::string ToString( NOTATION aNotation = NOTATION::SI ) const = 0;
|
|
|
|
std::string ToSpiceString() const { return ToString( NOTATION::SPICE ); }
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
// For parsers that don't accept strings with our suffixes.
|
2022-09-16 04:11:14 +00:00
|
|
|
virtual std::string ToSimpleString() const = 0;
|
2022-02-21 03:58:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2022-06-21 02:22:52 +00:00
|
|
|
class SIM_VALUE_INST : public SIM_VALUE
|
2022-02-21 03:58:31 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE_INST() = default;
|
|
|
|
SIM_VALUE_INST( const T& aValue );
|
2022-02-21 03:58:31 +00:00
|
|
|
|
2022-09-03 17:39:49 +00:00
|
|
|
bool HasValue() const override;
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
// TODO: Don't pass aNotation. Make a FromSpiceString() function instead.
|
2022-09-16 04:11:14 +00:00
|
|
|
bool FromString( const std::string& aString, NOTATION aNotation = NOTATION::SI ) override;
|
|
|
|
std::string ToString( NOTATION aNotation = NOTATION::SI ) const override;
|
|
|
|
std::string ToSimpleString() const override;
|
2022-02-21 03:58:31 +00:00
|
|
|
|
|
|
|
void operator=( const T& aValue );
|
2022-06-21 02:22:52 +00:00
|
|
|
bool operator==( const T& aOther ) const;
|
2022-04-12 14:37:06 +00:00
|
|
|
bool operator==( const SIM_VALUE& aOther ) const override;
|
2022-02-21 03:58:31 +00:00
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
template <typename Type>
|
|
|
|
friend SIM_VALUE_INST<Type> operator+( const SIM_VALUE_INST<Type>& aLeft,
|
|
|
|
const SIM_VALUE_INST<Type>& aRight );
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
friend SIM_VALUE_INST<Type> operator-( const SIM_VALUE_INST<Type>& aLeft,
|
|
|
|
const SIM_VALUE_INST<Type>& aRight );
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
friend SIM_VALUE_INST<Type> operator*( const SIM_VALUE_INST<Type>& aLeft,
|
|
|
|
const SIM_VALUE_INST<Type>& aRight );
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
friend SIM_VALUE_INST<Type> operator/( const SIM_VALUE_INST<Type>& aLeft,
|
|
|
|
const SIM_VALUE_INST<Type>& aRight );
|
|
|
|
|
2022-09-27 20:47:00 +00:00
|
|
|
std::optional<T> Get() { return m_value; };
|
|
|
|
|
2022-02-21 03:58:31 +00:00
|
|
|
private:
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string getMetricSuffix();
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2022-08-25 22:50:47 +00:00
|
|
|
std::optional<T> m_value = std::nullopt;
|
2022-02-21 03:58:31 +00:00
|
|
|
};
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
typedef SIM_VALUE_INST<bool> SIM_VALUE_BOOL;
|
2022-09-18 00:22:51 +00:00
|
|
|
typedef SIM_VALUE_INST<int> SIM_VALUE_INT;
|
2022-06-21 02:22:52 +00:00
|
|
|
typedef SIM_VALUE_INST<double> SIM_VALUE_FLOAT;
|
2022-09-03 17:39:49 +00:00
|
|
|
typedef SIM_VALUE_INST<std::complex<double>> SIM_VALUE_COMPLEX;
|
2022-09-16 04:11:14 +00:00
|
|
|
typedef SIM_VALUE_INST<std::string> SIM_VALUE_STRING;
|
2022-06-21 02:22:52 +00:00
|
|
|
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-04-01 04:30:50 +00:00
|
|
|
namespace SIM_VALUE_GRAMMAR
|
2022-03-09 01:40:59 +00:00
|
|
|
{
|
|
|
|
template <NOTATION Notation>
|
2022-09-16 04:11:14 +00:00
|
|
|
std::string allowedIntChars;
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct digits : plus<tao::pegtl::digit> {}; // For some reason it fails on just "digit".
|
|
|
|
|
|
|
|
struct sign : one<'+', '-'> {};
|
|
|
|
|
2022-05-31 02:55:48 +00:00
|
|
|
struct intPart : seq<opt<sign>, digits> {};
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
//struct fracPartPrefix : one<'.'> {};
|
|
|
|
struct fracPart : digits {};
|
|
|
|
//struct fracPartWithPrefix : seq<fracPartPrefix, fracPart> {};
|
|
|
|
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
template <SIM_VALUE::TYPE ValueType>
|
2022-03-09 01:40:59 +00:00
|
|
|
struct significand;
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct significand<SIM_VALUE::TYPE_FLOAT> :
|
2022-03-09 01:40:59 +00:00
|
|
|
sor<seq<intPart, one<'.'>, fracPart>,
|
|
|
|
seq<intPart, one<'.'>>,
|
|
|
|
intPart,
|
|
|
|
seq<one<'.'>, fracPart>,
|
2022-06-21 02:22:52 +00:00
|
|
|
one<'.'>,
|
|
|
|
one<'-'>> {};
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct significand<SIM_VALUE::TYPE_INT> : intPart {};
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct exponentPrefix : one<'e', 'E'> {};
|
|
|
|
struct exponent : seq<opt<sign>, opt<digits>> {};
|
|
|
|
struct exponentWithPrefix : seq<exponentPrefix, exponent> {};
|
|
|
|
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
|
2022-03-09 01:40:59 +00:00
|
|
|
struct metricSuffix;
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct metricSuffix<SIM_VALUE::TYPE_INT, NOTATION::SI>
|
2022-03-09 01:40:59 +00:00
|
|
|
: one<'k', 'K', 'M', 'G', 'T', 'P', 'E'> {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct metricSuffix<SIM_VALUE::TYPE_INT, NOTATION::SPICE>
|
2022-03-09 01:40:59 +00:00
|
|
|
: sor<TAO_PEGTL_ISTRING( "k" ),
|
|
|
|
TAO_PEGTL_ISTRING( "Meg" ),
|
|
|
|
TAO_PEGTL_ISTRING( "G" ),
|
|
|
|
TAO_PEGTL_ISTRING( "T" )> {};
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct metricSuffix<SIM_VALUE::TYPE_FLOAT, NOTATION::SI>
|
2022-03-09 01:40:59 +00:00
|
|
|
: one<'a', 'f', 'p', 'n', 'u', 'm', 'k', 'K', 'M', 'G', 'T', 'P', 'E'> {};
|
2022-06-21 02:22:52 +00:00
|
|
|
template <> struct metricSuffix<SIM_VALUE::TYPE_FLOAT, NOTATION::SPICE>
|
2022-03-09 01:40:59 +00:00
|
|
|
: sor<TAO_PEGTL_ISTRING( "f" ),
|
|
|
|
TAO_PEGTL_ISTRING( "p" ),
|
|
|
|
TAO_PEGTL_ISTRING( "n" ),
|
|
|
|
TAO_PEGTL_ISTRING( "u" ),
|
2022-05-08 09:54:35 +00:00
|
|
|
TAO_PEGTL_ISTRING( "Meg" ), // "Meg" must be before "m".
|
2022-03-09 01:40:59 +00:00
|
|
|
TAO_PEGTL_ISTRING( "m" ),
|
|
|
|
//TAO_PEGTL_ISTRING( "mil" ),
|
|
|
|
TAO_PEGTL_ISTRING( "k" ),
|
|
|
|
TAO_PEGTL_ISTRING( "G" ),
|
|
|
|
TAO_PEGTL_ISTRING( "T" )> {};
|
|
|
|
|
|
|
|
|
2022-06-21 02:22:52 +00:00
|
|
|
template <NOTATION Notation>
|
|
|
|
struct garbageSuffix;
|
|
|
|
|
|
|
|
template <> struct garbageSuffix<NOTATION::SI> : seq<> {};
|
2022-08-08 15:04:37 +00:00
|
|
|
template <> struct garbageSuffix<NOTATION::SPICE> : star<alpha> {};
|
2022-06-21 02:22:52 +00:00
|
|
|
|
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
|
2022-06-12 03:39:13 +00:00
|
|
|
struct number : seq<opt<significand<ValueType>>,
|
2022-03-09 01:40:59 +00:00
|
|
|
opt<exponentWithPrefix>,
|
2022-06-21 02:22:52 +00:00
|
|
|
opt<metricSuffix<ValueType, Notation>>,
|
|
|
|
garbageSuffix<Notation>> {};
|
2022-03-09 01:40:59 +00:00
|
|
|
|
2022-04-12 14:37:06 +00:00
|
|
|
template <SIM_VALUE::TYPE ValueType, NOTATION Notation>
|
2022-06-21 02:22:52 +00:00
|
|
|
struct numberGrammar : must<number<ValueType, Notation>, tao::pegtl::eof> {};
|
2022-03-09 01:40:59 +00:00
|
|
|
|
|
|
|
|
2022-09-16 04:11:14 +00:00
|
|
|
bool IsValid( const std::string& aString,
|
2022-06-21 02:22:52 +00:00
|
|
|
SIM_VALUE::TYPE aValueType = SIM_VALUE::TYPE_FLOAT,
|
2022-03-09 01:40:59 +00:00
|
|
|
NOTATION aNotation = NOTATION::SI );
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // SIM_VALUE_H
|