21#include <unordered_map>
48 return (value * R::num) /
static_cast<double>(R::den);
61 return value * 0.3048;
74 return value * 3.28084;
126 using Key = std::pair<Unit, Unit>;
127 using Conversion = std::function<
double(
double)>;
134 std::size_t operator()(
const Key& key)
const noexcept
136 static_assert(
sizeof(
Unit) <
sizeof(std::size_t),
137 "sizeof(Length::Unit) changed, it must be less than "
138 "sizeof(std::size_t)");
140 int shift =
sizeof(
Unit) * 8;
141 return static_cast<std::size_t
>(key.first) << shift |
142 static_cast<std::size_t
>(key.second);
146 using ConversionTable = std::unordered_map<Key, Conversion, KeyHash>;
148 static ConversionTable CONVERSIONS{
172 auto iter = CONVERSIONS.find(Key{fromUnit, toUnit});
174 if (iter == CONVERSIONS.end())
176 NS_FATAL_ERROR(
"No conversion defined for " << fromUnit <<
" -> " << toUnit);
179 return iter->second(value);
214 return static_cast<std::size_t
>(
u);
235 if (unit.has_value())
237 return Length(value, *unit);
254 std::istringstream stream(input);
266 if (!unit.has_value())
268 NS_FATAL_ERROR(
"A Length object could not be constructed from the unit "
271 <<
"', because the string is not associated "
272 "with a Length::Unit entry");
275 m_value = Convert(value, *unit, Length::Unit::Meter);
283 m_value = Convert(value, unit, Length::Unit::Meter);
297 m_value = Convert(
q, Length::Unit::Meter);
314 return diff <= tolerance;
322 return !
IsEqual(other, tolerance);
354 return !
IsLess(other, tolerance);
376 double value = Convert(
m_value, Length::Unit::Meter, unit);
421 return Length(value, Length::Unit::Meter);
428 return Length(value, Length::Unit::Meter);
434 double value = left.
GetDouble() * scalar;
435 return Length(value, Length::Unit::Meter);
441 return right * scalar;
452 return left * (1.0 / scalar);
460 return std::numeric_limits<double>::quiet_NaN();
469 double value = numerator / denominator;
471 if (std::isnan(value))
482 return static_cast<int64_t
>(std::trunc(value));
501 using StringTable = std::unordered_map<Length::Unit, std::string, EnumHash>;
503 static const StringTable STRINGS{
504 {Length::Unit::Nanometer,
"nm"},
505 {Length::Unit::Micrometer,
"um"},
506 {Length::Unit::Millimeter,
"mm"},
507 {Length::Unit::Centimeter,
"cm"},
508 {Length::Unit::Meter,
"m"},
509 {Length::Unit::Kilometer,
"km"},
510 {Length::Unit::NauticalMile,
"nmi"},
511 {Length::Unit::Inch,
"in"},
512 {Length::Unit::Foot,
"ft"},
513 {Length::Unit::Yard,
"yd"},
514 {Length::Unit::Mile,
"mi"},
517 auto iter = STRINGS.find(unit);
519 if (iter == STRINGS.end())
521 NS_FATAL_ERROR(
"A symbol could not be found for Length::Unit with value "
522 << EnumHash()(unit));
531 using Entry = std::tuple<std::string, std::string>;
532 using StringTable = std::unordered_map<Length::Unit, Entry, EnumHash>;
534 static const StringTable STRINGS{
535 {Length::Unit::Nanometer, Entry{
"nanometer",
"nanometers"}},
536 {Length::Unit::Micrometer, Entry{
"micrometer",
"micrometer"}},
537 {Length::Unit::Millimeter, Entry{
"millimeter",
"millimeters"}},
538 {Length::Unit::Centimeter, Entry{
"centimeter",
"centimeters"}},
539 {Length::Unit::Meter, Entry{
"meter",
"meters"}},
540 {Length::Unit::Kilometer, Entry{
"kilometer",
"kilometers"}},
541 {Length::Unit::NauticalMile, Entry{
"nautical mile",
"nautical miles"}},
542 {Length::Unit::Inch, Entry{
"inch",
"inches"}},
543 {Length::Unit::Foot, Entry{
"foot",
"feet"}},
544 {Length::Unit::Yard, Entry{
"yard",
"yards"}},
545 {Length::Unit::Mile, Entry{
"mile",
"miles"}},
548 auto iter = STRINGS.find(unit);
550 if (iter == STRINGS.end())
552 NS_FATAL_ERROR(
"A symbol could not be found for Length::Unit with value "
553 << EnumHash()(unit));
558 return std::get<1>(iter->second);
561 return std::get<0>(iter->second);
564std::optional<Length::Unit>
567 using UnitTable = std::unordered_map<std::string, Length::Unit>;
569 static const UnitTable UNITS{
570 {
"nm", Length::Unit::Nanometer},
571 {
"nanometer", Length::Unit::Nanometer},
572 {
"nanometers", Length::Unit::Nanometer},
573 {
"nanometre", Length::Unit::Nanometer},
574 {
"nanometres", Length::Unit::Nanometer},
575 {
"um", Length::Unit::Micrometer},
576 {
"micrometer", Length::Unit::Micrometer},
577 {
"micrometers", Length::Unit::Micrometer},
578 {
"micrometre", Length::Unit::Micrometer},
579 {
"micrometres", Length::Unit::Micrometer},
580 {
"mm", Length::Unit::Millimeter},
581 {
"millimeter", Length::Unit::Millimeter},
582 {
"millimeters", Length::Unit::Millimeter},
583 {
"millimetre", Length::Unit::Millimeter},
584 {
"millimetres", Length::Unit::Millimeter},
585 {
"cm", Length::Unit::Centimeter},
586 {
"centimeter", Length::Unit::Centimeter},
587 {
"centimeters", Length::Unit::Centimeter},
588 {
"centimetre", Length::Unit::Centimeter},
589 {
"centimetres", Length::Unit::Centimeter},
590 {
"m", Length::Unit::Meter},
591 {
"meter", Length::Unit::Meter},
592 {
"metre", Length::Unit::Meter},
593 {
"meters", Length::Unit::Meter},
594 {
"metres", Length::Unit::Meter},
595 {
"km", Length::Unit::Kilometer},
596 {
"kilometer", Length::Unit::Kilometer},
597 {
"kilometers", Length::Unit::Kilometer},
598 {
"kilometre", Length::Unit::Kilometer},
599 {
"kilometres", Length::Unit::Kilometer},
600 {
"nmi", Length::Unit::NauticalMile},
601 {
"nauticalmile", Length::Unit::NauticalMile},
602 {
"nauticalmiles", Length::Unit::NauticalMile},
603 {
"in", Length::Unit::Inch},
604 {
"inch", Length::Unit::Inch},
605 {
"inches", Length::Unit::Inch},
606 {
"ft", Length::Unit::Foot},
607 {
"foot", Length::Unit::Foot},
608 {
"feet", Length::Unit::Foot},
609 {
"yd", Length::Unit::Yard},
610 {
"yard", Length::Unit::Yard},
611 {
"yards", Length::Unit::Yard},
612 {
"mi", Length::Unit::Mile},
613 {
"mile", Length::Unit::Mile},
614 {
"miles", Length::Unit::Mile},
618 static auto Normalize = [](
const std::string& str) {
620 output.reserve(str.size());
622 for (
unsigned char c : str)
630 output.push_back(std::tolower(c));
636 unitString = Normalize(unitString);
638 auto iter = UNITS.find(unitString);
640 if (iter != UNITS.end())
651 stream << l.
As(Length::Unit::Meter);
659 stream <<
q.Value() <<
' ' <<
ToSymbol(
q.Unit());
688std::tuple<bool, double, std::string>
699 value = std::stod(input, &pos);
701 catch (
const std::exception& e)
703 NS_LOG_ERROR(
"Caught exception while parsing double: " << e.what());
705 return std::make_tuple(
false, 0,
"");
709 while (pos < input.size() && std::isspace(input[pos]))
714 if (pos < input.size())
716 NS_LOG_LOGIC(
"String has value and symbol, extracting symbol");
719 symbol = input.substr(pos);
722 return std::make_tuple(
true, value, symbol);
728 bool success =
false;
734 auto origFlags = stream.flags();
742 if (success && symbol.empty())
744 NS_LOG_LOGIC(
"Temp string only contained value, extracting unit symbol from stream");
752 if (symbol ==
"nautical")
758 symbol.push_back(
' ');
766 stream.flags(origFlags);
774 return Length(value, Length::Unit::Nanometer);
780 return Length(value, Length::Unit::Micrometer);
786 return Length(value, Length::Unit::Millimeter);
792 return Length(value, Length::Unit::Centimeter);
798 return Length(value, Length::Unit::Meter);
804 return Length(value, Length::Unit::Kilometer);
810 return Length(value, Length::Unit::NauticalMile);
816 return Length(value, Length::Unit::Inch);
822 return Length(value, Length::Unit::Foot);
828 return Length(value, Length::Unit::Yard);
834 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.
Length & operator=(const Length &other)=default
Copy Assignment operator.
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()
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 ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
#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 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 Convert(double value, ns3::Length::Unit fromUnit, ns3::Length::Unit toUnit)
Convert a value in one unit to the equivalent value in another unit.
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.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
bool operator==(const EventId &a, const EventId &b)
std::ostream & operator<<(std::ostream &os, const Angles &a)
std::istream & operator>>(std::istream &is, Angles &a)
bool operator<(const EventId &a, const EventId &b)
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...