Skip to content

Commit 2e348c2

Browse files
committed
core: (fixes #303) Demonstrate conditional use of ShowProgress
Assisted-by: Claude Fable 5
1 parent fba6f52 commit 2e348c2

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

src/core/examples/sample-show-progress.cc

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "ns3/core-module.h"
2323

2424
#include <chrono>
25+
#include <optional>
2526
#include <string>
2627
#include <thread>
2728

@@ -103,18 +104,21 @@ main(int argc, char** argv)
103104
Time interval = Seconds(10);
104105
Time wait = MilliSeconds(10);
105106
bool verbose = false;
107+
bool showProgress = true;
106108

107109
CommandLine cmd(__FILE__);
108110
cmd.AddValue("stop", "Simulation duration in virtual time.", stop);
109111
cmd.AddValue("interval", "Approximate reporting interval, in wall clock time.", interval);
110112
cmd.AddValue("wait", "Wallclock time to burn on each event.", wait);
111113
cmd.AddValue("verbose", "Turn on verbose progress message.", verbose);
114+
cmd.AddValue("showProgress", "Show progress reporting.", showProgress);
112115
cmd.Parse(argc, argv);
113116

114117
std::cout << "\n"
115118
<< cmd.GetName() << ":\n"
116119
<< "\n"
117120
<< "verbose progress message: " << (verbose ? "on\n" : "off\n")
121+
<< "progress reporting: " << (showProgress ? "on\n" : "off\n")
118122
<< "target reporting interval: " << interval.As(Time::S) << "\n"
119123
<< "average event sleep time: " << wait.As(Time::MS) << "\n"
120124
<< "total simulation run time: " << stop.As(Time::S) << std::endl;
@@ -123,8 +127,27 @@ main(int argc, char** argv)
123127
h->Event();
124128

125129
Simulator::Stop(stop);
126-
ShowProgress spinner(interval);
127-
spinner.SetVerbose(verbose);
130+
131+
// ShowProgress begins reporting when constructed; use std::optional
132+
// to construct it conditionally while keeping it alive through
133+
// Simulator::Run(). Here, the "showProgress" command-line argument
134+
// controls whether ShowProgress runs or not, and the "verbose"
135+
// command-line argument controls the verbosity of ShowProgress's output.
136+
std::optional<ShowProgress> spinner;
137+
if (showProgress)
138+
{
139+
spinner.emplace(interval);
140+
// Note that in the above statement, by default, ShowProgress
141+
// writes to std::cout. You can redirect its output to std::cerr
142+
// by passing "std::cerr" as a second argument in the above.
143+
spinner->SetVerbose(verbose);
144+
}
145+
146+
// Note that if you simply want to enable ShowProgress unconditionally
147+
// in your program, you can simply write, without the std::optional:
148+
//
149+
// ShowProgress spinner(interval);
150+
// spinner.SetVerbose(verbose);
128151

129152
Simulator::Run();
130153
Simulator::Destroy();

src/core/model/show-progress.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ namespace ns3
4747
* Example usage:
4848
*
4949
* @code
50-
* int main (int arg, char ** argv)
50+
* int main(int arg, char** argv)
5151
* {
52-
* // Create your model
52+
* // Create your model
5353
*
54-
* ShowProgress progress (Seconds (10), std::cerr);
55-
* Simulator::Run ();
56-
* Simulator::Destroy ();
54+
* ShowProgress progress(Seconds(10), std::cerr);
55+
* Simulator::Run();
56+
* Simulator::Destroy();
5757
* }
5858
* @endcode
5959
*
@@ -72,6 +72,26 @@ namespace ns3
7272
* Elapsed wall clock: 16s
7373
* @endcode
7474
*
75+
* ShowProgress begins reporting when constructed and must remain in
76+
* scope through Simulator::Run(). In particular, an instance declared
77+
* inside a conditional block would be destroyed at the closing brace,
78+
* before the simulation runs, and would produce no output. To create
79+
* the reporter conditionally, use std::optional:
80+
*
81+
* @code
82+
* bool showProgress;
83+
* CommandLine cmd(__FILE__);
84+
* cmd.AddValue("showProgress", "Show progress reporting.", showProgress);
85+
* ...
86+
* std::optional<ShowProgress> progress;
87+
* if (showProgress)
88+
* {
89+
* progress.emplace(Seconds(10), std::cerr);
90+
* }
91+
* Simulator::Run();
92+
* Simulator::Destroy();
93+
* @endcode
94+
*
7595
* A more extensive example of use is provided in sample-show-progress.cc.
7696
*
7797
* Based on a python version by Gustavo Carneiro <gjcarneiro@gmail.com>,

0 commit comments

Comments
 (0)