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 The 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifndef SPICE_VALUE_H
24#define SPICE_VALUE_H
25
26#include <wx/string.h>
27#include <wx/valtext.h>
28
29
39{
40 void FromString( const wxString& aString );
41 wxString ToString() const;
42
43 void UpdateUnits( const wxString& aUnits );
44
46 wxString Range;
47};
48
49
52{
53public:
55 {
56 PFX_FEMTO = -15,
57 PFX_PICO = -12,
66 };
67
69 : m_base( 0 ), m_prefix( PFX_NONE ), m_spiceStr( false )
70 {
71 }
72
74 SPICE_VALUE( const wxString& aString );
75
76 SPICE_VALUE( int aInt, UNIT_PREFIX aPrefix = PFX_NONE )
77 : m_base( aInt ), m_prefix( aPrefix ), m_spiceStr( false )
78 {
79 Normalize();
80 }
81
82 SPICE_VALUE( double aDouble, UNIT_PREFIX aPrefix = PFX_NONE )
83 : m_base( aDouble ), m_prefix( aPrefix ), m_spiceStr( false )
84 {
85 Normalize();
86 }
87
91 void Normalize();
92
93 double ToNormalizedDouble( wxString* aPrefix );
94
95 double ToDouble() const;
96
100 wxString ToString() const;
101
107 wxString ToString( const SPICE_VALUE_FORMAT& aFormat );
108
112 wxString ToSpiceString() const;
113
118 wxString ToOrigString() const
119 {
120 return m_spiceStr ? ToSpiceString() : ToString();
121 }
122
126 bool IsSpiceString() const
127 {
128 return m_spiceStr;
129 }
130
131 bool operator==( const SPICE_VALUE& aOther ) const
132 {
133 return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
134 }
135
136 bool operator>( const SPICE_VALUE& aOther ) const
137 {
138 return this->ToDouble() > aOther.ToDouble();
139 }
140
141 bool operator<( const SPICE_VALUE& aOther ) const
142 {
143 return this->ToDouble() < aOther.ToDouble();
144 }
145
146 bool operator>=( const SPICE_VALUE& aOther ) const
147 {
148 return ( *this == aOther || *this > aOther );
149 }
150
151 bool operator<=( const SPICE_VALUE& aOther ) const
152 {
153 return ( *this == aOther || *this < aOther );
154 }
155
156 SPICE_VALUE operator-( const SPICE_VALUE& aOther ) const;
157 SPICE_VALUE operator+( const SPICE_VALUE& aOther ) const;
158 SPICE_VALUE operator*( const SPICE_VALUE& aOther ) const;
159 SPICE_VALUE operator/( const SPICE_VALUE& aOther ) const;
160
162 static void StripZeros( wxString& aString );
163
164 static UNIT_PREFIX ParseSIPrefix( wxChar c );
165
166private:
167 double m_base;
169
172};
173
174
176class SPICE_VALIDATOR : public wxTextValidator
177{
178public:
179 SPICE_VALIDATOR( bool aEmptyAllowed = false )
180 : m_emptyAllowed( aEmptyAllowed )
181 {
182 }
183
184 wxObject* Clone() const override
185 {
186 return new SPICE_VALIDATOR( *this );
187 }
188
189 bool Validate( wxWindow* aParent ) override;
190
191private:
194};
195
196#endif /* SPICE_VALUE_H */
wxObject * Clone() const override
bool m_emptyAllowed
< Is it valid to get an empty value?
bool Validate(wxWindow *aParent) override
SPICE_VALIDATOR(bool aEmptyAllowed=false)
Helper class to recognize Spice formatted values.
Definition spice_value.h:52
SPICE_VALUE operator+(const SPICE_VALUE &aOther) const
double m_base
bool operator>(const SPICE_VALUE &aOther) const
SPICE_VALUE(double aDouble, UNIT_PREFIX aPrefix=PFX_NONE)
Definition spice_value.h:82
SPICE_VALUE operator-(const SPICE_VALUE &aOther) const
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
static UNIT_PREFIX ParseSIPrefix(wxChar c)
SPICE_VALUE(int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
Definition spice_value.h:76
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
bool operator>=(const SPICE_VALUE &aOther) const
SPICE_VALUE()
Parses the string to create a Spice value (e.g. 100n)
Definition spice_value.h:68
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.
bool operator<(const SPICE_VALUE &aOther) const
bool operator<=(const SPICE_VALUE &aOther) const
bool IsSpiceString() const
Return true if the object was initiated with a Spice formatted string value.
A SPICE_VALUE_FORMAT holds precision and range info for formatting values.Helper class to handle Spic...
Definition spice_value.h:39
wxString ToString() const
void UpdateUnits(const wxString &aUnits)
void FromString(const wxString &aString)