Taskolib  1.3.3
Step.h
Go to the documentation of this file.
1 
23 // SPDX-License-Identifier: LGPL-2.1-or-later
24 
25 #ifndef TASKOLIB_STEP_H_
26 #define TASKOLIB_STEP_H_
27 
28 #include <chrono>
29 #include <set>
30 #include <string>
31 
32 #include "taskolib/CommChannel.h"
33 #include "taskolib/Context.h"
34 #include "taskolib/time_types.h"
35 #include "taskolib/Timeout.h"
37 #include "taskolib/VariableName.h"
38 
39 namespace task {
40 
41 using VariableNames = std::set<VariableName>;
42 
52 class Step
53 {
54 public:
56  enum Type
57  {
60  };
61 
63  static constexpr short max_indentation_level{ 20 };
64 
65 
67  explicit Step(Type type = type_action)
68  : type_{ type }
69  {}
70 
118  bool execute(Context& context, CommChannel* comm_channel = nullptr,
119  OptionalStepIndex opt_step_index = gul14::nullopt,
120  TimeoutTrigger* sequence_timeout = nullptr);
121 
134  {
136  }
137 
144  short get_indentation_level() const noexcept { return indentation_level_; }
145 
153  const std::string& get_label() const { return label_; }
154 
171  const std::string& get_script() const { return script_; }
172 
179 
186 
188  Timeout get_timeout() const { return timeout_; }
189 
191  Type get_type() const noexcept { return type_; }
192 
198  bool is_running() const noexcept { return is_running_; }
199 
201  bool is_disabled() const noexcept { return is_disabled_; }
202 
204  Step& set_disabled(bool disable);
205 
215  Step& set_indentation_level(short level);
216 
224  Step& set_label(const std::string& label);
225 
231  Step& set_running(bool is_running);
232 
237  Step& set_script(const std::string& script);
238 
245 
252 
254  Step& set_timeout(Timeout timeout);
255 
260  Step& set_type(Type type);
261 
263  Step& set_used_context_variable_names(const VariableNames& used_context_variable_names);
264  Step& set_used_context_variable_names(VariableNames&& used_context_variable_names);
265 
266 private:
267  std::string label_;
268  std::string script_;
274  short indentation_level_{ 0 };
275  bool is_running_{ false };
276  bool is_disabled_{ false };
277 
282  void copy_used_variables_from_context_to_lua(const Context& context, sol::state& lua);
283 
288  void copy_used_variables_from_lua_to_context(const sol::state& lua, Context& context);
289 
294  bool execute_impl(Context& context, CommChannel* comm_channel
295  , OptionalStepIndex index, TimeoutTrigger* sequence_timeout);
296 };
297 
299 using ExecutionSteps = std::set<Step::Type>;
300 
307  };
308 
310 std::string to_string(Step::Type type);
311 
318 bool executes_script(Step::Type step_type);
319 
321 bool requires_bool_return_value(Step::Type step_type) noexcept;
322 
323 } // namespace task
324 
325 #endif
Declaration of the CommChannel struct.
Declaration of the Context and VariableValue types.
Logic to check if a timeout elapsed.
Declaration of the Timeout class.
Declaration of the VariableName class and of an associated specialization of std::hash.
A step is the main building block of a sequence.
Definition: Step.h:53
TimePoint get_time_of_last_execution() const
Return the timestamp of the last execution of this step's script.
Definition: Step.h:178
static constexpr short max_indentation_level
Maximum allowed level of indentation (or nesting of steps)
Definition: Step.h:63
bool execute_impl(Context &context, CommChannel *comm_channel, OptionalStepIndex index, TimeoutTrigger *sequence_timeout)
Execute the Lua script, throwing an exception if anything goes wrong.
Definition: Step.cc:113
Timeout get_timeout() const
Return the timeout duration for executing the script.
Definition: Step.h:188
Step & set_time_of_last_modification(TimePoint t)
Set the timestamp of the last modification of this step's script or label.
Definition: Step.cc:252
Step & set_running(bool is_running)
Set whether the step should be marked as "currently running".
Definition: Step.cc:233
short get_indentation_level() const noexcept
Return the indentation level of this step.
Definition: Step.h:144
bool is_disabled_
Definition: Step.h:276
Type get_type() const noexcept
Return the type of this step.
Definition: Step.h:191
Step & set_type(Type type)
Set the type of this step.
Definition: Step.cc:264
const std::string & get_label() const
Return the label of the step.
Definition: Step.h:153
bool is_running() const noexcept
Return whether this step is currently being executed.
Definition: Step.h:198
void copy_used_variables_from_context_to_lua(const Context &context, sol::state &lua)
Copy the variables listed in used_context_variable_names_ from the given Context into a Lua state.
Definition: Step.cc:50
Step & set_disabled(bool disable)
Set whether the step should be disabled (or possibly executed).
Definition: Step.cc:199
Type
An enum for differentiating the different types of step.
Definition: Step.h:57
@ type_end
Definition: Step.h:58
@ type_catch
Definition: Step.h:59
@ type_action
Definition: Step.h:58
@ type_if
Definition: Step.h:58
@ type_else
Definition: Step.h:58
@ type_while
Definition: Step.h:58
@ type_elseif
Definition: Step.h:58
@ type_try
Definition: Step.h:58
Step(Type type=type_action)
Construct a Step of a certain type.
Definition: Step.h:67
const std::string & get_script() const
Return the script.
Definition: Step.h:171
void copy_used_variables_from_lua_to_context(const sol::state &lua, Context &context)
Copy the variables listed in used_context_variable_names_ from a Lua state into the given Context.
Definition: Step.cc:80
TimePoint time_of_last_execution_
Definition: Step.h:271
TimePoint time_of_last_modification_
Definition: Step.h:270
Step & set_script(const std::string &script)
Set the script that should be executed when this step is run.
Definition: Step.cc:239
std::string script_
Definition: Step.h:268
Timeout timeout_
Definition: Step.h:272
Step & set_time_of_last_execution(TimePoint t)
Set the timestamp of the last execution of this step's script.
Definition: Step.cc:246
Step & set_used_context_variable_names(const VariableNames &used_context_variable_names)
Set the names of the variables that should be im-/exported from/to the script.
Definition: Step.cc:271
Type type_
Definition: Step.h:273
Step & set_indentation_level(short level)
Set the indentation level of this step.
Definition: Step.cc:211
TimePoint get_time_of_last_modification() const
Return the timestamp of the last modification of this step's script or label.
Definition: Step.h:185
bool is_disabled() const noexcept
Return whether this step is currently disabled.
Definition: Step.h:201
VariableNames used_context_variable_names_
Definition: Step.h:269
bool execute(Context &context, CommChannel *comm_channel=nullptr, OptionalStepIndex opt_step_index=gul14::nullopt, TimeoutTrigger *sequence_timeout=nullptr)
Execute the step script within the given context, sending status information to a message queue.
Definition: Step.cc:167
Step & set_label(const std::string &label)
Set the label.
Definition: Step.cc:226
std::string label_
Definition: Step.h:267
Step & set_timeout(Timeout timeout)
Set the timeout duration for executing the script.
Definition: Step.cc:258
const VariableNames & get_used_context_variable_names() const
Retrieve the names of the variables that should be im-/exported to and from the context.
Definition: Step.h:133
short indentation_level_
Definition: Step.h:274
bool is_running_
Definition: Step.h:275
Evaluates when the clock is elapsed.
Definition: TimeoutTrigger.h:49
A type for storing a timeout duration.
Definition: Timeout.h:72
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33
bool executes_script(Step::Type step_type)
Determine if a script is executed.
Definition: Step.cc:304
const ExecutionSteps execution_steps
Step types that execute a script.
Definition: Step.h:302
std::set< Step::Type > ExecutionSteps
Alias for a step type collection that executes a script.
Definition: Step.h:299
std::string to_string(Step::Type type)
Return a lower-case name for a step type ("action", "if", "end").
Definition: Step.cc:287
bool requires_bool_return_value(Step::Type step_type) noexcept
Determine if a certain step type requires a boolean return value from the script.
Definition: Step.cc:309
std::set< VariableName > VariableNames
Definition: Step.h:41
gul14::optional< StepIndex > OptionalStepIndex
An optional step index (gul14::optional<StepIndex>).
Definition: StepIndex.h:37
std::chrono::time_point< Clock > TimePoint
Definition: time_types.h:34
A struct combining a message queue and several atomic flags.
Definition: CommChannel.h:43
A context stores information that influences the execution of steps and sequences,...
Definition: Context.h:128
Declaration of time-related types.