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#pragma once
24
25#include <wx/string.h>
26#include <wx/valtext.h>
27
28
38{
39 void FromString( const wxString& aString );
40 wxString ToString() const;
41
42 void UpdateUnits( const wxString& aUnits );
43
45 wxString Range;
46};
47
48
51{
52public:
54 {
55 PFX_FEMTO = -15,
56 PFX_PICO = -12,
65 };
66
68 m_base( 0 ),
70 m_spiceStr( false )
71 {
72 }
73
75 SPICE_VALUE( const wxString& aString );
76
77 SPICE_VALUE( int aInt, UNIT_PREFIX aPrefix = PFX_NONE ) :
78 m_base( aInt ),
79 m_prefix( aPrefix ),
80 m_spiceStr( false )
81 {
82 Normalize();
83 }
84
85 SPICE_VALUE( double aDouble, UNIT_PREFIX aPrefix = PFX_NONE ) :
86 m_base( aDouble ),
87 m_prefix( aPrefix ),
88 m_spiceStr( false )
89 {
90 Normalize();
91 }
92
96 void Normalize();
97
98 double ToNormalizedDouble( wxString* aPrefix );
99
100 double ToDouble() const;
101
105 wxString ToString() const;
106
112 wxString ToString( const SPICE_VALUE_FORMAT& aFormat );
113
117 wxString ToSpiceString() const;
118
123 wxString ToOrigString() const
124 {
125 return m_spiceStr ? ToSpiceString() : ToString();
126 }
127
131 bool IsSpiceString() const
132 {
133 return m_spiceStr;
134 }
135
136 bool operator==( const SPICE_VALUE& aOther ) const
137 {
138 return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
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->ToDouble() < aOther.ToDouble();
149 }
150
151 bool operator>=( const SPICE_VALUE& aOther ) const
152 {
153 return ( *this == aOther || *this > aOther );
154 }
155
156 bool operator<=( const SPICE_VALUE& aOther ) const
157 {
158 return ( *this == aOther || *this < aOther );
159 }
160
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 SPICE_VALUE operator/( const SPICE_VALUE& aOther ) const;
165
167 static void StripZeros( wxString& aString );
168
169 static UNIT_PREFIX ParseSIPrefix( wxChar c );
170
171private:
172 double m_base;
174
177};
178
179
181class SPICE_VALIDATOR : public wxTextValidator
182{
183public:
184 SPICE_VALIDATOR( bool aEmptyAllowed = false ) :
185 m_emptyAllowed( aEmptyAllowed )
186 {
187 }
188
189 wxObject* Clone() const override
190 {
191 return new SPICE_VALIDATOR( *this );
192 }
193
194 bool Validate( wxWindow* aParent ) override;
195
196private:
199};
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 handle Spice way of expressing values (e.g. 10.5 Meg)
Definition spice_value.h:51
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:85
SPICE_VALUE operator-(const SPICE_VALUE &aOther) const
UNIT_PREFIX m_prefix
static UNIT_PREFIX ParseSIPrefix(wxChar c)
SPICE_VALUE(int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
Definition spice_value.h:77
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
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
double ToDouble() const
static void StripZeros(wxString &aString)
Remove redundant zeros from the end of a string.
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 m_spiceStr
Was the value defined using the Spice notation?
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.
Definition spice_value.h:38
wxString ToString() const
void UpdateUnits(const wxString &aUnits)
void FromString(const wxString &aString)