A Discrete-Event Network Simulator
API
length-example.cc
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 #include "ns3/core-module.h"
22 #include "ns3/length.h"
23 
24 #include <iostream>
25 
33 using namespace ns3;
34 
40 void Constructors ()
41 {
42  double input = 5;
43  Length::Quantity quantity (input, Length::Unit::Meter);
44 
45  std::cout << "\nConstructors:"
46  << "\nLength (" << input << ", Unit::Meter) = " << Length (input, Length::Unit::Meter)
47  << "\nLength (" << input << ", \"m\") = " << Length (input, "m")
48  << "\nLength (" << input << ", \"meter\") = " << Length (input, "meter")
49  << "\nLength (Quantity(" << input << ", Unit::Meter)) = " << Length (quantity)
50  << "\nLength (\"5m\") = " << Length ("5m")
51  << "\nLength (\"5 m\") = " << Length ("5 m")
52  << "\nLength (\"5meter\") = " << Length ("5meter")
53  << "\nLength (\"5 meter\") = " << Length ("5 meter")
54  << "\nLength (\"5meters\") = " << Length ("5meters")
55  << "\nLength (\"5 meters\") = " << Length ("5 meters")
56  << std::endl;
57 }
58 
64 void Conversions ()
65 {
66  //construct length using value and unit
67  Length moonDistance (3.84402e8, Length::Unit::Meter);
68 
69  //Demonstrate conversion to various units
70  std::cout << "\nConversions: "
71  << "\nDistance to moon = " << moonDistance
72  << "\nIn Feet: " << moonDistance.As (Length::Unit::Foot)
73  << "\nIn Miles: " << moonDistance.As (Length::Unit::Mile)
74  << "\nIn Kilometers: " << moonDistance.As (Length::Unit::Kilometer)
75  << std::endl;
76 }
77 
84 {
85  double scale = 10;
86 
87  //construct lengths using helper functions
88  Length oneMeter = Meters (1);
89  Length twoMeter = Meters (2);
90 
91  std::cout << "\nArithmetic Operations:"
92  << "\nAddition: " << oneMeter << " + " << oneMeter << " = "
93  << (oneMeter + oneMeter)
94  << "\nSubtraction: " << oneMeter << " - " << oneMeter << " = "
95  << (oneMeter - oneMeter)
96  << "\nMultiplication By Scalar: " << oneMeter << " * " << scale << " = "
97  << (oneMeter * scale)
98  << "\nDivision: " << oneMeter << " / " << twoMeter << " = "
99  << (oneMeter / twoMeter)
100  << "\nDivision By Scalar: " << oneMeter << " / " << scale << " = "
101  << (oneMeter / scale)
102  << std::endl;
103 
104 }
105 
112 {
113  Length oneMeter = Meters (1);
114  Length twoMeter = Meters (2);
115  Length threeMeter = Meters (3);
116 
117  std::cout << "\nComparison Operations:" << std::boolalpha
118  << "\nEquality: " << oneMeter << " == " << oneMeter << " is "
119  << (oneMeter == oneMeter)
120  << "\nEquality: " << oneMeter << " == " << twoMeter << " is "
121  << (oneMeter == twoMeter)
122  << "\nInequality: " << oneMeter << " != " << oneMeter << " is "
123  << (oneMeter != oneMeter)
124  << "\nInequality: " << oneMeter << " != " << twoMeter << " is "
125  << (oneMeter != twoMeter)
126  << "\nLesser: " << oneMeter << " < " << oneMeter << " is "
127  << (oneMeter < oneMeter)
128  << "\nLesser: " << oneMeter << " < " << twoMeter << " is "
129  << (oneMeter < twoMeter)
130  << "\nLesser: " << threeMeter << " < " << oneMeter << " is "
131  << (threeMeter < oneMeter)
132  << "\nGreater: " << oneMeter << " > " << oneMeter << " is "
133  << (oneMeter > oneMeter)
134  << "\nGreater: " << oneMeter << " > " << twoMeter << " is "
135  << (oneMeter > twoMeter)
136  << "\nGreater: " << threeMeter << " > " << oneMeter << " is "
137  << (threeMeter > oneMeter)
138  << std::endl;
139 }
140 
146 void DivAndMod ()
147 {
148  //construct length using helper function
149  Length totalLen = Feet (20);
150  Length pieceLen = Feet (3);
151  Length remainder;
152 
153  int64_t count = Div (totalLen, pieceLen, &remainder);
154 
155  std::cout << "\nHow many times can a " << totalLen.As (Length::Unit::Foot) << " length "
156  << "be split into " << pieceLen.As (Length::Unit::Foot) << " sized pieces? "
157  << count
158  << "\nremainder: " << remainder.As (Length::Unit::Foot)
159  << std::endl;
160 
161  std::cout << "\nHow much remains after splitting a "
162  << totalLen.As (Length::Unit::Foot) << " length into "
163  << pieceLen.As (Length::Unit::Foot) << " sized pieces? "
164  << Mod (totalLen, pieceLen).As (Length::Unit::Foot)
165  << std::endl;
166 }
167 
168 int main (int argc, char** argv)
169 {
170  Constructors ();
171  Conversions ();
174  DivAndMod ();
175 
176  return 0;
177 }
An immutable class which represents a value in a specific length unit.
Definition: length.h:272
Represents a length in meters.
Definition: length.h:244
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
Definition: length.cc:379
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
void Conversions()
Demonstrates the use of ns3::Length conversions.
Length Meters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:802
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
void Constructors()
Demonstrates the use of ns3::Length constructors.
void EqualityOperators()
Demonstrates the use of ns3::Length equality operators.
void ArithmeticOperators()
Demonstrates the use of ns3::Length arithmetic operators.
void DivAndMod()
Demonstrates the use of ns3::Length multiplications and divisions.
Every class exported by the ns3 library is enclosed in the ns3 namespace.