ngscopeclient v0.3
Loading...
Searching...
No Matches
Unit.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2026 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
49#define UNIT_OVERLOAD_LABEL "Overload"
50
58class Unit
59{
60public:
61
62 enum UnitType
63 {
64 UNIT_FS, //Time. Note that this is not a SI base unit.
65 //Using femtoseconds allows integer math for all known scope timebases,
66 //which keeps things nice and simple.
67 UNIT_HZ, //Frequency
68 UNIT_VOLTS, //Voltage
69 UNIT_AMPS, //Current
70 UNIT_OHMS, //Resistance
71 UNIT_BITRATE, //Bits per second
72 UNIT_PERCENT, //Dimensionless ratio
73 UNIT_DB, //Dimensionless ratio
74 UNIT_DBM, //dB mW (more common than dBW)
75 UNIT_COUNTS, //Dimensionless ratio (histogram)
76 UNIT_COUNTS_SCI, //Dimensionless ratio (histogram, but scientific notation)
77 UNIT_LOG_BER, //Dimensionless ratio (value is a logarithm)
78 UNIT_RATIO_SCI, //Dimensionless ratio (scientific notation)
79 UNIT_SAMPLERATE, //Sample rate (Hz but displayed as S/s)
80 UNIT_SAMPLEDEPTH, //Memory depth (number of samples)
81 UNIT_WATTS, //Power
82 UNIT_UI, //Unit interval (relative to signal bit rate)
83 UNIT_DEGREES, //Angular degrees
84 UNIT_RPM, //Revolutions per minute
85 UNIT_CELSIUS, //Degrees Celsius
86 UNIT_RHO, //Reflection coefficient (dimensionless ratio)
87 UNIT_HEXNUM, //Hexadecimal address or similar
88 UNIT_PM, //Distance or wavelength.
89 //As with femtoseconds, this provides a reasonable range
90 //(1 picometer to +/- 9223 km) of distances using int64's.
91
92 UNIT_MILLIVOLTS, //Hack needed for voltage in the X axis since we use integer coordinates there
93 UNIT_MICROVOLTS, //Hack needed for voltage in the X axis since we use integer coordinates there
94 UNIT_VOLT_SEC, //Hack needed to measure area under the curve in terms of volt-seconds
95 UNIT_MICROHZ, //Hack needed for accurate timing in FFT filter
96
97 UNIT_BYTES, //used mostly for displaying memory usage
98
99 //Did I mention we really need proper algebraic unit support?
100 UNIT_W_M2_NM, //absolute spectral irradiance
101 //(scale by 100 to get uW/cm^2/nm)
102
103 UNIT_W_M2, //absolute irradiance
104 //(scale by 100 to get uW/cm^2)
105
106 UNIT_MICROAMPS, //Another hack for current in the X axis
107
108 UNIT_FARADS, //Capacitance in farads
109
110 //TODO: more here
111 };
112
113 Unit(Unit::UnitType t = UNIT_COUNTS)
114 : m_type(t)
115 {}
116
117 Unit(const std::string& rhs);
118 std::string ToString() const;
119 std::string ToStringLong() const;
120
121 std::string PrettyPrint(double value, int sigfigs = -1, bool useDisplayLocale = true) const;
122 std::string PrettyPrintTabular(double value, int leftdigits = 4, int rightdigits = 3) const;
123 std::string PrettyPrintInt64(int64_t value, int sigfigs = -1, bool useDisplayLocale = true) const;
124
125 std::string PrettyPrintRange(double pixelMin, double pixelMax, double rangeMin, double rangeMax) const;
126
127 double ParseString(const std::string& str, bool useDisplayLocale = true);
128 int64_t ParseStringInt64(const std::string& str, bool useDisplayLocale = true);
129
130 UnitType GetType()
131 { return m_type; }
132
133 bool operator==(const Unit& rhs)
134 { return m_type == rhs.m_type; }
135
136 bool operator!=(const Unit& rhs)
137 { return m_type != rhs.m_type; }
138
139 bool operator!=(UnitType rhs)
140 { return m_type != rhs; }
141
142 Unit operator*(const Unit& rhs);
143 Unit operator/(const Unit& rhs);
144
145 bool IsLogarithmic();
146
147 static void InitializeLocales();
148
149protected:
150 UnitType m_type;
151
152 void GetSIScalingFactor(double num, double& scaleFactor, std::string& prefix) const;
153 void GetUnitSuffix(UnitType type, double num, double& scaleFactor, std::string& prefix, std::string& numprefix, std::string& suffix) const;
154
155#ifdef _WIN32
159 static std::string m_slocale;
160
161#else
166
171#endif
172
175
176 //public so threads can properly set default locale on initialization, but should not be called by general user code
177public:
178 static void SetPrintingLocale();
179 static void SetDefaultLocale();
180};
181
182#endif
Definition AcceleratorBuffer.h:204
A unit of measurement, plus conversion to pretty-printed output.
Definition Unit.h:59
static void SetPrintingLocale()
Sets the current locale to the user's selected LC_NUMERIC for printing numbers for display.
Definition Unit.cpp:1495
void GetSIScalingFactor(double num, double &scaleFactor, std::string &prefix) const
Gets the appropriate SI scaling factor for a number.
Definition Unit.cpp:265
Unit operator*(const Unit &rhs)
Multiplies two units and calculates the resulting unit.
Definition Unit.cpp:1420
std::string ToStringLong() const
Converts this unit to a string in long format.
Definition Unit.cpp:142
static char m_decimalSeparator
The decimal separator for pretty-printing in the user's selected locale.
Definition Unit.h:174
static locale_t m_locale
The user's requested locale for display.
Definition Unit.h:165
static void InitializeLocales()
Initialize the locales for pretty-printing and file interchange.
Definition Unit.cpp:1466
std::string ToString() const
Converts this unit to a string.
Definition Unit.cpp:157
Unit operator/(const Unit &rhs)
Divides two units and calculates the resulting unit.
Definition Unit.cpp:1438
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:344
std::string PrettyPrintTabular(double value, int leftdigits=4, int rightdigits=3) const
Prints a value with SI scaling factors in a format suitable for tabular display.
Definition Unit.cpp:762
static locale_t m_defaultLocale
Handle to the "C" locale, used for interchange.
Definition Unit.h:170
std::string PrettyPrintInt64(int64_t value, int sigfigs=-1, bool useDisplayLocale=true) const
Prints a value with SI scaling factors.
Definition Unit.cpp:835
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:1009
static void SetDefaultLocale()
Sets the current locale to "C" for interchange.
Definition Unit.cpp:1507
std::string PrettyPrint(double value, int sigfigs=-1, bool useDisplayLocale=true) const
Prints a value with SI scaling factors.
Definition Unit.cpp:660
double ParseString(const std::string &str, bool useDisplayLocale=true)
Parses a string based on the supplied unit.
Definition Unit.cpp:1186
int64_t ParseStringInt64(const std::string &str, bool useDisplayLocale=true)
Parses a string based on the supplied unit.
Definition Unit.cpp:1293