A Discrete-Event Network Simulator
API
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 
46 namespace ns3
47 {
48 
243 class Length
244 {
245  public:
250  enum Unit : uint16_t
251  {
252  // Metric Units
253  Nanometer = 1,
260 
261  // US Customary Units
265  Mile
266  };
267 
271  class Quantity
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 
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 
491  Length& operator=(const Length::Quantity& q);
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 
636 std::string ToSymbol(Length::Unit unit);
637 
654 std::string ToName(Length::Unit unit, bool plural = false);
655 
673 std::optional<Length::Unit> FromString(std::string unitString);
674 
691 std::ostream& operator<<(std::ostream& stream, const Length& l);
692 
709 std::ostream& operator<<(std::ostream& stream, const Length::Quantity& q);
710 
727 std::ostream& operator<<(std::ostream& stream, Length::Unit unit);
728 
743 std::istream& operator>>(std::istream& stream, Length& l);
744 
759 bool operator==(const Length& left, const Length& right);
760 
775 bool operator!=(const Length& left, const Length& right);
776 
791 bool operator<(const Length& left, const Length& right);
792 
807 bool operator<=(const Length& left, const Length& right);
808 
823 bool operator>(const Length& left, const Length& right);
824 
839 bool operator>=(const Length& left, const Length& right);
840 
854 Length operator+(const Length& left, const Length& right);
855 
869 Length operator-(const Length& left, const Length& right);
870 
884 Length operator*(double scalar, const Length& l);
898 Length operator*(const Length& l, double scalar);
899 
916 Length operator/(const Length& left, double scalar);
917 
933 double operator/(const Length& numerator, const Length& denominator);
934 
955 int64_t Div(const Length& numerator, const Length& denominator, Length* remainder = nullptr);
956 
972 Length Mod(const Length& numerator, const Length& denominator);
973 
981 Length NanoMeters(double value);
982 Length MicroMeters(double value);
983 Length MilliMeters(double value);
984 Length CentiMeters(double value);
985 Length Meters(double value);
986 Length KiloMeters(double value);
987 Length NauticalMiles(double value);
988 Length Inches(double value);
989 Length Feet(double value);
990 Length Yards(double value);
991 Length Miles(double value);
994 #ifdef HAVE_BOOST_UNITS
995 template <class U, class T>
996 Length::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
double m_value
Value of the length.
Definition: length.h:336
Quantity(double value, Length::Unit unit)
Constructor.
Definition: length.h:280
~Quantity()=default
Destructor.
Quantity & operator=(Quantity &&other)=default
Move Assignment Operator.
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.
Quantity & operator=(const Quantity &other)=default
Copy Assignment Operator.
Represents a length in meters.
Definition: length.h:244
Length & operator=(Length &&other)=default
Move Assignment operator.
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
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(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 & operator=(const Length &other)=default
Copy Assignment operator.
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
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:678
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
ATTRIBUTE_HELPER_HEADER(ValueClassTest)
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
value
Definition: second.py:48