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 <optional>
35 #include <ostream>
36 #include <string>
37 
47 namespace ns3 {
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 
288  Quantity (const Quantity&) = default;
289 
293  Quantity (Quantity&&) = default;
294 
298  ~Quantity () = default;
299 
305  Quantity& operator= (const Quantity& other) = default;
306 
312  Quantity& operator= (Quantity&& other) = default;
313 
319  double Value () const
320  {
321  return m_value;
322  }
323 
330  {
331  return m_unit;
332  }
333 
334 private:
335  double m_value;
337  };
338 
347 
363  static std::optional<Length> TryParse (double value, const std::string& unit);
364 
370  Length ();
371 
381  Length (const std::string& text);
382 
395  Length (double value, const std::string& unit);
396 
406  Length (double value, Length::Unit unit);
407 
413  Length (Quantity quantity);
414 
415 #ifdef HAVE_BOOST_UNITS
428  template <class U, class T>
429  explicit Length (boost::units::quantity<U, T> quantity);
430 #endif
431 
439  Length (const Length& other) = default;
440 
451  Length (Length&& other) = default;
452 
456  ~Length () = default;
457 
467  Length& operator= (const Length& other) = default;
468 
479  Length& operator= (Length&& other) = default;
480 
491 
502  bool IsEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
503 
514  bool IsNotEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
515 
526  bool IsLess (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
527 
543  bool IsLessOrEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
544 
560  bool IsGreater (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
561 
577  bool IsGreaterOrEqual (const Length& other, double tolerance = DEFAULT_TOLERANCE) const;
578 
593  void swap (Length& other);
594 
604  double GetDouble () const;
605 
616  Quantity As (Unit unit) const;
617 
618 private:
619  double m_value;
620 }; // class Length
621 
623 
635 std::string ToSymbol (Length::Unit unit);
636 
653 std::string ToName (Length::Unit unit, bool plural = false);
654 
672 std::optional<Length::Unit> FromString (std::string unitString);
673 
690 std::ostream& operator<< (std::ostream& stream, const Length& l);
691 
708 std::ostream& operator<< (std::ostream& stream, const Length::Quantity& q);
709 
726 std::ostream& operator<< (std::ostream& stream, Length::Unit unit);
727 
742 std::istream& operator>> (std::istream& stream, Length& l);
743 
758 bool operator== (const Length& left, const Length& right);
759 
774 bool operator!= (const Length& left, const Length& right);
775 
790 bool operator< (const Length& left, const Length& right);
791 
806 bool operator<= (const Length& left, const Length& right);
807 
822 bool operator> (const Length& left, const Length& right);
823 
838 bool operator>= (const Length& left, const Length& right);
839 
853 Length operator+ (const Length& left, const Length& right);
854 
868 Length operator- (const Length& left, const Length& right);
869 
883 Length operator* (double scalar, const Length& l);
897 Length operator* (const Length& l, double scalar);
898 
915 Length operator/ (const Length& left, double scalar);
916 
932 double operator/ (const Length& numerator, const Length& denominator);
933 
954 int64_t Div (const Length& numerator, const Length& denominator, Length* remainder = nullptr);
955 
971 Length Mod (const Length& numerator, const Length& denominator);
972 
980 Length NanoMeters (double value);
981 Length MicroMeters (double value);
982 Length MilliMeters (double value);
983 Length CentiMeters (double value);
984 Length Meters (double value);
985 Length KiloMeters (double value);
986 Length NauticalMiles (double value);
987 Length Inches (double value);
988 Length Feet (double value);
989 Length Yards (double value);
990 Length Miles (double value);
993 #ifdef HAVE_BOOST_UNITS
994 template <class U, class T>
995 Length::Length (boost::units::quantity<U, T> quantity)
996  : m_value (0)
997 {
998  namespace bu = boost::units;
999  using BoostMeters = bu::quantity<bu::si::length, double>;
1000 
1001  //convert value to meters
1002  m_value = static_cast<BoostMeters> (quantity).value ();
1003 }
1004 #endif
1005 
1006 } // namespace ns3
1007 
1008 #endif /* NS3_LENGTH_H_ */
1009 
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:335
Quantity(double value, Length::Unit unit)
Constructor.
Definition: length.h:280
~Quantity()=default
Destructor.
double Value() const
The value of the quantity.
Definition: length.h:319
Length::Unit Unit() const
The unit of the quantity.
Definition: length.h:329
Quantity(const Quantity &)=default
Copy Constructor.
Length::Unit m_unit
unit of length of the value
Definition: length.h:336
Quantity(Quantity &&)=default
Move Constructor.
Quantity & operator=(const Quantity &other)=default
Copy Assignment Operator.
Represents a length in meters.
Definition: length.h:244
void swap(Length &other)
Swap values with another object.
Definition: length.cc:365
static constexpr double DEFAULT_TOLERANCE
Default tolerance value used for the member comparison functions (IsEqual, IsLess,...
Definition: length.h:346
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:357
double GetDouble() const
Current length value.
Definition: length.cc:373
bool IsGreater(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is less in value than this instance.
Definition: length.cc:349
double m_value
Length in meters.
Definition: length.h:619
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:310
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
Definition: length.cc:379
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:341
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:238
~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:252
bool IsLess(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is greater in value than this instance.
Definition: length.cc:333
bool IsNotEqual(const Length &other, double tolerance=DEFAULT_TOLERANCE) const
Check if other is not equal in value to this instance.
Definition: length.cc:325
#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:131
bool operator>=(const int64x64_t &lhs, const int64x64_t &rhs)
Greater or equal operator.
Definition: int64x64.h:166
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)
Subtraction operator.
Definition: int64x64.h:103
int64x64_t operator+(const int64x64_t &lhs, const int64x64_t &rhs)
Addition operator.
Definition: int64x64.h:89
int64x64_t operator*(const int64x64_t &lhs, const int64x64_t &rhs)
Multiplication operator.
Definition: int64x64.h:117
Length KiloMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:808
Length MilliMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:790
Length NauticalMiles(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:814
std::string ToName(Length::Unit unit, bool plural)
Return the name of the supplied unit.
Definition: length.cc:536
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition: length.cc:413
Length Yards(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:832
Length Feet(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:826
Length Mod(const Length &numerator, const Length &denominator)
Calculate the amount remaining after dividing two lengths.
Definition: length.cc:493
Length MicroMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:784
Length Miles(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:838
Length Meters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:802
std::string ToSymbol(Length::Unit unit)
Return the symbol of the supplied unit.
Definition: length.cc:506
Length CentiMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:796
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:474
Length NanoMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:778
Length Inches(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:820
std::optional< Length::Unit > FromString(std::string unitString)
Find the equivalent Length::Unit for a unit string.
Definition: length.cc:572
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:158
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:176
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:1612
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139