KiCad PCB EDA Suite
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  * @author Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * https://www.gnu.org/licenses/gpl-3.0.html
20  * or you may search the http://www.gnu.org website for the version 3 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef SPICE_VALUE_H
26 #define SPICE_VALUE_H
27 
28 #include <wx/string.h>
29 #include <wx/valtext.h>
30 
33 {
34 public:
36  {
37  PFX_FEMTO = -15,
38  PFX_PICO = -12,
39  PFX_NANO = -9,
40  PFX_MICRO = -6,
41  PFX_MILI = -3,
42  PFX_NONE = 0,
43  PFX_KILO = 3,
44  PFX_MEGA = 6,
45  PFX_GIGA = 9,
46  PFX_TERA = 12
47  };
48 
50  : m_base( 0 ), m_prefix( PFX_NONE ), m_spiceStr( false )
51  {
52  }
53 
55  SPICE_VALUE( const wxString& aString );
56 
57  SPICE_VALUE( int aInt, UNIT_PREFIX aPrefix = PFX_NONE )
58  : m_base( aInt ), m_prefix( aPrefix ), m_spiceStr( false )
59  {
60  Normalize();
61  }
62 
63  SPICE_VALUE( double aDouble, UNIT_PREFIX aPrefix = PFX_NONE )
64  : m_base( aDouble ), m_prefix( aPrefix ), m_spiceStr( false )
65  {
66  Normalize();
67  }
68 
72  void Normalize();
73 
74  double ToDouble() const;
75 
79  wxString ToString() const;
80 
84  wxString ToSpiceString() const;
85 
90  wxString ToOrigString() const
91  {
92  return m_spiceStr ? ToSpiceString() : ToString();
93  }
94 
98  bool IsSpiceString() const
99  {
100  return m_spiceStr;
101  }
102 
103  bool operator==( const SPICE_VALUE& aOther ) const
104  {
105  return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
106  }
107 
108  bool operator>( const SPICE_VALUE& aOther ) const
109  {
110  return this->ToDouble() > aOther.ToDouble();
111  }
112 
113  bool operator<( const SPICE_VALUE& aOther ) const
114  {
115  return this->ToDouble() < aOther.ToDouble();
116  }
117 
118  bool operator>=( const SPICE_VALUE& aOther ) const
119  {
120  return ( *this == aOther || *this > aOther );
121  }
122 
123  bool operator<=( const SPICE_VALUE& aOther ) const
124  {
125  return ( *this == aOther || *this < aOther );
126  }
127 
128  SPICE_VALUE operator-( const SPICE_VALUE& aOther ) const;
129  SPICE_VALUE operator+( const SPICE_VALUE& aOther ) const;
130  SPICE_VALUE operator*( const SPICE_VALUE& aOther ) const;
131  SPICE_VALUE operator/( const SPICE_VALUE& aOther ) const;
132 
133 private:
134  double m_base;
136 
139 
141  static void stripZeros( wxString& aString );
142 };
143 
144 
146 class SPICE_VALIDATOR : public wxTextValidator
147 {
148 public:
149  SPICE_VALIDATOR( bool aEmptyAllowed = false )
150  : m_emptyAllowed( aEmptyAllowed )
151  {
152  }
153 
154  wxObject* Clone() const override
155  {
156  return new SPICE_VALIDATOR( *this );
157  }
158 
159  bool Validate( wxWindow* aParent ) override;
160 
161 private:
164 };
165 
166 #endif /* SPICE_VALUE_H */
bool Validate(wxWindow *aParent) override
SPICE_VALUE operator/(const SPICE_VALUE &aOther) const
wxString ToSpiceString() const
Returns string value in Spice format (e.g.
double m_base
Definition: spice_value.h:134
bool m_emptyAllowed
Is it valid to get an empty value?
Definition: spice_value.h:163
bool operator==(const SPICE_VALUE &aOther) const
Definition: spice_value.h:103
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:86
SPICE_VALUE operator*(const SPICE_VALUE &aOther) const
bool IsSpiceString() const
Returns true if the object was initiated with a Spice formatted string value.
Definition: spice_value.h:98
bool operator>(const SPICE_VALUE &aOther) const
Definition: spice_value.h:108
bool operator<(const SPICE_VALUE &aOther) const
Definition: spice_value.h:113
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
static void stripZeros(wxString &aString)
Removes redundant zeros from the end of a string.
SPICE_VALUE operator+(const SPICE_VALUE &aOther) const
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
SPICE_VALIDATOR(bool aEmptyAllowed=false)
Definition: spice_value.h:149
SPICE_VALUE(double aDouble, UNIT_PREFIX aPrefix=PFX_NONE)
Definition: spice_value.h:63
wxString ToString() const
Returns string value as when converting double to string (e.g.
Helper class to recognize Spice formatted values
Definition: spice_value.h:146
SPICE_VALUE(int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
Definition: spice_value.h:57
wxObject * Clone() const override
Definition: spice_value.h:154
double ToDouble() const
bool operator<=(const SPICE_VALUE &aOther) const
Definition: spice_value.h:123
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE operator-(const SPICE_VALUE &aOther) const
bool operator>=(const SPICE_VALUE &aOther) const
Definition: spice_value.h:118
wxString ToOrigString() const
Returns either a normal string or Spice format string, depending on the original value format...
Definition: spice_value.h:90