34 #include <type_traits>
35 #include <unordered_map>
63 return (
value *
R::num) /
static_cast<double>(R::den);
76 return value * 0.3048;
89 return value * 3.28084;
141 using Key = std::pair<Unit, Unit>;
142 using Conversion = std::function<double(
double)>;
149 std::size_t operator()(
const Key& key)
const noexcept
151 static_assert(
sizeof(
Unit) <
sizeof(std::size_t),
152 "sizeof(Length::Unit) changed, it must be less than "
153 "sizeof(std::size_t)");
155 int shift =
sizeof(
Unit) * 8;
156 return static_cast<std::size_t
>(key.first) << shift |
157 static_cast<std::size_t
>(key.second);
161 using ConversionTable = std::unordered_map<Key, Conversion, KeyHash>;
163 static ConversionTable CONVERSIONS{
164 {{Unit::Nanometer, Unit::Meter}, ScaleValue<std::nano>},
165 {{Unit::Meter, Unit::Nanometer}, ScaleValue<std::giga>},
166 {{Unit::Micrometer, Unit::Meter}, ScaleValue<std::micro>},
167 {{Unit::Meter, Unit::Micrometer}, ScaleValue<std::mega>},
168 {{Unit::Millimeter, Unit::Meter}, ScaleValue<std::milli>},
169 {{Unit::Meter, Unit::Millimeter}, ScaleValue<std::kilo>},
170 {{Unit::Centimeter, Unit::Meter}, ScaleValue<std::centi>},
171 {{Unit::Meter, Unit::Centimeter}, ScaleValue<std::hecto>},
172 {{Unit::Meter, Unit::Meter}, ScaleValue<std::ratio<1, 1>>},
173 {{Unit::Kilometer, Unit::Meter}, ScaleValue<std::kilo>},
174 {{Unit::Meter, Unit::Kilometer}, ScaleValue<std::milli>},
175 {{Unit::NauticalMile, Unit::Meter}, ScaleValue<std::ratio<1852, 1>>},
176 {{Unit::Meter, Unit::NauticalMile}, ScaleValue<std::ratio<1, 1852>>},
177 {{Unit::Inch, Unit::Meter}, USToMeter<std::ratio<1, 12>>},
178 {{Unit::Meter, Unit::Inch}, MeterToUS<std::ratio<12, 1>>},
181 {{Unit::Yard, Unit::Meter}, USToMeter<std::ratio<3, 1>>},
182 {{Unit::Meter, Unit::Yard}, MeterToUS<std::ratio<1, 3>>},
183 {{Unit::Mile, Unit::Meter}, USToMeter<std::ratio<5280, 1>>},
184 {{Unit::Meter, Unit::Mile}, MeterToUS<std::ratio<1, 5280>>},
187 auto iter = CONVERSIONS.find(Key{fromUnit, toUnit});
189 if (iter == CONVERSIONS.end())
191 NS_FATAL_ERROR(
"No conversion defined for " << fromUnit <<
" -> " << toUnit);
194 return iter->second(
value);
229 return static_cast<std::size_t
>(u);
243 std::optional<Length>
250 if (unit.has_value())
252 return Length(value, *unit);
269 std::istringstream stream(input);
281 if (!unit.has_value())
283 NS_FATAL_ERROR(
"A Length object could not be constructed from the unit "
286 <<
"', because the string is not associated "
287 "with a Length::Unit entry");
329 return diff <= tolerance;
337 return !
IsEqual(other, tolerance);
369 return !
IsLess(other, tolerance);
436 return Length(value, Length::Unit::Meter);
443 return Length(value, Length::Unit::Meter);
449 double value = left.
GetDouble() * scalar;
450 return Length(value, Length::Unit::Meter);
456 return right * scalar;
467 return left * (1.0 / scalar);
475 return std::numeric_limits<double>::quiet_NaN();
484 double value = numerator / denominator;
486 if (std::isnan(value))
494 *remainder =
Length(rem, Length::Unit::Meter);
497 return static_cast<int64_t
>(std::trunc(value));
510 return Length(rem, Length::Unit::Meter);
516 using StringTable = std::unordered_map<Length::Unit, std::string, EnumHash>;
518 static const StringTable STRINGS{
519 {Length::Unit::Nanometer,
"nm"},
520 {Length::Unit::Micrometer,
"um"},
521 {Length::Unit::Millimeter,
"mm"},
522 {Length::Unit::Centimeter,
"cm"},
523 {Length::Unit::Meter,
"m"},
524 {Length::Unit::Kilometer,
"km"},
525 {Length::Unit::NauticalMile,
"nmi"},
526 {Length::Unit::Inch,
"in"},
527 {Length::Unit::Foot,
"ft"},
528 {Length::Unit::Yard,
"yd"},
529 {Length::Unit::Mile,
"mi"},
532 auto iter = STRINGS.find(unit);
534 if (iter == STRINGS.end())
536 NS_FATAL_ERROR(
"A symbol could not be found for Length::Unit with value "
537 << EnumHash()(unit));
546 using Entry = std::tuple<std::string, std::string>;
547 using StringTable = std::unordered_map<Length::Unit, Entry, EnumHash>;
549 static const StringTable STRINGS{
550 {Length::Unit::Nanometer, Entry{
"nanometer",
"nanometers"}},
551 {Length::Unit::Micrometer, Entry{
"micrometer",
"micrometer"}},
552 {Length::Unit::Millimeter, Entry{
"millimeter",
"millimeters"}},
553 {Length::Unit::Centimeter, Entry{
"centimeter",
"centimeters"}},
554 {Length::Unit::Meter, Entry{
"meter",
"meters"}},
555 {Length::Unit::Kilometer, Entry{
"kilometer",
"kilometers"}},
556 {Length::Unit::NauticalMile, Entry{
"nautical mile",
"nautical miles"}},
557 {Length::Unit::Inch, Entry{
"inch",
"inches"}},
558 {Length::Unit::Foot, Entry{
"foot",
"feet"}},
559 {Length::Unit::Yard, Entry{
"yard",
"yards"}},
560 {Length::Unit::Mile, Entry{
"mile",
"miles"}},
563 auto iter = STRINGS.find(unit);
565 if (iter == STRINGS.end())
567 NS_FATAL_ERROR(
"A symbol could not be found for Length::Unit with value "
568 << EnumHash()(unit));
573 return std::get<1>(iter->second);
576 return std::get<0>(iter->second);
579 std::optional<Length::Unit>
582 using UnitTable = std::unordered_map<std::string, Length::Unit>;
584 static const UnitTable UNITS{
585 {
"nm", Length::Unit::Nanometer},
586 {
"nanometer", Length::Unit::Nanometer},
587 {
"nanometers", Length::Unit::Nanometer},
588 {
"nanometre", Length::Unit::Nanometer},
589 {
"nanometres", Length::Unit::Nanometer},
590 {
"um", Length::Unit::Micrometer},
591 {
"micrometer", Length::Unit::Micrometer},
592 {
"micrometers", Length::Unit::Micrometer},
593 {
"micrometre", Length::Unit::Micrometer},
594 {
"micrometres", Length::Unit::Micrometer},
595 {
"mm", Length::Unit::Millimeter},
596 {
"millimeter", Length::Unit::Millimeter},
597 {
"millimeters", Length::Unit::Millimeter},
598 {
"millimetre", Length::Unit::Millimeter},
599 {
"millimetres", Length::Unit::Millimeter},
600 {
"cm", Length::Unit::Centimeter},
601 {
"centimeter", Length::Unit::Centimeter},
602 {
"centimeters", Length::Unit::Centimeter},
603 {
"centimetre", Length::Unit::Centimeter},
604 {
"centimetres", Length::Unit::Centimeter},
605 {
"m", Length::Unit::Meter},
606 {
"meter", Length::Unit::Meter},
607 {
"metre", Length::Unit::Meter},
608 {
"meters", Length::Unit::Meter},
609 {
"metres", Length::Unit::Meter},
610 {
"km", Length::Unit::Kilometer},
611 {
"kilometer", Length::Unit::Kilometer},
612 {
"kilometers", Length::Unit::Kilometer},
613 {
"kilometre", Length::Unit::Kilometer},
614 {
"kilometres", Length::Unit::Kilometer},
615 {
"nmi", Length::Unit::NauticalMile},
616 {
"nauticalmile", Length::Unit::NauticalMile},
617 {
"nauticalmiles", Length::Unit::NauticalMile},
618 {
"in", Length::Unit::Inch},
619 {
"inch", Length::Unit::Inch},
620 {
"inches", Length::Unit::Inch},
621 {
"ft", Length::Unit::Foot},
622 {
"foot", Length::Unit::Foot},
623 {
"feet", Length::Unit::Foot},
624 {
"yd", Length::Unit::Yard},
625 {
"yard", Length::Unit::Yard},
626 {
"yards", Length::Unit::Yard},
627 {
"mi", Length::Unit::Mile},
628 {
"mile", Length::Unit::Mile},
629 {
"miles", Length::Unit::Mile},
633 static auto Normalize = [](
const std::string& str) {
635 output.reserve(str.size());
637 for (
unsigned char c : str)
645 output.push_back(std::tolower(c));
651 unitString = Normalize(unitString);
653 auto iter = UNITS.find(unitString);
655 if (iter != UNITS.end())
666 stream << l.
As(Length::Unit::Meter);
703 std::tuple<bool, double, std::string>
714 value = std::stod(input, &pos);
716 catch (
const std::exception& e)
718 NS_LOG_ERROR(
"Caught exception while parsing double: " << e.what());
720 return std::make_tuple(
false, 0,
"");
724 while (pos < input.size() && std::isspace(input[pos]))
729 if (pos < input.size())
731 NS_LOG_LOGIC(
"String has value and symbol, extracting symbol");
734 symbol = input.substr(pos);
737 return std::make_tuple(
true, value, symbol);
743 bool success =
false;
749 auto origFlags = stream.flags();
757 if (success && symbol.empty())
759 NS_LOG_LOGIC(
"Temp string only contained value, extracting unit symbol from stream");
767 if (symbol ==
"nautical")
773 symbol.push_back(
' ');
781 stream.flags(origFlags);
789 return Length(value, Length::Unit::Nanometer);
795 return Length(value, Length::Unit::Micrometer);
801 return Length(value, Length::Unit::Millimeter);
807 return Length(value, Length::Unit::Centimeter);
813 return Length(value, Length::Unit::Meter);
819 return Length(value, Length::Unit::Kilometer);
825 return Length(value, Length::Unit::NauticalMile);
831 return Length(value, Length::Unit::Inch);
837 return Length(value, Length::Unit::Foot);
843 return Length(value, Length::Unit::Yard);
849 return Length(value, Length::Unit::Mile);
Functor for hashing Length::Unit values.
std::size_t operator()(ns3::Length::Unit u) const noexcept
Produce a hash value for a Length::Unit.
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.
Declaration of ns3::Length class.
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!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
bool operator==(const EventId &a, const EventId &b)
ATTRIBUTE_HELPER_CPP(ValueClassTest)
bool operator<(const EventId &a, const EventId &b)
void swap(UUID &uuid1, UUID &uuid2) noexcept
std::istream & operator>>(std::istream &is, Angles &a)
std::ostream & operator<<(std::ostream &os, const Angles &a)