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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 
35 {
36 public:
38  {
39  PFX_FEMTO = -15,
40  PFX_PICO = -12,
41  PFX_NANO = -9,
42  PFX_MICRO = -6,
43  PFX_MILI = -3,
44  PFX_NONE = 0,
45  PFX_KILO = 3,
46  PFX_MEGA = 6,
47  PFX_GIGA = 9,
48  PFX_TERA = 12
49  };
50 
52  : m_base( 0 ), m_prefix( PFX_NONE ), m_spiceStr( false )
53  {
54  }
55 
57  SPICE_VALUE( const wxString& aString );
58 
59  SPICE_VALUE( int aInt, UNIT_PREFIX aPrefix = PFX_NONE )
60  : m_base( aInt ), m_prefix( aPrefix ), m_spiceStr( false )
61  {
62  Normalize();
63  }
64 
65  SPICE_VALUE( double aDouble, UNIT_PREFIX aPrefix = PFX_NONE )
66  : m_base( aDouble ), m_prefix( aPrefix ), m_spiceStr( false )
67  {
68  Normalize();
69  }
70 
74  void Normalize();
75 
76  double ToDouble() const;
77 
81  wxString ToString() const;
82 
86  wxString ToSpiceString() const;
87 
92  wxString ToOrigString() const
93  {
94  return m_spiceStr ? ToSpiceString() : ToString();
95  }
96 
100  bool IsSpiceString() const
101  {
102  return m_spiceStr;
103  }
104 
105  bool operator==( const SPICE_VALUE& aOther ) const
106  {
107  return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
108  }
109 
110  bool operator>( const SPICE_VALUE& aOther ) const
111  {
112  return this->ToDouble() > aOther.ToDouble();
113  }
114 
115  bool operator<( const SPICE_VALUE& aOther ) const
116  {
117  return this->ToDouble() < aOther.ToDouble();
118  }
119 
120  bool operator>=( const SPICE_VALUE& aOther ) const
121  {
122  return ( *this == aOther || *this > aOther );
123  }
124 
125  bool operator<=( const SPICE_VALUE& aOther ) const
126  {
127  return ( *this == aOther || *this < aOther );
128  }
129 
130  SPICE_VALUE operator-( const SPICE_VALUE& aOther ) const;
131  SPICE_VALUE operator+( const SPICE_VALUE& aOther ) const;
132  SPICE_VALUE operator*( const SPICE_VALUE& aOther ) const;
133  SPICE_VALUE operator/( const SPICE_VALUE& aOther ) const;
134 
135 private:
137  static void stripZeros( wxString& aString );
138 
139  double m_base;
141 
144 };
145 
146 
148 class SPICE_VALIDATOR : public wxTextValidator
149 {
150 public:
151  SPICE_VALIDATOR( bool aEmptyAllowed = false )
152  : m_emptyAllowed( aEmptyAllowed )
153  {
154  }
155 
156  wxObject* Clone() const override
157  {
158  return new SPICE_VALIDATOR( *this );
159  }
160 
161  bool Validate( wxWindow* aParent ) override;
162 
163 private:
166 };
167 
168 #endif /* SPICE_VALUE_H */
SPICE_VALUE operator-(const SPICE_VALUE &aOther) const
wxString ToSpiceString() const
Return string value in Spice format (e.g.
bool Validate(wxWindow *aParent) override
double m_base
Definition: spice_value.h:139
bool m_emptyAllowed
< Is it valid to get an empty value?
Definition: spice_value.h:165
double ToDouble() const
void Normalize()
Normalize the value.
Definition: spice_value.cpp:88
bool operator>(const SPICE_VALUE &aOther) const
Definition: spice_value.h:110
wxString ToOrigString() const
Return either a normal string or Spice format string, depending on the original value format.
Definition: spice_value.h:92
bool IsSpiceString() const
Return true if the object was initiated with a Spice formatted string value.
Definition: spice_value.h:100
wxString ToString() const
Return string value as when converting double to string (e.g.
bool operator>=(const SPICE_VALUE &aOther) const
Definition: spice_value.h:120
bool operator<(const SPICE_VALUE &aOther) const
Definition: spice_value.h:115
UNIT_PREFIX m_prefix
Was the value defined using the Spice notation?
Definition: spice_value.h:140
static void stripZeros(wxString &aString)
< Remove redundant zeros from the end of a string.
< Helper class to handle Spice way of expressing values (e.g. 10.5 Meg) Helper class to recognize Spi...
Definition: spice_value.h:34
SPICE_VALIDATOR(bool aEmptyAllowed=false)
Definition: spice_value.h:151
SPICE_VALUE(double aDouble, UNIT_PREFIX aPrefix=PFX_NONE)
Definition: spice_value.h:65
SPICE_VALUE operator *(const SPICE_VALUE &aOther) const
SPICE_VALUE(int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
Definition: spice_value.h:59
SPICE_VALUE operator+(const SPICE_VALUE &aOther) const
bool operator==(const SPICE_VALUE &aOther) const
Definition: spice_value.h:105
wxObject * Clone() const override
Definition: spice_value.h:156
SPICE_VALUE operator/(const SPICE_VALUE &aOther) const
bool m_spiceStr
Definition: spice_value.h:143
bool operator<=(const SPICE_VALUE &aOther) const
Definition: spice_value.h:125
SPICE_VALUE()
Parses the string to create a Spice value (e.g. 100n)
Definition: spice_value.h:51