KiCad PCB EDA Suite
Loading...
Searching...
No Matches
spice_value.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016 CERN
5 * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, you may find one here:
21 * https://www.gnu.org/licenses/gpl-3.0.html
22 * or you may search the http://www.gnu.org website for the version 3 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#ifndef SPICE_VALUE_H
28#define SPICE_VALUE_H
29
30#include <wx/string.h>
31#include <wx/valtext.h>
32
33
43{
44 void FromString( const wxString& aString );
45 wxString ToString() const;
46
47 void UpdateUnits( const wxString& aUnits );
48
50 wxString Range;
51};
52
53
56{
57public:
59 {
60 PFX_FEMTO = -15,
61 PFX_PICO = -12,
69 PFX_TERA = 12
70 };
71
73 : m_base( 0 ), m_prefix( PFX_NONE ), m_spiceStr( false )
74 {
75 }
76
78 SPICE_VALUE( const wxString& aString );
79
80 SPICE_VALUE( int aInt, UNIT_PREFIX aPrefix = PFX_NONE )
81 : m_base( aInt ), m_prefix( aPrefix ), m_spiceStr( false )
82 {
83 Normalize();
84 }
85
86 SPICE_VALUE( double aDouble, UNIT_PREFIX aPrefix = PFX_NONE )
87 : m_base( aDouble ), m_prefix( aPrefix ), m_spiceStr( false )
88 {
89 Normalize();
90 }
91
95 void Normalize();
96
97 double ToNormalizedDouble( wxString* aPrefix );
98
99 double ToDouble() const;
100
104 wxString ToString() const;
105
111 wxString ToString( const SPICE_VALUE_FORMAT& aFormat );
112
116 wxString ToSpiceString() const;
117
122 wxString ToOrigString() const
123 {
124 return m_spiceStr ? ToSpiceString() : ToString();
125 }
126
130 bool IsSpiceString() const
131 {
132 return m_spiceStr;
133 }
134
135 bool operator==( const SPICE_VALUE& aOther ) const
136 {
137 return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
138 }
139
140 bool operator>( const SPICE_VALUE& aOther ) const
141 {
142 return this->ToDouble() > aOther.ToDouble();
143 }
144
145 bool operator<( const SPICE_VALUE& aOther ) const
146 {
147 return this->ToDouble() < aOther.ToDouble();
148 }
149
150 bool operator>=( const SPICE_VALUE& aOther ) const
151 {
152 return ( *this == aOther || *this > aOther );
153 }
154
155 bool operator<=( const SPICE_VALUE& aOther ) const
156 {
157 return ( *this == aOther || *this < aOther );
158 }
159
160 SPICE_VALUE operator-( const SPICE_VALUE& aOther ) const;
161 SPICE_VALUE operator+( const SPICE_VALUE& aOther ) const;
162 SPICE_VALUE operator*( const SPICE_VALUE& aOther ) const;
163 SPICE_VALUE operator/( const SPICE_VALUE& aOther ) const;
164
166 static void StripZeros( wxString& aString );
167
168 static UNIT_PREFIX ParseSIPrefix( wxChar c );
169
170private:
171 double m_base;
173
176};
177
178
180class SPICE_VALIDATOR : public wxTextValidator
181{
182public:
183 SPICE_VALIDATOR( bool aEmptyAllowed = false )
184 : m_emptyAllowed( aEmptyAllowed )
185 {
186 }
187
188 wxObject* Clone() const override
189 {
190 return new SPICE_VALIDATOR( *this );
191 }
192
193 bool Validate( wxWindow* aParent ) override;
194
195private:
198};
199
200#endif /* SPICE_VALUE_H */
wxObject * Clone() const override
Definition: spice_value.h:188
bool m_emptyAllowed
< Is it valid to get an empty value?
Definition: spice_value.h:197
bool Validate(wxWindow *aParent) override
SPICE_VALIDATOR(bool aEmptyAllowed=false)
Definition: spice_value.h:183
Helper class to recognize Spice formatted values.
Definition: spice_value.h:56
SPICE_VALUE operator+(const SPICE_VALUE &aOther) const
double m_base
Definition: spice_value.h:171
bool operator>(const SPICE_VALUE &aOther) const
Definition: spice_value.h:140
SPICE_VALUE(double aDouble, UNIT_PREFIX aPrefix=PFX_NONE)
Definition: spice_value.h:86
SPICE_VALUE operator-(const SPICE_VALUE &aOther) const
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:172
static UNIT_PREFIX ParseSIPrefix(wxChar c)
SPICE_VALUE(int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
Definition: spice_value.h:80
void Normalize()
Normalize the value.
double ToNormalizedDouble(wxString *aPrefix)
wxString ToString() const
Return string value as when converting double to string (e.g.
SPICE_VALUE operator/(const SPICE_VALUE &aOther) const
Remove redundant zeros from the end of a string.
wxString ToSpiceString() const
Return string value in Spice format (e.g.
SPICE_VALUE operator*(const SPICE_VALUE &aOther) const
bool operator==(const SPICE_VALUE &aOther) const
Definition: spice_value.h:135
bool operator>=(const SPICE_VALUE &aOther) const
Definition: spice_value.h:150
SPICE_VALUE()
Parses the string to create a Spice value (e.g. 100n)
Definition: spice_value.h:72
double ToDouble() const
static void StripZeros(wxString &aString)
wxString ToOrigString() const
Return either a normal string or Spice format string, depending on the original value format.
Definition: spice_value.h:122
bool operator<(const SPICE_VALUE &aOther) const
Definition: spice_value.h:145
bool operator<=(const SPICE_VALUE &aOther) const
Definition: spice_value.h:155
bool m_spiceStr
Definition: spice_value.h:175
bool IsSpiceString() const
Return true if the object was initiated with a Spice formatted string value.
Definition: spice_value.h:130
A SPICE_VALUE_FORMAT holds precision and range info for formatting values.Helper class to handle Spic...
Definition: spice_value.h:43
wxString ToString() const
Definition: spice_value.cpp:58
void UpdateUnits(const wxString &aUnits)
Definition: spice_value.cpp:64
void FromString(const wxString &aString)
Definition: spice_value.cpp:41