21 #include "ns3/length.h"
35 #include <type_traits>
36 #include <unordered_map>
62 return (value * R::num) /
static_cast<double> (R::den);
74 return value * 0.3048;
86 return value * 3.28084;
135 using Key = std::pair<Unit, Unit>;
136 using Conversion = std::function<double (
double)>;
143 std::size_t operator () (
const Key& key)
const noexcept
145 static_assert (
sizeof(
Unit) <
sizeof(std::size_t),
146 "sizeof(Length::Unit) changed, it must be less than "
147 "sizeof(std::size_t)");
149 int shift =
sizeof(
Unit) * 8;
150 return static_cast<std::size_t
> (key.first) << shift |
151 static_cast<std::size_t
> (key.second);
156 using ConversionTable = std::unordered_map<Key, Conversion, KeyHash>;
158 static ConversionTable CONVERSIONS {
159 { {Unit::Nanometer, Unit::Meter}, ScaleValue<std::nano> },
160 { {Unit::Meter, Unit::Nanometer}, ScaleValue<std::giga> },
161 { {Unit::Micrometer, Unit::Meter}, ScaleValue<std::micro> },
162 { {Unit::Meter, Unit::Micrometer}, ScaleValue<std::mega> },
163 { {Unit::Millimeter, Unit::Meter}, ScaleValue<std::milli> },
164 { {Unit::Meter, Unit::Millimeter}, ScaleValue<std::kilo> },
165 { {Unit::Centimeter, Unit::Meter}, ScaleValue<std::centi> },
166 { {Unit::Meter, Unit::Centimeter}, ScaleValue<std::hecto> },
167 { {Unit::Meter, Unit::Meter}, ScaleValue<std::ratio<1,1> > },
168 { {Unit::Kilometer, Unit::Meter}, ScaleValue<std::kilo> },
169 { {Unit::Meter, Unit::Kilometer}, ScaleValue<std::milli> },
170 { {Unit::NauticalMile, Unit::Meter}, ScaleValue<std::ratio<1852, 1> > },
171 { {Unit::Meter, Unit::NauticalMile}, ScaleValue<std::ratio<1, 1852> > },
172 { {Unit::Inch, Unit::Meter}, USToMeter<std::ratio<1, 12> > },
173 { {Unit::Meter, Unit::Inch}, MeterToUS<std::ratio<12, 1> > },
176 { {Unit::Yard, Unit::Meter}, USToMeter<std::ratio<3, 1> > },
177 { {Unit::Meter, Unit::Yard}, MeterToUS<std::ratio<1, 3> > },
178 { {Unit::Mile, Unit::Meter}, USToMeter<std::ratio<5280, 1> > },
179 { {Unit::Meter, Unit::Mile}, MeterToUS<std::ratio<1, 5280> > }
182 auto iter = CONVERSIONS.find ( Key {fromUnit, toUnit} );
184 if (iter == CONVERSIONS.end ())
187 <<
" -> " << toUnit);
190 return iter->second (value);
224 return static_cast<std::size_t
> (u);
237 std::optional<Length>
244 if (unit.has_value ())
246 return Length (value, *unit);
263 std::istringstream stream (input);
275 if (!unit.has_value ())
277 NS_FATAL_ERROR (
"A Length object could not be constructed from the unit "
278 "string '" << unitString <<
"', because the string is not associated "
279 "with a Length::Unit entry");
294 :
Length (quantity.Value (), quantity.
Unit ())
321 return diff <= tolerance;
329 return !
IsEqual (other, tolerance);
361 return !
IsLess (other, tolerance);
428 return Length ( value, Length::Unit::Meter );
435 return Length ( value, Length::Unit::Meter );
441 double value = left.
GetDouble () * scalar;
442 return Length ( value, Length::Unit::Meter );
448 return right * scalar;
459 return left * (1.0 / scalar);
467 return std::numeric_limits<double>::quiet_NaN ();
476 double value = numerator / denominator;
478 if (std::isnan (value))
486 *remainder =
Length (rem, Length::Unit::Meter);
489 return static_cast<int64_t
> (std::trunc (value));
497 if (std::isnan (rem))
502 return Length (rem, Length::Unit::Meter);
508 using StringTable = std::unordered_map<Length::Unit, std::string, EnumHash>;
510 static const StringTable STRINGS {
511 {Length::Unit::Nanometer,
"nm"},
512 {Length::Unit::Micrometer,
"um"},
513 {Length::Unit::Millimeter,
"mm"},
514 {Length::Unit::Centimeter,
"cm"},
515 {Length::Unit::Meter,
"m"},
516 {Length::Unit::Kilometer,
"km"},
517 {Length::Unit::NauticalMile,
"nmi"},
518 {Length::Unit::Inch,
"in"},
519 {Length::Unit::Foot,
"ft"},
520 {Length::Unit::Yard,
"yd"},
521 {Length::Unit::Mile,
"mi"}
524 auto iter = STRINGS.find (unit);
526 if (iter == STRINGS.end ())
528 NS_FATAL_ERROR (
"A symbol could not be found for Length::Unit with value "
529 << EnumHash ()(unit));
538 using Entry = std::tuple<std::string, std::string>;
539 using StringTable = std::unordered_map<Length::Unit, Entry, EnumHash>;
541 static const StringTable STRINGS {
542 {Length::Unit::Nanometer, Entry{
"nanometer",
"nanometers"}},
543 {Length::Unit::Micrometer, Entry{
"micrometer",
"micrometer"}},
544 {Length::Unit::Millimeter, Entry{
"millimeter",
"millimeters"}},
545 {Length::Unit::Centimeter, Entry{
"centimeter",
"centimeters"}},
546 {Length::Unit::Meter, Entry{
"meter",
"meters"}},
547 {Length::Unit::Kilometer, Entry{
"kilometer",
"kilometers"}},
548 {Length::Unit::NauticalMile, Entry{
"nautical mile",
"nautical miles"}},
549 {Length::Unit::Inch, Entry{
"inch",
"inches"}},
550 {Length::Unit::Foot, Entry{
"foot",
"feet"}},
551 {Length::Unit::Yard, Entry{
"yard",
"yards"}},
552 {Length::Unit::Mile, Entry{
"mile",
"miles"}}
555 auto iter = STRINGS.find (unit);
557 if (iter == STRINGS.end ())
559 NS_FATAL_ERROR (
"A symbol could not be found for Length::Unit with value "
560 << EnumHash ()(unit));
565 return std::get<1> (iter->second);
568 return std::get<0> (iter->second);
571 std::optional<Length::Unit>
574 using UnitTable = std::unordered_map<std::string, Length::Unit>;
576 static const UnitTable UNITS {
577 {
"nm", Length::Unit::Nanometer },
578 {
"nanometer", Length::Unit::Nanometer },
579 {
"nanometers", Length::Unit::Nanometer },
580 {
"nanometre", Length::Unit::Nanometer },
581 {
"nanometres", Length::Unit::Nanometer },
582 {
"um", Length::Unit::Micrometer },
583 {
"micrometer", Length::Unit::Micrometer },
584 {
"micrometers", Length::Unit::Micrometer },
585 {
"micrometre", Length::Unit::Micrometer },
586 {
"micrometres", Length::Unit::Micrometer },
587 {
"mm", Length::Unit::Millimeter },
588 {
"millimeter", Length::Unit::Millimeter },
589 {
"millimeters", Length::Unit::Millimeter },
590 {
"millimetre", Length::Unit::Millimeter },
591 {
"millimetres", Length::Unit::Millimeter },
592 {
"cm", Length::Unit::Centimeter },
593 {
"centimeter", Length::Unit::Centimeter },
594 {
"centimeters", Length::Unit::Centimeter },
595 {
"centimetre", Length::Unit::Centimeter },
596 {
"centimetres", Length::Unit::Centimeter },
597 {
"m", Length::Unit::Meter },
598 {
"meter", Length::Unit::Meter },
599 {
"metre", Length::Unit::Meter },
600 {
"meters", Length::Unit::Meter },
601 {
"metres", Length::Unit::Meter },
602 {
"km", Length::Unit::Kilometer },
603 {
"kilometer", Length::Unit::Kilometer },
604 {
"kilometers", Length::Unit::Kilometer },
605 {
"kilometre", Length::Unit::Kilometer },
606 {
"kilometres", Length::Unit::Kilometer },
607 {
"nmi", Length::Unit::NauticalMile },
608 {
"nauticalmile", Length::Unit::NauticalMile },
609 {
"nauticalmiles", Length::Unit::NauticalMile },
610 {
"in", Length::Unit::Inch },
611 {
"inch", Length::Unit::Inch },
612 {
"inches", Length::Unit::Inch },
613 {
"ft", Length::Unit::Foot },
614 {
"foot", Length::Unit::Foot },
615 {
"feet", Length::Unit::Foot },
616 {
"yd", Length::Unit::Yard },
617 {
"yard", Length::Unit::Yard },
618 {
"yards", Length::Unit::Yard },
619 {
"mi", Length::Unit::Mile },
620 {
"mile", Length::Unit::Mile },
621 {
"miles", Length::Unit::Mile }
625 static auto Normalize = [] (
const std::string& str)
628 output.reserve (str.size ());
630 for (
unsigned char c : str)
633 if (std::isspace (c) )
638 output.push_back (std::tolower (c));
644 unitString = Normalize (unitString);
646 auto iter = UNITS.find (unitString);
648 if (iter != UNITS.end ())
659 stream << l.
As (Length::Unit::Meter);
696 std::tuple<bool, double, std::string>
707 value = std::stod(input, &pos);
709 catch (
const std::exception& e)
711 NS_LOG_ERROR (
"Caught exception while parsing double: " << e.what());
713 return std::make_tuple(
false, 0,
"");
717 while (pos < input.size () && std::isspace(input[pos]))
720 if (pos < input.size ())
722 NS_LOG_LOGIC (
"String has value and symbol, extracting symbol");
725 symbol = input.substr(pos);
728 return std::make_tuple(
true, value, symbol);
734 bool success =
false;
740 auto origFlags = stream.flags ();
741 std::skipws (stream);
748 if (success && symbol.empty ())
750 NS_LOG_LOGIC (
"Temp string only contained value, extracting unit symbol from stream");
758 if (symbol ==
"nautical")
764 symbol.push_back (
' ');
765 symbol.append (temp);
772 stream.flags (origFlags);
780 return Length (value, Length::Unit::Nanometer);
786 return Length (value, Length::Unit::Micrometer);
792 return Length (value, Length::Unit::Millimeter);
798 return Length (value, Length::Unit::Centimeter);
804 return Length (value, Length::Unit::Meter);
810 return Length (value, Length::Unit::Kilometer);
816 return Length (value, Length::Unit::NauticalMile);
822 return Length (value, Length::Unit::Inch);
828 return Length (value, Length::Unit::Foot);
834 return Length (value, Length::Unit::Yard);
840 return Length (value, Length::Unit::Mile);
Functor for hashing Length::Unit values.
An immutable class which represents a value in a specific length unit.
double Value() const
The value of the quantity.
Length::Unit Unit() const
The unit of the quantity.
Represents a length in meters.
void swap(Length &other)
Swap values with another object.
bool IsGreaterOrEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is equal or less in value than this instance.
double GetDouble() const
Current length value.
bool IsGreater(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is less in value than this instance.
double m_value
Length in meters.
bool IsEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is equal in value to this instance.
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
bool IsLessOrEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater or equal in value than this instance.
static std::optional< Length > TryParse(double value, const std::string &unit)
Attempt to construct a Length object from a value and a unit string.
Unit
Units of length in various measurement systems that are supported by the Length class.
Length & operator=(const Length &other)=default
Copy Assignment operator.
Length()
Default Constructor.
bool IsLess(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater in value than this instance.
bool IsNotEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is not equal in value to this instance.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
int64x64_t operator/(const int64x64_t &lhs, const int64x64_t &rhs)
Division operator.
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Length KiloMeters(double value)
Construct a length from a value in the indicated unit.
Length MilliMeters(double value)
Construct a length from a value in the indicated unit.
Length NauticalMiles(double value)
Construct a length from a value in the indicated unit.
std::string ToName(Length::Unit unit, bool plural)
Return the name of the supplied unit.
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Length Yards(double value)
Construct a length from a value in the indicated unit.
Length Feet(double value)
Construct a length from a value in the indicated unit.
Length Mod(const Length &numerator, const Length &denominator)
Calculate the amount remaining after dividing two lengths.
Length MicroMeters(double value)
Construct a length from a value in the indicated unit.
Length Miles(double value)
Construct a length from a value in the indicated unit.
Length Meters(double value)
Construct a length from a value in the indicated unit.
std::string ToSymbol(Length::Unit unit)
Return the symbol of the supplied unit.
Length CentiMeters(double value)
Construct a length from a value in the indicated unit.
int64_t Div(const Length &numerator, const Length &denominator, Length *remainder)
Calculate how many times numerator can be split into denominator sized pieces.
Length NanoMeters(double value)
Construct a length from a value in the indicated unit.
Length Inches(double value)
Construct a length from a value in the indicated unit.
std::optional< Length::Unit > FromString(std::string unitString)
Find the equivalent Length::Unit for a unit string.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Length::Unit Unit
Save some typing by defining a short alias for Length::Unit.
double Convert(const ns3::Length::Quantity &from, ns3::Length::Unit toUnit)
Convert a Length::Quantity to the equivalent value in another unit.
double USToMeter(double value)
Convert a value from a US Customary unit (inches, feet, yards etc.) to meters.
double MeterToFoot(double value)
Convert a value in meters to the equivalent value in feet.
double MeterToUS(double value)
Convert a value from meters to a US Customary unit (inches, feet, yards etc.)
double FootToMeter(double value)
Convert a value in feet to the equivalent value in meters.
double ScaleValue(double value)
Helper function to scale an input value by a given ratio.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::tuple< bool, double, std::string > ParseLengthString(const std::string &input)
This function provides a string parsing method that does not rely on istream, which has been found to...
bool operator==(const EventId &a, const EventId &b)
ATTRIBUTE_HELPER_CPP(Length)
bool operator<(const EventId &a, const EventId &b)
void swap(UUID &uuid1, UUID &uuid2) noexcept
bool operator!=(Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > a, Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > b)
Inequality test.
std::istream & operator>>(std::istream &is, Angles &a)
std::ostream & operator<<(std::ostream &os, const Angles &a)