netxsimdg
Loading...
Searching...
No Matches
Iterator.cpp
Go to the documentation of this file.
1
8
9#include <sstream>
10
11namespace Nextsim {
12
13Iterator::NullIterant Iterator::nullIterant;
14
15Iterator::Iterator()
16 : iterant(&nullIterant)
17{
18}
19
20Iterator::Iterator(Iterant* iterant)
21 : iterant(iterant)
22{
23}
24
25void Iterator::setIterant(Iterant* iterant) { this->iterant = iterant; }
26
27void Iterator::setStartStopStep(TimePoint startTime, TimePoint stopTime, Duration timestep)
28{
29 this->startTime = startTime;
30 this->stopTime = stopTime;
31 this->timestep = timestep;
32}
33
34TimePoint Iterator::parseAndSet(const std::string& startTimeStr, const std::string& stopTimeStr,
35 const std::string& durationStr, const std::string& stepStr)
36{
37 std::stringstream ss(startTimeStr);
38 ss >> startTime;
39 ss = std::stringstream(stepStr);
40 ss >> timestep;
41 if (!durationStr.empty()) {
42 ss = std::stringstream(durationStr);
43 Duration duration;
44 ss >> duration;
45 stopTime = startTime + duration;
46 } else {
47 ss = std::stringstream(stopTimeStr);
48 ss >> stopTime;
49 }
50
51 return startTime;
52}
53
55{
56 iterant->start(startTime);
57
58 for (auto t = startTime; t < stopTime; t += timestep) {
59 TimestepTime tsTime = { t, timestep };
60 iterant->iterate(tsTime);
61 }
62
63 iterant->stop(stopTime);
64}
65
66} /* namespace Nextsim */
A base class for classes that specify what happens during one timestep.
Definition Iterator.hpp:76
virtual void iterate(const TimestepTime &dt)=0
virtual void stop(const TimePoint &stopTime)=0
virtual void start(const TimePoint &startTime)=0
void run()
Run the Iterant over the specified time period.
Definition Iterator.cpp:54
TimePoint parseAndSet(const std::string &startTimeStr, const std::string &stopTimeStr, const std::string &durationStr, const std::string &stepStr)
Parses the four strings and sets the time parameters from them.
Definition Iterator.cpp:34
static NullIterant nullIterant
A static instance of the NullIterant class.
Definition Iterator.hpp:119
void setStartStopStep(TimePoint startTime, TimePoint stopTime, Duration timestep)
Sets the time parameters as a start time, stop time and timestep length.
Definition Iterator.cpp:27
void setIterant(Iterant *iterant)
Sets the iterant to be iterated using a pointer.
Definition Iterator.cpp:25