A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
length-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Lawrence Livermore National Laboratory
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathew Bielejeski <bielejeski1@llnl.gov>
7 */
8
9#include "ns3/core-module.h"
10#include "ns3/length.h"
11
12#include <iostream>
13
14/**
15 * \defgroup length-examples Demonstrates usage of the ns3::Length class
16 * \ingroup core-examples
17 * \ingroup length
18 */
19
20/**
21 * \file
22 * \ingroup length-examples
23 * Demonstrates usage of the ns3::Length class
24 */
25
26using namespace ns3;
27
28/**
29 * \ingroup length-examples
30 * \brief Demonstrates the use of ns3::Length constructors.
31 */
32void
34{
35 double input = 5;
36 Length::Quantity quantity(input, Length::Unit::Meter);
37
38 std::cout << "\nConstructors:"
39 << "\nLength (" << input << ", Unit::Meter) = " << Length(input, Length::Unit::Meter)
40 << "\nLength (" << input << ", \"m\") = " << Length(input, "m") << "\nLength ("
41 << input << ", \"meter\") = " << Length(input, "meter") << "\nLength (Quantity("
42 << input << ", Unit::Meter)) = " << Length(quantity)
43 << "\nLength (\"5m\") = " << Length("5m") << "\nLength (\"5 m\") = " << Length("5 m")
44 << "\nLength (\"5meter\") = " << Length("5meter")
45 << "\nLength (\"5 meter\") = " << Length("5 meter")
46 << "\nLength (\"5meters\") = " << Length("5meters")
47 << "\nLength (\"5 meters\") = " << Length("5 meters") << std::endl;
48}
49
50/**
51 * \ingroup length-examples
52 * \brief Demonstrates the use of ns3::Length conversions.
53 */
54void
56{
57 // construct length using value and unit
58 Length moonDistance(3.84402e8, Length::Unit::Meter);
59
60 // Demonstrate conversion to various units
61 std::cout << "\n"
62 << "Conversions:\n"
63 << "Distance to moon = " << moonDistance << "\n"
64 << "In Feet: " << moonDistance.As(Length::Unit::Foot) << "\n"
65 << "In Miles: " << moonDistance.As(Length::Unit::Mile) << "\n"
66 << "In Kilometers: " << moonDistance.As(Length::Unit::Kilometer) << std::endl;
67}
68
69/**
70 * \ingroup length-examples
71 * \brief Demonstrates the use of ns3::Length arithmetic operators.
72 */
73void
75{
76 double scale = 10;
77
78 // construct lengths using helper functions
79 Length oneMeter = Meters(1);
80 Length twoMeter = Meters(2);
81
82 std::cout << "\n"
83 << "Arithmetic Operations:\n"
84 << "Addition: " << oneMeter << " + " << twoMeter << " = " << (oneMeter + twoMeter)
85 << "\n"
86 << "Subtraction: " << twoMeter << " - " << oneMeter << " = " << (twoMeter - oneMeter)
87 << "\n"
88 << "Multiplication By Scalar: " << oneMeter << " * " << scale << " = "
89 << (oneMeter * scale) << "\n"
90 << "Division: " << oneMeter << " / " << twoMeter << " = " << (oneMeter / twoMeter)
91 << "\n"
92 << "Division By Scalar: " << oneMeter << " / " << scale << " = " << (oneMeter / scale)
93 << std::endl;
94}
95
96/**
97 * \ingroup length-examples
98 * \brief Demonstrates the use of ns3::Length equality operators.
99 */
100void
102{
103 Length oneMeter = Meters(1);
104 Length twoMeter = Meters(2);
105 Length threeMeter = Meters(3);
106
107 // NOLINTBEGIN(misc-redundant-expression)
108 std::cout << "\n"
109 << "Comparison Operations:\n"
110 << std::boolalpha << "Equality: " << oneMeter << " == " << oneMeter << " is "
111 << (oneMeter == oneMeter) << "\n"
112 << "Equality: " << oneMeter << " == " << twoMeter << " is " << (oneMeter == twoMeter)
113 << "\n"
114 << "Inequality: " << oneMeter << " != " << oneMeter << " is "
115 << (oneMeter != oneMeter) << "\n"
116 << "Inequality: " << oneMeter << " != " << twoMeter << " is "
117 << (oneMeter != twoMeter) << "\n"
118 << "Lesser: " << oneMeter << " < " << oneMeter << " is " << (oneMeter < oneMeter)
119 << "\n"
120 << "Lesser: " << oneMeter << " < " << twoMeter << " is " << (oneMeter < twoMeter)
121 << "\n"
122 << "Lesser: " << threeMeter << " < " << oneMeter << " is " << (threeMeter < oneMeter)
123 << "\n"
124 << "Greater: " << oneMeter << " > " << oneMeter << " is " << (oneMeter > oneMeter)
125 << "\n"
126 << "Greater: " << oneMeter << " > " << twoMeter << " is " << (oneMeter > twoMeter)
127 << "\n"
128 << "Greater: " << threeMeter << " > " << oneMeter << " is " << (threeMeter > oneMeter)
129 << std::endl;
130 // NOLINTEND(misc-redundant-expression)
131}
132
133/**
134 * \ingroup length-examples
135 * \brief Demonstrates the use of ns3::Length multiplications and divisions.
136 */
137void
139{
140 // construct length using helper function
141 Length totalLen = Feet(20);
142 Length pieceLen = Feet(3);
143 Length remainder;
144
145 int64_t count = Div(totalLen, pieceLen, &remainder);
146
147 std::cout << "\nHow many times can a " << totalLen.As(Length::Unit::Foot) << " length "
148 << "be split into " << pieceLen.As(Length::Unit::Foot) << " sized pieces? " << count
149 << "\nremainder: " << remainder.As(Length::Unit::Foot) << std::endl;
150
151 std::cout << "\nHow much remains after splitting a " << totalLen.As(Length::Unit::Foot)
152 << " length into " << pieceLen.As(Length::Unit::Foot) << " sized pieces? "
153 << Mod(totalLen, pieceLen).As(Length::Unit::Foot) << std::endl;
154}
155
156int
157main(int argc, char** argv)
158{
159 Constructors();
160 Conversions();
163 DivAndMod();
164
165 return 0;
166}
An immutable class which represents a value in a specific length unit.
Definition length.h:261
Represents a length in meters.
Definition length.h:233
Quantity As(Unit unit) const
Create a Quantity in a specific unit from a Length.
Definition length.cc:376
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:824
Length Mod(const Length &numerator, const Length &denominator)
Calculate the amount remaining after dividing two lengths.
Definition length.cc:490
Length Meters(double value)
Construct a length from a value in the indicated unit.
Definition length.cc:800
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:471
Every class exported by the ns3 library is enclosed in the ns3 namespace.