ngscopeclient 0.1-dev+51fbda87c
Unit.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2024 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
37#ifndef Unit_h
38#define Unit_h
39
40#ifndef _WIN32
41#include <locale.h>
42
43#if defined(__APPLE__) || defined(__FreeBSD__)
44#include <xlocale.h>
45#endif
46
47#endif
48
56class Unit
57{
58public:
59
60 enum UnitType
61 {
62 UNIT_FS, //Time. Note that this is not a SI base unit.
63 //Using femtoseconds allows integer math for all known scope timebases,
64 //which keeps things nice and simple.
65 UNIT_HZ, //Frequency
66 UNIT_VOLTS, //Voltage
67 UNIT_AMPS, //Current
68 UNIT_OHMS, //Resistance
69 UNIT_BITRATE, //Bits per second
70 UNIT_PERCENT, //Dimensionless ratio
71 UNIT_DB, //Dimensionless ratio
72 UNIT_DBM, //dB mW (more common than dBW)
73 UNIT_COUNTS, //Dimensionless ratio (histogram)
74 UNIT_COUNTS_SCI, //Dimensionless ratio (histogram, but scientific notation)
75 UNIT_LOG_BER, //Dimensionless ratio (value is a logarithm)
76 UNIT_RATIO_SCI, //Dimensionless ratio (scientific notation)
77 UNIT_SAMPLERATE, //Sample rate (Hz but displayed as S/s)
78 UNIT_SAMPLEDEPTH, //Memory depth (number of samples)
79 UNIT_WATTS, //Power
80 UNIT_UI, //Unit interval (relative to signal bit rate)
81 UNIT_DEGREES, //Angular degrees
82 UNIT_RPM, //Revolutions per minute
83 UNIT_CELSIUS, //Degrees Celsius
84 UNIT_RHO, //Reflection coefficient (dimensionless ratio)
85 UNIT_HEXNUM, //Hexadecimal address or similar
86 UNIT_PM, //Distance or wavelength.
87 //As with femtoseconds, this provides a reasonable range
88 //(1 picometer to +/- 9223 km) of distances using int64's.
89
90 UNIT_MILLIVOLTS, //Hack needed for voltage in the X axis since we use integer coordinates there
91 UNIT_MICROVOLTS, //Hack needed for voltage in the X axis since we use integer coordinates there
92 UNIT_VOLT_SEC, //Hack needed to measure area under the curve in terms of volt-seconds
93
94 UNIT_BYTES, //used mostly for displaying memory usage
95
96 //Did I mention we really need proper algebraic unit support?
97 UNIT_W_M2_NM, //absolute spectral irradiance
98 //(scale by 100 to get uW/cm^2/nm)
99
100 UNIT_W_M2, //absolute irradiance
101 //(scale by 100 to get uW/cm^2)
102
103 UNIT_MICROAMPS, //Another hack for current in the X axis
104
105 UNIT_FARADS, //Capacitance in farads
106
107 //TODO: more here
108 };
109
110 Unit(Unit::UnitType t = UNIT_COUNTS)
111 : m_type(t)
112 {}
113
114 Unit(const std::string& rhs);
115 std::string ToString() const;
116
117 std::string PrettyPrint(double value, int sigfigs = -1, bool useDisplayLocale = true) const;
118 std::string PrettyPrintInt64(int64_t value, int sigfigs = -1, bool useDisplayLocale = true) const;
119
120 std::string PrettyPrintRange(double pixelMin, double pixelMax, double rangeMin, double rangeMax) const;
121
122 double ParseString(const std::string& str, bool useDisplayLocale = true);
123 int64_t ParseStringInt64(const std::string& str, bool useDisplayLocale = true);
124
125 UnitType GetType()
126 { return m_type; }
127
128 bool operator==(const Unit& rhs)
129 { return m_type == rhs.m_type; }
130
131 bool operator!=(const Unit& rhs)
132 { return m_type != rhs.m_type; }
133
134 bool operator!=(UnitType rhs)
135 { return m_type != rhs; }
136
137 Unit operator*(const Unit& rhs);
138 Unit operator/(const Unit& rhs);
139
140 static void SetLocale(const char* locale);
141
142protected:
143 UnitType m_type;
144
145 void GetSIScalingFactor(double num, double& scaleFactor, std::string& prefix) const;
146 void GetUnitSuffix(UnitType type, double num, double& scaleFactor, std::string& prefix, std::string& numprefix, std::string& suffix) const;
147
148#ifdef _WIN32
152 static std::string m_slocale;
153
154#else
158 static locale_t m_locale;
159
163 static locale_t m_defaultLocale;
164#endif
165
166 static void SetPrintingLocale();
167 static void SetDefaultLocale();
168};
169
170#endif
A unit of measurement, plus conversion to pretty-printed output.
Definition: Unit.h:57
static void SetPrintingLocale()
Sets the current locale to the user's selected LC_NUMERIC for printing numbers for display.
Definition: Unit.cpp:1228
void GetSIScalingFactor(double num, double &scaleFactor, std::string &prefix) const
Gets the appropriate SI scaling factor for a number.
Definition: Unit.cpp:229
Unit operator*(const Unit &rhs)
Multiplies two units and calculates the resulting unit.
Definition: Unit.cpp:1171
static locale_t m_locale
The user's requested locale for display.
Definition: Unit.h:158
std::string ToString() const
Converts this unit to a string.
Definition: Unit.cpp:124
Unit operator/(const Unit &rhs)
Divides two units and calculates the resulting unit.
Definition: Unit.cpp:1189
void GetUnitSuffix(UnitType type, double num, double &scaleFactor, std::string &prefix, std::string &numprefix, std::string &suffix) const
Gets the suffix for a unit.
Definition: Unit.cpp:308
static locale_t m_defaultLocale
Handle to the "C" locale, used for interchange.
Definition: Unit.h:163
std::string PrettyPrintInt64(int64_t value, int sigfigs=-1, bool useDisplayLocale=true) const
Prints a value with SI scaling factors.
Definition: Unit.cpp:675
std::string PrettyPrintRange(double pixelMin, double pixelMax, double rangeMin, double rangeMax) const
Prints a value with SI scaling factors and unnecessarily significant sub-pixel digits removed.
Definition: Unit.cpp:781
static void SetDefaultLocale()
Sets the current locale to "C" for interchange.
Definition: Unit.cpp:1240
std::string PrettyPrint(double value, int sigfigs=-1, bool useDisplayLocale=true) const
Prints a value with SI scaling factors.
Definition: Unit.cpp:587
double ParseString(const std::string &str, bool useDisplayLocale=true)
Parses a string based on the supplied unit.
Definition: Unit.cpp:962
int64_t ParseStringInt64(const std::string &str, bool useDisplayLocale=true)
Parses a string based on the supplied unit.
Definition: Unit.cpp:1064