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
47namespace ns3 {
48
244{
245public:
250 enum Unit : uint16_t
251 {
252 //Metric Units
260
261 //US Customary Units
265 Mile
266 };
267
272 {
273public:
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
334private:
335 double m_value;
337 };
338
347
364 static std::tuple<bool, 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
619private:
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
675std::tuple<bool, Length::Unit> FromString (std::string unitString);
676
693std::ostream& operator<< (std::ostream& stream, const Length& l);
694
711std::ostream& operator<< (std::ostream& stream, const Length::Quantity& q);
712
729std::ostream& operator<< (std::ostream& stream, Length::Unit unit);
730
745std::istream& operator>> (std::istream& stream, Length& l);
746
761bool operator== (const Length& left, const Length& right);
762
777bool operator!= (const Length& left, const Length& right);
778
793bool operator< (const Length& left, const Length& right);
794
809bool operator<= (const Length& left, const Length& right);
810
825bool operator> (const Length& left, const Length& right);
826
841bool operator>= (const Length& left, const Length& right);
842
856Length operator+ (const Length& left, const Length& right);
857
871Length operator- (const Length& left, const Length& right);
872
886Length operator* (double scalar, const Length& l);
900Length operator* (const Length& l, double scalar);
901
918Length operator/ (const Length& left, double scalar);
919
935double operator/ (const Length& numerator, const Length& denominator);
936
957int64_t Div (const Length& numerator, const Length& denominator, Length* remainder = nullptr);
958
974Length Mod (const Length& numerator, const Length& denominator);
975
983Length NanoMeters (double value);
984Length MicroMeters (double value);
985Length MilliMeters (double value);
986Length CentiMeters (double value);
987Length Meters (double value);
988Length KiloMeters (double value);
989Length NauticalMiles (double value);
990Length Inches (double value);
991Length Feet (double value);
992Length Yards (double value);
993Length Miles (double value);
996#ifdef HAVE_BOOST_UNITS
997template <class U, class T>
998Length::Length (boost::units::quantity<U, T> quantity)
999 : m_value (0)
1000{
1001 namespace bu = boost::units;
1002 using BoostMeters = bu::quantity<bu::si::length, double>;
1003
1004 //convert value to meters
1005 m_value = static_cast<BoostMeters> (quantity).value ();
1006}
1007#endif
1008
1009} // namespace ns3
1010
1011#endif /* NS3_LENGTH_H_ */
1012
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 & operator=(const Quantity &other)=default
Copy Assignment Operator.
~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.
Represents a length in meters.
Definition: length.h:244
void swap(Length &other)
Swap values with another object.
Definition: length.cc:374
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:366
double GetDouble() const
Current length value.
Definition: length.cc:382
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:358
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:319
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
Definition: length.cc:388
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
Length(Length &&other)=default
Move Constructor.
~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:342
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
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
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:821
Length MilliMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:803
Length NauticalMiles(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:827
std::string ToName(Length::Unit unit, bool plural)
Return the name of the supplied unit.
Definition: length.cc:545
bool operator>(const Length &left, const Length &right)
Check if left has a value greater than right.
Definition: length.cc:422
Length Yards(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:845
Length Feet(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:839
Length Mod(const Length &numerator, const Length &denominator)
Calculate the amount remaining after dividing two lengths.
Definition: length.cc:502
Length MicroMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:797
Length Miles(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:851
std::tuple< bool, Length::Unit > FromString(std::string unitString)
Find the equivalent Length::Unit for a unit string.
Definition: length.cc:581
Length Meters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:815
std::string ToSymbol(Length::Unit unit)
Return the symbol of the supplied unit.
Definition: length.cc:515
Length CentiMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:809
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:483
Length NanoMeters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:791
Length Inches(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:833
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
ATTRIBUTE_HELPER_HEADER(ValueClassTest)
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
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