KiCad PCB EDA Suite
SPICE_VALUE Class Reference

Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)

More...

#include <spice_value.h>

Public Types

enum  UNIT_PREFIX {
  PFX_FEMTO = -15, PFX_PICO = -12, PFX_NANO = -9, PFX_MICRO = -6,
  PFX_MILI = -3, PFX_NONE = 0, PFX_KILO = 3, PFX_MEGA = 6,
  PFX_GIGA = 9, PFX_TERA = 12
}
 

Public Member Functions

 SPICE_VALUE ()
 
 SPICE_VALUE (const wxString &aString)
 

Parses the string to create a Spice value (e.g. 100n)

More...
 
 SPICE_VALUE (int aInt, UNIT_PREFIX aPrefix=PFX_NONE)
 
 SPICE_VALUE (double aDouble, UNIT_PREFIX aPrefix=PFX_NONE)
 
void Normalize ()
 Normalizes the value. More...
 
double ToDouble () const
 
wxString ToString () const
 Returns string value as when converting double to string (e.g. More...
 
wxString ToSpiceString () const
 Returns string value in Spice format (e.g. More...
 
wxString ToOrigString () const
 Returns either a normal string or Spice format string, depending on the original value format. More...
 
bool IsSpiceString () const
 Returns true if the object was initiated with a Spice formatted string value. More...
 
bool operator== (const SPICE_VALUE &aOther) const
 
bool operator> (const SPICE_VALUE &aOther) const
 
bool operator< (const SPICE_VALUE &aOther) const
 
bool operator>= (const SPICE_VALUE &aOther) const
 
bool operator<= (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator- (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator+ (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator* (const SPICE_VALUE &aOther) const
 
SPICE_VALUE operator/ (const SPICE_VALUE &aOther) const
 

Static Private Member Functions

static void stripZeros (wxString &aString)
 

Removes redundant zeros from the end of a string.

More...
 

Private Attributes

double m_base
 
UNIT_PREFIX m_prefix
 
bool m_spiceStr
 

Was the value defined using the Spice notation?

More...
 

Detailed Description

Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)

Definition at line 32 of file spice_value.h.

Member Enumeration Documentation

Enumerator
PFX_FEMTO 
PFX_PICO 
PFX_NANO 
PFX_MICRO 
PFX_MILI 
PFX_NONE 
PFX_KILO 
PFX_MEGA 
PFX_GIGA 
PFX_TERA 

Definition at line 35 of file spice_value.h.

Constructor & Destructor Documentation

SPICE_VALUE::SPICE_VALUE ( )
inline

Definition at line 49 of file spice_value.h.

50  : m_base( 0 ), m_prefix( PFX_NONE ), m_spiceStr( false )
51  {
52  }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE::SPICE_VALUE ( const wxString &  aString)

Parses the string to create a Spice value (e.g. 100n)

Definition at line 36 of file spice_value.cpp.

References dummy(), m_base, m_prefix, m_spiceStr, Normalize(), PFX_FEMTO, PFX_GIGA, PFX_KILO, PFX_MEGA, PFX_MICRO, PFX_MILI, PFX_NANO, PFX_NONE, PFX_PICO, and PFX_TERA.

37 {
38  char buf[8] = { 0, };
39 
40  if( aString.IsEmpty() )
41  throw KI_PARAM_ERROR( _( "Spice value cannot be empty" ) );
42 
43  LOCALE_IO dummy; // All numeric values should be in "C" locale(decimal separator = .)
44 
45  if( sscanf( (const char*) aString.c_str(), "%lf%7s", &m_base, buf ) == 0 )
46  throw KI_PARAM_ERROR( _( "Invalid Spice value string" ) );
47 
48  if( *buf == 0 )
49  {
51  m_spiceStr = false;
52  Normalize();
53  return;
54  }
55 
56  m_spiceStr = true;
57 
58  for( char* bufPtr = buf; *bufPtr; ++bufPtr )
59  *bufPtr = tolower( *bufPtr );
60 
61  if( !strcmp( buf, "meg" ) )
62  {
64  }
65  else
66  {
67  switch( buf[0] )
68  {
69  case 'f': m_prefix = PFX_FEMTO; break;
70  case 'p': m_prefix = PFX_PICO; break;
71  case 'n': m_prefix = PFX_NANO; break;
72  case 'u': m_prefix = PFX_MICRO; break;
73  case 'm': m_prefix = PFX_MILI; break;
74  case 'k': m_prefix = PFX_KILO; break;
75  case 'g': m_prefix = PFX_GIGA; break;
76  case 't': m_prefix = PFX_TERA; break;
77 
78  default:
79  throw KI_PARAM_ERROR( _( "Invalid unit prefix" ) );
80  }
81  }
82 
83  Normalize();
84 }
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:167
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:87
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
class KI_PARAM_ERROR is a class used to hold a translatable error message and may be used when throwi...
Definition: ki_exception.h:45
SPICE_VALUE::SPICE_VALUE ( int  aInt,
UNIT_PREFIX  aPrefix = PFX_NONE 
)
inline

Definition at line 57 of file spice_value.h.

References Normalize().

58  : m_base( aInt ), m_prefix( aPrefix ), m_spiceStr( false )
59  {
60  Normalize();
61  }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:87
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE::SPICE_VALUE ( double  aDouble,
UNIT_PREFIX  aPrefix = PFX_NONE 
)
inline

Definition at line 63 of file spice_value.h.

References Normalize(), ToDouble(), ToSpiceString(), and ToString().

64  : m_base( aDouble ), m_prefix( aPrefix ), m_spiceStr( false )
65  {
66  Normalize();
67  }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:87
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138

Member Function Documentation

bool SPICE_VALUE::IsSpiceString ( ) const
inline

Returns true if the object was initiated with a Spice formatted string value.

Definition at line 98 of file spice_value.h.

References m_spiceStr.

Referenced by TUNER_SLIDER::updateValueText().

99  {
100  return m_spiceStr;
101  }
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
void SPICE_VALUE::Normalize ( )

Normalizes the value.

The unit prefix is picked so the base is (0.001 <= base < 1000).

Definition at line 87 of file spice_value.cpp.

References m_base, and m_prefix.

Referenced by operator*(), operator+(), operator-(), operator/(), and SPICE_VALUE().

88 {
89  while( std::fabs( m_base ) >= 1000.0 )
90  {
91  m_base *= 0.001;
92  m_prefix = (UNIT_PREFIX)( m_prefix + 3 );
93  }
94 
95  while( m_base != 0.0 && std::fabs( m_base ) < 1.000 )
96  {
97  m_base *= 1000.0;
98  m_prefix = (UNIT_PREFIX)( m_prefix - 3 );
99  }
100 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
SPICE_VALUE SPICE_VALUE::operator* ( const SPICE_VALUE aOther) const

Definition at line 207 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

Referenced by operator<=().

208 {
209  SPICE_VALUE res( m_base * aOther.m_base, (UNIT_PREFIX)( m_prefix + aOther.m_prefix ) );
210  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
211  res.Normalize();
212 
213  return res;
214 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE SPICE_VALUE::operator+ ( const SPICE_VALUE aOther) const

Definition at line 145 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

Referenced by operator<=().

146 {
147  int prefixDiff = m_prefix - aOther.m_prefix;
148  SPICE_VALUE res;
149  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
150 
151  // Convert both numbers to a common prefix
152  if( prefixDiff > 0 )
153  {
154  // Switch to the aOther prefix
155  res.m_base = ( m_base * std::pow( 10, prefixDiff ) ) + aOther.m_base;
156  res.m_prefix = aOther.m_prefix;
157  }
158  else if( prefixDiff < 0 )
159  {
160  // Use the current prefix
161  res.m_base = m_base + ( aOther.m_base * std::pow( 10, -prefixDiff ) );
162  res.m_prefix = m_prefix;
163  }
164  else
165  {
166  res.m_base = m_base + aOther.m_base;
167  res.m_prefix = m_prefix; // == aOther.m_prefix
168  }
169 
170  res.Normalize();
171 
172  return res;
173 }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:87
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE SPICE_VALUE::operator- ( const SPICE_VALUE aOther) const

Definition at line 176 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

Referenced by operator<=().

177 {
178  int prefixDiff = m_prefix - aOther.m_prefix;
179  SPICE_VALUE res;
180  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
181 
182  // Convert both numbers to a common prefix
183  if( prefixDiff > 0 )
184  {
185  // Switch to the aOther prefix
186  res.m_base = m_base * std::pow( 10, prefixDiff ) - aOther.m_base;
187  res.m_prefix = aOther.m_prefix;
188  }
189  else if( prefixDiff < 0 )
190  {
191  // Use the current prefix
192  res.m_base = m_base - aOther.m_base * std::pow( 10, -prefixDiff );
193  res.m_prefix = m_prefix;
194  }
195  else
196  {
197  res.m_base = m_base - aOther.m_base;
198  res.m_prefix = m_prefix; // == aOther.m_prefix
199  }
200 
201  res.Normalize();
202 
203  return res;
204 }
double m_base
Definition: spice_value.h:134
void Normalize()
Normalizes the value.
Definition: spice_value.cpp:87
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
SPICE_VALUE SPICE_VALUE::operator/ ( const SPICE_VALUE aOther) const

Definition at line 217 of file spice_value.cpp.

References m_base, m_prefix, m_spiceStr, and Normalize().

Referenced by operator<=().

218 {
219  SPICE_VALUE res( m_base / aOther.m_base, (UNIT_PREFIX)( m_prefix - aOther.m_prefix ) );
220  res.m_spiceStr = m_spiceStr || aOther.m_spiceStr;
221  res.Normalize();
222 
223  return res;
224 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
Helper class to handle Spice way of expressing values (e.g. 10.5 Meg)
Definition: spice_value.h:32
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
bool SPICE_VALUE::operator< ( const SPICE_VALUE aOther) const
inline

Definition at line 113 of file spice_value.h.

References ToDouble().

114  {
115  return this->ToDouble() < aOther.ToDouble();
116  }
double ToDouble() const
bool SPICE_VALUE::operator<= ( const SPICE_VALUE aOther) const
inline

Definition at line 123 of file spice_value.h.

References operator*(), operator+(), operator-(), and operator/().

124  {
125  return ( *this == aOther || *this < aOther );
126  }
bool SPICE_VALUE::operator== ( const SPICE_VALUE aOther) const
inline

Definition at line 103 of file spice_value.h.

References m_base, and m_prefix.

104  {
105  return ( m_prefix == aOther.m_prefix && m_base == aOther.m_base );
106  }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
bool SPICE_VALUE::operator> ( const SPICE_VALUE aOther) const
inline

Definition at line 108 of file spice_value.h.

References ToDouble().

109  {
110  return this->ToDouble() > aOther.ToDouble();
111  }
double ToDouble() const
bool SPICE_VALUE::operator>= ( const SPICE_VALUE aOther) const
inline

Definition at line 118 of file spice_value.h.

119  {
120  return ( *this == aOther || *this > aOther );
121  }
void SPICE_VALUE::stripZeros ( wxString &  aString)
staticprivate

Removes redundant zeros from the end of a string.

Definition at line 227 of file spice_value.cpp.

Referenced by ToSpiceString(), and ToString().

228 {
229  if ( aString.Find( ',' ) >= 0 || aString.Find( '.' ) >= 0 )
230  {
231  while( aString.EndsWith( '0' ) )
232  aString.RemoveLast();
233 
234  if( aString.EndsWith( '.' ) || aString.EndsWith( ',' ) )
235  aString.RemoveLast();
236  }
237 }
double SPICE_VALUE::ToDouble ( ) const

Definition at line 103 of file spice_value.cpp.

References m_base, m_prefix, and PFX_NONE.

Referenced by operator<(), operator>(), SPICE_VALUE(), and ToString().

104 {
105  double res = m_base;
106 
107  if( m_prefix != PFX_NONE )
108  res *= std::pow( 10, (int) m_prefix );
109 
110  return res;
111 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
wxString SPICE_VALUE::ToOrigString ( ) const
inline

Returns either a normal string or Spice format string, depending on the original value format.

Definition at line 90 of file spice_value.h.

References m_spiceStr, ToSpiceString(), and ToString().

Referenced by TUNER_SLIDER::onMaxTextEnter(), TUNER_SLIDER::onMinTextEnter(), TUNER_SLIDER::onSave(), TUNER_SLIDER::onValueTextEnter(), TUNER_SLIDER::SetMax(), TUNER_SLIDER::SetMin(), and TUNER_SLIDER::TUNER_SLIDER().

91  {
92  return m_spiceStr ? ToSpiceString() : ToString();
93  }
wxString ToSpiceString() const
Returns string value in Spice format (e.g.
wxString ToString() const
Returns string value as when converting double to string (e.g.
bool m_spiceStr
Was the value defined using the Spice notation?
Definition: spice_value.h:138
wxString SPICE_VALUE::ToSpiceString ( ) const

Returns string value in Spice format (e.g.

123.3456789k).

Definition at line 122 of file spice_value.cpp.

References m_base, m_prefix, PFX_FEMTO, PFX_GIGA, PFX_KILO, PFX_MEGA, PFX_MICRO, PFX_MILI, PFX_NANO, PFX_NONE, PFX_PICO, PFX_TERA, and stripZeros().

Referenced by SIM_PLOT_FRAME::onCursorUpdate(), DIALOG_SPICE_MODEL::parsePowerSource(), SPICE_VALUE(), ToOrigString(), and TUNER_SLIDER::updateValueText().

123 {
124  wxString res = wxString::FromCDouble( m_base );
125  stripZeros( res );
126 
127  switch( m_prefix )
128  {
129  case PFX_FEMTO: res += "f"; break;
130  case PFX_PICO: res += "p"; break;
131  case PFX_NANO: res += "n"; break;
132  case PFX_MICRO: res += "u"; break;
133  case PFX_MILI: res += "m"; break;
134  case PFX_NONE: break;
135  case PFX_KILO: res += "k"; break;
136  case PFX_MEGA: res += "Meg"; break;
137  case PFX_GIGA: res += "G"; break;
138  case PFX_TERA: res += "T"; break;
139  }
140 
141  return res;
142 }
double m_base
Definition: spice_value.h:134
UNIT_PREFIX m_prefix
Definition: spice_value.h:135
static void stripZeros(wxString &aString)
Removes redundant zeros from the end of a string.
wxString SPICE_VALUE::ToString ( ) const

Returns string value as when converting double to string (e.g.

123456.789).

Definition at line 113 of file spice_value.cpp.

References Format(), stripZeros(), and ToDouble().

Referenced by SPICE_VALUE(), ToOrigString(), and TUNER_SLIDER::updateValueText().

114 {
115  wxString res( wxString::Format( "%.3f", ToDouble() ) );
116  stripZeros( res );
117 
118  return res;
119 }
static void stripZeros(wxString &aString)
Removes redundant zeros from the end of a string.
double ToDouble() const
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205

Member Data Documentation

double SPICE_VALUE::m_base
private
UNIT_PREFIX SPICE_VALUE::m_prefix
private
bool SPICE_VALUE::m_spiceStr
private

Was the value defined using the Spice notation?

Definition at line 138 of file spice_value.h.

Referenced by IsSpiceString(), operator*(), operator+(), operator-(), operator/(), SPICE_VALUE(), and ToOrigString().


The documentation for this class was generated from the following files: