A Discrete-Event Network Simulator
API
length-example.cc
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 #include "ns3/core-module.h"
21 #include "ns3/length.h"
22 
23 #include <iostream>
24 
37 using namespace ns3;
38 
43 void
45 {
46  double input = 5;
47  Length::Quantity quantity(input, Length::Unit::Meter);
48 
49  std::cout << "\nConstructors:"
50  << "\nLength (" << input << ", Unit::Meter) = " << Length(input, Length::Unit::Meter)
51  << "\nLength (" << input << ", \"m\") = " << Length(input, "m") << "\nLength ("
52  << input << ", \"meter\") = " << Length(input, "meter") << "\nLength (Quantity("
53  << input << ", Unit::Meter)) = " << Length(quantity)
54  << "\nLength (\"5m\") = " << Length("5m") << "\nLength (\"5 m\") = " << Length("5 m")
55  << "\nLength (\"5meter\") = " << Length("5meter")
56  << "\nLength (\"5 meter\") = " << Length("5 meter")
57  << "\nLength (\"5meters\") = " << Length("5meters")
58  << "\nLength (\"5 meters\") = " << Length("5 meters") << std::endl;
59 }
60 
65 void
67 {
68  // construct length using value and unit
69  Length moonDistance(3.84402e8, Length::Unit::Meter);
70 
71  // Demonstrate conversion to various units
72  std::cout << "\n"
73  << "Conversions:\n"
74  << "Distance to moon = " << moonDistance << "\n"
75  << "In Feet: " << moonDistance.As(Length::Unit::Foot) << "\n"
76  << "In Miles: " << moonDistance.As(Length::Unit::Mile) << "\n"
77  << "In Kilometers: " << moonDistance.As(Length::Unit::Kilometer) << std::endl;
78 }
79 
84 void
86 {
87  double scale = 10;
88 
89  // construct lengths using helper functions
90  Length oneMeter = Meters(1);
91  Length twoMeter = Meters(2);
92 
93  std::cout << "\n"
94  << "Arithmetic Operations:\n"
95  << "Addition: " << oneMeter << " + " << twoMeter << " = " << (oneMeter + twoMeter)
96  << "\n"
97  << "Subtraction: " << twoMeter << " - " << oneMeter << " = " << (twoMeter - oneMeter)
98  << "\n"
99  << "Multiplication By Scalar: " << oneMeter << " * " << scale << " = "
100  << (oneMeter * scale) << "\n"
101  << "Division: " << oneMeter << " / " << twoMeter << " = " << (oneMeter / twoMeter)
102  << "\n"
103  << "Division By Scalar: " << oneMeter << " / " << scale << " = " << (oneMeter / scale)
104  << std::endl;
105 }
106 
111 void
113 {
114  Length oneMeter = Meters(1);
115  Length twoMeter = Meters(2);
116  Length threeMeter = Meters(3);
117 
118  // NOLINTBEGIN(misc-redundant-expression)
119  std::cout << "\n"
120  << "Comparison Operations:\n"
121  << std::boolalpha << "Equality: " << oneMeter << " == " << oneMeter << " is "
122  << (oneMeter == oneMeter) << "\n"
123  << "Equality: " << oneMeter << " == " << twoMeter << " is " << (oneMeter == twoMeter)
124  << "\n"
125  << "Inequality: " << oneMeter << " != " << oneMeter << " is "
126  << (oneMeter != oneMeter) << "\n"
127  << "Inequality: " << oneMeter << " != " << twoMeter << " is "
128  << (oneMeter != twoMeter) << "\n"
129  << "Lesser: " << oneMeter << " < " << oneMeter << " is " << (oneMeter < oneMeter)
130  << "\n"
131  << "Lesser: " << oneMeter << " < " << twoMeter << " is " << (oneMeter < twoMeter)
132  << "\n"
133  << "Lesser: " << threeMeter << " < " << oneMeter << " is " << (threeMeter < oneMeter)
134  << "\n"
135  << "Greater: " << oneMeter << " > " << oneMeter << " is " << (oneMeter > oneMeter)
136  << "\n"
137  << "Greater: " << oneMeter << " > " << twoMeter << " is " << (oneMeter > twoMeter)
138  << "\n"
139  << "Greater: " << threeMeter << " > " << oneMeter << " is " << (threeMeter > oneMeter)
140  << std::endl;
141  // NOLINTEND(misc-redundant-expression)
142 }
143 
148 void
150 {
151  // construct length using helper function
152  Length totalLen = Feet(20);
153  Length pieceLen = Feet(3);
154  Length remainder;
155 
156  int64_t count = Div(totalLen, pieceLen, &remainder);
157 
158  std::cout << "\nHow many times can a " << totalLen.As(Length::Unit::Foot) << " length "
159  << "be split into " << pieceLen.As(Length::Unit::Foot) << " sized pieces? " << count
160  << "\nremainder: " << remainder.As(Length::Unit::Foot) << std::endl;
161 
162  std::cout << "\nHow much remains after splitting a " << totalLen.As(Length::Unit::Foot)
163  << " length into " << pieceLen.As(Length::Unit::Foot) << " sized pieces? "
164  << Mod(totalLen, pieceLen).As(Length::Unit::Foot) << std::endl;
165 }
166 
167 int
168 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:387
void Conversions()
Demonstrates the use of ns3::Length conversions.
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.
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 Meters(double value)
Construct a length from a value in the indicated unit.
Definition: length.cc:811
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.