/* * 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 #include #include #include #include namespace SIM_VALUE_GRAMMAR { using namespace tao::pegtl; enum class NOTATION { SI, SPICE }; } class SIM_VALUE { public: using NOTATION = SIM_VALUE_GRAMMAR::NOTATION; // Names like BOOL, INT, FLOAT need to be prefixed or MS Windows compilers will complain (enum // class doesn't help). enum TYPE { TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_COMPLEX, TYPE_STRING, TYPE_BOOL_VECTOR, TYPE_INT_VECTOR, TYPE_FLOAT_VECTOR, TYPE_COMPLEX_VECTOR }; static std::unique_ptr Create( TYPE aType, std::string aString ); static std::unique_ptr Create( TYPE aType ); virtual ~SIM_VALUE() = default; SIM_VALUE() = default; virtual bool HasValue() const = 0; void operator=( const std::string& aString ); virtual bool operator==( const SIM_VALUE& aOther ) const = 0; bool operator!=( const SIM_VALUE& aOther ) const; 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 ); } // For parsers that don't accept strings with our suffixes. virtual std::string ToSimpleString() const = 0; }; template class SIM_VALUE_INST : public SIM_VALUE { public: SIM_VALUE_INST() = default; SIM_VALUE_INST( const T& aValue ); bool HasValue() const override; // TODO: Don't pass aNotation. Make a FromSpiceString() function instead. 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; void operator=( const T& aValue ); bool operator==( const T& aOther ) const; bool operator==( const SIM_VALUE& aOther ) const override; template friend SIM_VALUE_INST operator+( const SIM_VALUE_INST& aLeft, const SIM_VALUE_INST& aRight ); template friend SIM_VALUE_INST operator-( const SIM_VALUE_INST& aLeft, const SIM_VALUE_INST& aRight ); template friend SIM_VALUE_INST operator*( const SIM_VALUE_INST& aLeft, const SIM_VALUE_INST& aRight ); template friend SIM_VALUE_INST operator/( const SIM_VALUE_INST& aLeft, const SIM_VALUE_INST& aRight ); private: std::string getMetricSuffix(); std::optional m_value = std::nullopt; }; typedef SIM_VALUE_INST SIM_VALUE_BOOL; typedef SIM_VALUE_INST SIM_VALUE_INT; typedef SIM_VALUE_INST SIM_VALUE_FLOAT; typedef SIM_VALUE_INST> SIM_VALUE_COMPLEX; typedef SIM_VALUE_INST SIM_VALUE_STRING; namespace SIM_VALUE_GRAMMAR { template std::string allowedIntChars; struct digits : plus {}; // For some reason it fails on just "digit". struct sign : one<'+', '-'> {}; struct intPart : seq, digits> {}; //struct fracPartPrefix : one<'.'> {}; struct fracPart : digits {}; //struct fracPartWithPrefix : seq {}; template struct significand; template <> struct significand : sor, fracPart>, seq>, intPart, seq, fracPart>, one<'.'>, one<'-'>> {}; template <> struct significand : intPart {}; struct exponentPrefix : one<'e', 'E'> {}; struct exponent : seq, opt> {}; struct exponentWithPrefix : seq {}; template struct metricSuffix; template <> struct metricSuffix : one<'k', 'K', 'M', 'G', 'T', 'P', 'E'> {}; template <> struct metricSuffix : sor {}; template <> struct metricSuffix : one<'a', 'f', 'p', 'n', 'u', 'm', 'k', 'K', 'M', 'G', 'T', 'P', 'E'> {}; template <> struct metricSuffix : sor {}; template struct garbageSuffix; template <> struct garbageSuffix : seq<> {}; template <> struct garbageSuffix : star {}; template struct number : seq>, opt, opt>, garbageSuffix> {}; template struct numberGrammar : must, tao::pegtl::eof> {}; bool IsValid( const std::string& aString, SIM_VALUE::TYPE aValueType = SIM_VALUE::TYPE_FLOAT, NOTATION aNotation = NOTATION::SI ); } #endif // SIM_VALUE_H