A Discrete-Event Network Simulator
API
length.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2019 Lawrence Livermore National Laboratory
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathew Bielejeski <bielejeski1@llnl.gov>
19  */
20 
21 #ifndef NS3_LENGTH_H_
22 #define NS3_LENGTH_H_
23 
24 #include "attribute.h"
25 #include "attribute-helper.h"
26 
27 #ifdef HAVE_BOOST
28 #include <boost/units/quantity.hpp>
29 #include <boost/units/systems/si.hpp>
30 #endif
31 
32 #include <istream>
33 #include <limits>
34 #include <ostream>
35 #include <string>
36 #include <tuple>
37 
47 namespace ns3 {
48 
238 class Length
239 {
240 public:
245  enum Unit : uint16_t
246  {
247  //Metric Units
248  Nanometer = 1,
255 
256  //US Customary Units
261  };
262 
266  class Quantity
267  {
268 public:
275  Quantity (double value, Length::Unit unit)
276  : m_value (value),
277  m_unit (unit)
278  { }
279 
283  Quantity (const Quantity&) = default;
284 
288  Quantity (Quantity&&) = default;
289 
293  ~Quantity () = default;
294 
298  Quantity& operator= (const Quantity&) = default;
299 
303  Quantity& operator= (Quantity&&) = default;
304 
310  double Value () const
311  {
312  return m_value;
313  }
314 
321  {
322  return m_unit;
323  }
324 
325 private:
326  double m_value;
328  };
329 
338 
355  static std::tuple<bool, Length> TryParse (double value, const std::string& unit);
356 
362  Length ();
363 
373  Length (const std::string& text);
374 
387  Length (double value, const std::string& unit);
388 
398  Length (double value, Length::Unit unit);
399 
405  Length (Quantity quantity);
406 
407 #ifdef HAVE_BOOST_UNITS
408 
420  template <class U, class T>
421  explicit Length (boost::units::quantity<U, T> quantity);
422 #endif
423 
431  Length (const Length& other) = default;
432 
443  Length (Length&& other) = default;
444 
448  ~Length () = default;
449 
459  Length& operator= (const Length& other) = default;
460 
471  Length& operator= (Length&& other) = default;
472 
483 
494  bool IsEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
495 
506  bool IsNotEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
507 
518  bool IsLess (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
519 
535  bool IsLessOrEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
536 
552  bool IsGreater (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
553 
569  bool IsGreaterOrEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
570 
585  void swap (Length& other);
586 
596  double GetDouble () const;
597 
608  Quantity As (Unit unit) const;
609 
610 private:
611  double m_value;
612 }; // class Length
613 
618 
630 std::string ToSymbol (Length::Unit unit);
631 
648 std::string ToName (Length::Unit unit, bool plural = false);
649 
669 std::tuple<bool, Length::Unit> FromString (std::string unitString);
670 
687 std::ostream& operator<< (std::ostream& stream, const Length& l);
688 
705 std::ostream& operator<< (std::ostream& stream, const Length::Quantity& q);
706 
723 std::ostream& operator<< (std::ostream& stream, Length::Unit unit);
724 
739 std::istream& operator>> (std::istream& stream, Length& l);
740 
755 bool operator== (const Length& left, const Length& right);
756 
771 bool operator!= (const Length& l, const Length& r);
772 
787 bool operator< (const Length& l, const Length& r);
788 
803 bool operator<= (const Length& l, const Length& r);
804 
819 bool operator> (const Length& l, const Length& r);
820 
835 bool operator>= (const Length& l, const Length& r);
836 
850 Length operator+ (const Length& first, const Length& second);
851 
865 Length operator- (const Length& first, const Length& second);
866 
880 Length operator* (double scalar, const Length& right);
881 
895 Length operator* (const Length& left, double scalar);
896 
913 Length operator/ (const Length& left, double scalar);
914 
930 double operator/ (const Length& numerator, const Length& denominator);
931 
952 int64_t Div (const Length& numerator, const Length& denominator, Length* remainder = nullptr);
953 
969 Length Mod (const Length& numerator, const Length& denominator);
970 
983 Length NanoMeters (double value);
984 
992 Length MicroMeters (double value);
993 
1001 Length MilliMeters (double value);
1002 
1010 Length CentiMeters (double value);
1011 
1019 Length Meters (double value);
1020 
1028 Length KiloMeters (double value);
1029 
1037 Length NauticalMiles (double value);
1038 
1046 Length Inches (double value);
1047 
1055 Length Feet (double value);
1056 
1064 Length Yards (double value);
1065 
1073 Length Miles (double value);
1076 #ifdef HAVE_BOOST_UNITS
1077 template <class U, class T>
1078 Length::Length (boost::units::quantity<U, T> quantity)
1079  : m_value (0)
1080 {
1081  namespace bu = boost::units;
1082  using BoostMeters = bu::quantity<bu::si::length, double>;
1083 
1084  //convert value to meters
1085  m_value = static_cast<BoostMeters> (quantity).value ();
1086 }
1087 #endif
1088 
1089 } // namespace ns3
1090 
1091 #endif /* NS3_LENGTH_H_ */
1092 
Quantity(double value, Length::Unit unit)
Constructor.
Definition: length.h:275
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
Definition: length.cc:388
Length KiloMeters(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:826
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:350
Definition: second.py:1
Length Feet(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:844
int64x64_t operator+(const int64x64_t &lhs)
Unary plus operator.
Definition: int64x64-128.h:491
std::string ToSymbol(Length::Unit unit)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:520
Length CentiMeters(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:814
int64x64_t operator-(const int64x64_t &lhs)
Unary negation operator (change sign operator).
Definition: int64x64-128.h:501
Quantity & operator=(const Quantity &)=default
Copy Assignment Operator.
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:160
~Length()=default
Destructor.
double m_value
Length in meters.
Definition: length.h:611
double GetDouble() const
Current length value
Definition: length.cc:382
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:166
Length NanoMeters(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:796
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:160
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:137
Unit
Units of length in various measurement systems that are supported by the Length class.
Definition: length.h:245
bool operator<=(const int64x64_t &lhs, const int64x64_t &rhs)
Less or equal operator.
Definition: int64x64.h:155
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:117
Length Mod(const Length &numerator, const Length &denominator)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:507
1e-9 meters
Definition: length.h:248
Base length unit in US customary system.
Definition: length.h:258
static constexpr double DEFAULT_TOLERANCE
Default tolerance value used for the member comparison functions (IsEqual, IsLess, etc.)
Definition: length.h:337
Length NauticalMiles(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:832
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Length Yards(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:850
int64_t Div(const Length &numerator, const Length &denominator, Length *remainder)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:488
Base length unit in metric system.
Definition: length.h:252
int64x64_t operator/(const int64x64_t &lhs, const int64x64_t &rhs)
Division operator.
Definition: int64x64.h:131
Length()
Default Constructor.
Definition: length.cc:258
Length MilliMeters(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:808
1e-2 meters
Definition: length.h:251
Length & operator=(const Length &other)=default
Copy Assignment operator.
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:366
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.
Definition: callback.h:1606
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void swap(Length &other)
Swap values with another object
Definition: length.cc:374
bool IsNotEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is not equal in value to this instance.
Definition: length.cc:334
1,852 meters
Definition: length.h:254
bool IsGreater(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is less in value than this instance.
Definition: length.cc:358
bool IsLess(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater in value than this instance.
Definition: length.cc:342
~Quantity()=default
Destructor.
static std::tuple< bool, Length > TryParse(double value, const std::string &unit)
Attempt to construct a Length object from a value and a unit string.
Definition: length.cc:239
1/12 of a foot
Definition: length.h:257
bool IsEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is equal in value to this instance.
Definition: length.cc:319
Length Inches(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:838
Attribute helper (ATTRIBUTE_ )macros definition.
An immutable class which represents a value in a specific length unit.
Definition: length.h:266
5,280 feet
Definition: length.h:260
1e3 meters
Definition: length.h:253
Length::Unit m_unit
unit of length of the value
Definition: length.h:327
std::tuple< bool, Length::Unit > FromString(std::string unitString)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:586
Length MicroMeters(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:802
double Value() const
The value of the quantity.
Definition: length.h:310
Length::Unit Unit() const
The unit of the quantity.
Definition: length.h:320
bool operator>(const int64x64_t &lhs, const int64x64_t &rhs)
Greater operator.
Definition: int64x64-128.h:431
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:142
Length Miles(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:856
1e-6 meters
Definition: length.h:249
Represents a length in meters
Definition: length.h:238
Definition: first.py:1
std::string ToName(Length::Unit unit, bool plural)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:550
Length Meters(double value)
This function provides a string parsing method that does not rely on istream, which has been found to...
Definition: length.cc:820
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
double m_value
Value of the length.
Definition: length.h:326
1e-3 meters
Definition: length.h:250