A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
length.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Lawrence Livermore National Laboratory
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathew Bielejeski <bielejeski1@llnl.gov>
18 */
19
20#ifndef NS3_LENGTH_H_
21#define NS3_LENGTH_H_
22
23#include "attribute-helper.h"
24#include "attribute.h"
25
26#ifdef HAVE_BOOST
27#include <boost/units/quantity.hpp>
28#include <boost/units/systems/si.hpp>
29#endif
30
31#include <istream>
32#include <limits>
33#include <optional>
34#include <ostream>
35#include <string>
36
46namespace ns3
47{
48
244{
245 public:
250 enum Unit : uint16_t
251 {
252 // Metric Units
260
261 // US Customary Units
265 Mile
266 };
267
272 {
273 public:
280 Quantity(double value, Length::Unit unit)
281 : m_value(value),
282 m_unit(unit)
283 {
284 }
285
289 Quantity(const Quantity&) = default;
290
294 Quantity(Quantity&&) = default;
295
299 ~Quantity() = default;
300
306 Quantity& operator=(const Quantity& other) = default;
307
313 Quantity& operator=(Quantity&& other) = default;
314
320 double Value() const
321 {
322 return m_value;
323 }
324
331 {
332 return m_unit;
333 }
334
335 private:
336 double m_value;
338 };
339
347 static constexpr double DEFAULT_TOLERANCE = std::numeric_limits<double>::epsilon();
348
364 static std::optional<Length> TryParse(double value, const std::string& unit);
365
371 Length();
372
382 Length(const std::string& text);
383
396 Length(double value, const std::string& unit);
397
407 Length(double value, Length::Unit unit);
408
414 Length(Quantity quantity);
415
416#ifdef HAVE_BOOST_UNITS
429 template <class U, class T>
430 explicit Length(boost::units::quantity<U, T> quantity);
431#endif
432
440 Length(const Length& other) = default;
441
452 Length(Length&& other) = default;
453
457 ~Length() = default;
458
468 Length& operator=(const Length& other) = default;
469
480 Length& operator=(Length&& other) = default;
481
492
503 bool IsEqual(const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
504
515 bool IsNotEqual(const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
516
527 bool IsLess(const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
528
544 bool IsLessOrEqual(const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
545
561 bool IsGreater(const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
562
578 bool IsGreaterOrEqual(const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
579
594 void swap(Length& other);
595
605 double GetDouble() const;
606
617 Quantity As(Unit unit) const;
618
619 private:
620 double m_value;
621}; // class Length
622
624
636std::string ToSymbol(Length::Unit unit);
637
654std::string ToName(Length::Unit unit, bool plural = false);
655
673std::optional<Length::Unit> FromString(std::string unitString);
674
691std::ostream& operator<<(std::ostream& stream, const Length& l);
692
709std::ostream& operator<<(std::ostream& stream, const Length::Quantity& q);
710
727std::ostream& operator<<(std::ostream& stream, Length::Unit unit);
728
743std::istream& operator>>(std::istream& stream, Length& l);
744
759bool operator==(const Length& left, const Length& right);
760
775bool operator!=(const Length& left, const Length& right);
776
791bool operator<(const Length& left, const Length& right);
792
807bool operator<=(const Length& left, const Length& right);
808
823bool operator>(const Length& left, const Length& right);
824
839bool operator>=(const Length& left, const Length& right);
840
854Length operator+(const Length& left, const Length& right);
855
869Length operator-(const Length& left, const Length& right);
870
884Length operator*(double scalar, const Length& l);
898Length operator*(const Length& l, double scalar);
899
916Length operator/(const Length& left, double scalar);
917
933double operator/(const Length& numerator, const Length& denominator);
934
955int64_t Div(const Length& numerator, const Length& denominator, Length* remainder = nullptr);
956
972Length Mod(const Length& numerator, const Length& denominator);
973
981Length NanoMeters(double value);
982Length MicroMeters(double value);
983Length MilliMeters(double value);
984Length CentiMeters(double value);
985Length Meters(double value);
986Length KiloMeters(double value);
987Length NauticalMiles(double value);
988Length Inches(double value);
989Length Feet(double value);
990Length Yards(double value);
991Length Miles(double value);
994#ifdef HAVE_BOOST_UNITS
995template <class U, class T>
996Length::Length(boost::units::quantity<U, T> quantity)
997 : m_value(0)
998{
999 namespace bu = boost::units;
1000 using BoostMeters = bu::quantity<bu::si::length, double>;
1001
1002 // convert value to meters
1003 m_value = static_cast<BoostMeters>(quantity).value();
1004}
1005#endif
1006
1007} // namespace ns3
1008
1009#endif /* NS3_LENGTH_H_ */
Attribute helper (ATTRIBUTE_ )macros definition.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
An immutable class which represents a value in a specific length unit.
Definition: length.h:272
Quantity & operator=(Quantity &&other)=default
Move Assignment Operator.
double m_value
Value of the length.
Definition: length.h:336
Quantity(double value, Length::Unit unit)
Constructor.
Definition: length.h:280
Quantity & operator=(const Quantity &other)=default
Copy Assignment Operator.
~Quantity()=default
Destructor.
double Value() const
The value of the quantity.
Definition: length.h:320
Length::Unit Unit() const
The unit of the quantity.
Definition: length.h:330
Quantity(const Quantity &)=default
Copy Constructor.
Length::Unit m_unit
unit of length of the value
Definition: length.h:337
Quantity(Quantity &&)=default
Move Constructor.
Represents a length in meters.
Definition: length.h:244
void swap(Length &other)
Swap values with another object.
Definition: length.cc:373
static constexpr double DEFAULT_TOLERANCE
Default tolerance value used for the member comparison functions (IsEqual, IsLess,...
Definition: length.h:347
bool IsGreaterOrEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is equal or less in value than this instance.
Definition: length.cc:365
double GetDouble() const
Current length value.
Definition: length.cc:381
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.
Definition: length.cc:357
double m_value
Length in meters.
Definition: length.h:620
Length & operator=(Length &&other)=default
Move Assignment operator.
Length(const Length &other)=default
Copy Constructor.
bool IsEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is equal in value to this instance.
Definition: length.cc:318
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
Definition: length.cc:387
bool IsLessOrEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater or equal in value than this instance.
Definition: length.cc:349
Length(Length &&other)=default
Move Constructor.
static std::optional< Length > TryParse(double value, const std::string &unit)
Attempt to construct a Length object from a value and a unit string.
Definition: length.cc:244
~Length()=default
Destructor.
Unit
Units of length in various measurement systems that are supported by the Length class.
Definition: length.h:251
@ NauticalMile
1,852 meters
Definition: length.h:259
@ Micrometer
1e-6 meters
Definition: length.h:254
@ Foot
Base length unit in US customary system.
Definition: length.h:263
@ Inch
1/12 of a foot
Definition: length.h:262
@ Centimeter
1e-2 meters
Definition: length.h:256
@ Mile
5,280 feet
Definition: length.h:265
@ Kilometer
1e3 meters
Definition: length.h:258
@ Meter
Base length unit in metric system.
Definition: length.h:257
@ Yard
3 feet
Definition: length.h:264
@ Nanometer
1e-9 meters
Definition: length.h:253
@ Millimeter
1e-3 meters
Definition: length.h:255
Length()
Default Constructor.
Definition: length.cc:258
bool IsLess(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater in value than this instance.
Definition: length.cc:341
bool IsNotEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is not equal in value to this instance.
Definition: length.cc:333
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
int64x64_t operator/(const int64x64_t &lhs, const int64x64_t &rhs)
Division operator.
Definition: int64x64.h:133
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:174
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
Definition: int64x64.h:161
int64x64_t operator-(const int64x64_t &lhs, const int64x64_t &rhs)
Subtraction operator.
Definition: int64x64.h:103
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition: int64x64.h:88
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:118
Length KiloMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:817
Length MilliMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:799
Length NauticalMiles(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:823
std::string ToName(Length::Unit unit, bool plural)
Return the name of the supplied unit.
Definition: length.cc:544
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition: length.cc:421
Length Yards(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:841
Length Feet(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:835
Length Mod(const Length &numerator, const Length &denominator)
Calculate the amount remaining after dividing two lengths.
Definition: length.cc:501
Length MicroMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:793
Length Miles(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:847
Length Meters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:811
std::string ToSymbol(Length::Unit unit)
Return the symbol of the supplied unit.
Definition: length.cc:514
Length CentiMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:805
int64_t Div(const Length &numerator, const Length &denominator, Length *remainder)
Calculate how many times numerator can be split into denominator sized pieces.
Definition: length.cc:482
Length NanoMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:787
Length Inches(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:829
std::optional< Length::Unit > FromString(std::string unitString)
Find the equivalent Length::Unit for a unit string.
Definition: length.cc:580
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.
Definition: callback.h:676
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170