25 #ifndef TASKOLIB_SEQUENCE_H_
26 #define TASKOLIB_SEQUENCE_H_
37 #include <gul14/cat.h>
38 #include <gul14/finalizer.h>
39 #include <gul14/optional.h>
40 #include <gul14/string_view.h>
41 #include <gul14/traits.h>
439 template <
typename StepType, std::enable_if_t<
440 std::is_same<gul14::remove_cvref_t<StepType>,
Step>::value,
bool> =
true>
446 auto return_iter =
steps_.insert(iter, std::forward<StepType>(step));
451 if (insert_idx <= error_idx)
452 return error_idx + 1;
503 template <
typename Closure>
509 const auto it =
steps_.begin() + (iter -
steps_.cbegin());
512 auto indent_if_necessary = gul14::finally(
515 old_indentation_level = it->get_indentation_level(),
516 old_type = it->get_type(),
517 old_disabled = it->is_disabled()]()
519 if (it->get_type() != old_type
520 || it->get_indentation_level() != old_indentation_level)
527 if (it->is_disabled() ==
false && old_disabled ==
true)
529 if (it->get_type() == Step::type_if
530 || it->get_type() == Step::type_while
531 || it->get_type() == Step::type_try)
533 auto it_end = find_end_of_continuation(it);
534 std::for_each(it, it_end, [](Step& st) { st.set_disabled(false); });
541 modification_fct(*it);
571 void push_back(
const Step& step);
581 void push_back(
Step&& step);
613 void set_error(gul14::optional<Error> opt_error);
627 void set_label(gul14::string_view label);
640 void set_maintainers(gul14::string_view maintainers);
656 void set_running(
bool running) noexcept { is_running_ = running; }
667 void set_step_setup_script(gul14::string_view step_setup_script);
674 void set_tags(
const std::vector<Tag>& tags);
682 void set_autorun(
bool autorun);
685 void set_disabled(
bool disabled =
true);
717 bool autorun_{
false } ;
718 bool is_disabled_{
false };
721 bool is_running_{
false };
784 void correct_error_index(
799 void enforce_consistency_of_disabled_flags() noexcept;
807 void enforce_invariants();
918 gul14::optional<
Error>
920 gul14::string_view exec_block_name,
935 void throw_if_full() const;
938 void throw_if_running() const;
941 void throw_if_disabled() const;
947 void throw_syntax_error_for_step(
ConstIterator it, gul14::string_view msg) const;
Declaration of the CommChannel struct.
Declaration of the Context and VariableValue types.
Declaration of the SequenceName class.
Declaration of the StepIndex type.
Declaration of the Step class.
Declaration of the Tag class.
Logic to check if a timeout elapsed.
Declaration of the UniqueId class.
An exception class carrying an error message and, optionally, the index of the step in which the erro...
Definition: exceptions.h:73
The machine-readable name of a Sequence.
Definition: SequenceName.h:44
A sequence of steps that can be modified and executed.
Definition: Sequence.h:160
gul14::optional< Error > execute(Context &context, CommChannel *comm_channel, OptionalStepIndex opt_step_index=gul14::nullopt)
Execute the sequence (or just one of its steps) within a given context.
Definition: Sequence.cc:338
const SequenceName & get_name() const noexcept
Return the machine-friendly name of the sequence.
Definition: Sequence.h:396
std::string step_setup_script_
Step setup script.
Definition: Sequence.h:715
ConstReverseIterator rbegin() const noexcept
Return a constant reverse iterator to the last Step in the container.
Definition: Sequence.h:589
const Step & operator[](SizeType idx) const noexcept
Access the step at a given index.
Definition: Sequence.h:551
Timeout get_timeout() const
Return the timeout duration for executing the sequence.
Definition: Sequence.h:421
void enforce_invariants()
Make sure that all class invariants are upheld.
Definition: Sequence.cc:283
std::string maintainers_
One or more maintainers.
Definition: Sequence.h:714
gul14::optional< Error > get_error() const
Return an optional Error object explaining why the sequence stopped prematurely.
Definition: Sequence.h:372
bool is_disabled() const noexcept
Return the disable flag. When set to true it will prohibit any execution.
Definition: Sequence.h:408
std::string indentation_error_
Empty if indentation is correct and complete, error message otherwise.
Definition: Sequence.h:709
void throw_if_full() const
Throw an Error if no further steps can be inserted into the sequence.
Definition: Sequence.cc:772
ConstReverseIterator rend() const noexcept
Return a constant reverse iterator pointing before the first Step in the container.
Definition: Sequence.h:597
UniqueId get_unique_id() const noexcept
Return the unique ID of the sequence.
Definition: Sequence.h:424
void set_running(bool running) noexcept
Set the sequence into the state "is running" (true) or "is not running" (false).
Definition: Sequence.h:656
void throw_if_running() const
When the sequence is executed it rejects with an Error exception.
Definition: Sequence.cc:778
bool is_running() const noexcept
Retrieve if the sequence is executed.
Definition: Sequence.h:466
ConstReverseIterator crbegin() const noexcept
Return a constant reverse iterator to the last Step in the container.
Definition: Sequence.h:240
TimeoutTrigger timeout_trigger_
Logic to check for elapsed sequence timeout.
Definition: Sequence.h:723
bool is_disabled_
Disabled sequence. Used for execution control.
Definition: Sequence.h:718
const std::string & get_indentation_error() const noexcept
Return an error string if the sequence is not consistently nested, or an empty string if the nesting ...
Definition: Sequence.h:387
TimePoint get_time_of_last_execution() const
Determine when the sequence was last executed.
Definition: Sequence.h:415
SequenceName name_
Machine-readable name.
Definition: Sequence.h:712
StepIndex SizeType
Alias for step type.
Definition: Sequence.h:163
void modify(ConstIterator iter, Closure modification_fct)
Modify a step inside the sequence.
Definition: Sequence.h:504
const std::string & get_maintainers() const noexcept
Return a string listing the maintainer(s) of the sequence.
Definition: Sequence.h:393
bool autorun_
Flag for automatic execution.
Definition: Sequence.h:717
void set_timeout(Timeout timeout)
Set the timeout duration for executing the sequence.
Definition: Sequence.h:688
const std::string & get_step_setup_script() const noexcept
Return the step setup script.
Definition: Sequence.h:399
void set_unique_id(UniqueId uid)
Set the unique ID of the sequence.
Definition: Sequence.h:696
ConstReverseIterator crend() const noexcept
Return a constant reverse iterator pointing before the first Step in the container.
Definition: Sequence.h:248
const std::vector< Tag > & get_tags() const noexcept
Return the tags associated with this sequence in alphabetical order.
Definition: Sequence.h:402
bool empty() const noexcept
Determine whether the sequence contains no steps.
Definition: Sequence.h:270
std::vector< Tag > tags_
Tags for categorizing the sequence.
Definition: Sequence.h:716
static constexpr std::size_t max_label_length
Maximum number of bytes of a Sequence label.
Definition: Sequence.h:172
std::vector< Step >::const_reverse_iterator ConstReverseIterator
Alias for a constant reverse vector iterator.
Definition: Sequence.h:169
static SizeType max_size() noexcept
Return the maximum number of steps that a sequence can hold.
Definition: Sequence.h:472
void correct_error_index(std::function< OptionalStepIndex(StepIndex err_idx)> get_new_index)
If the sequence has a stored error with step index information, call the given function and replace t...
Definition: Sequence.cc:223
std::vector< Step > steps_
Collection of steps.
Definition: Sequence.h:719
gul14::optional< Error > error_
An optional Error object describing why the Sequence stopped prematurely (if it has a value) or that ...
Definition: Sequence.h:706
ConstIterator end() const noexcept
Return a constant iterator pointing past the last Step in the container.
Definition: Sequence.h:278
UniqueId unique_id_
Unique ID.
Definition: Sequence.h:711
void enforce_consistency_of_disabled_flags() noexcept
Update the disabled flag of all steps to ensure that control structures are not partially disabled.
Definition: Sequence.cc:236
bool is_timeout_elapsed() const
Return true if the timeout is elapsed otherwise false.
Definition: Sequence.h:469
std::filesystem::path get_folder() const
Return the (relative) folder name associated with this sequence.
Definition: Sequence.cc:369
std::string label_
Human-readable sequence label.
Definition: Sequence.h:713
ConstIterator begin() const noexcept
Return a constant iterator to the first Step in the container.
Definition: Sequence.h:216
ConstIterator cend() const noexcept
Return a constant iterator pointing past the last Step in the container.
Definition: Sequence.h:232
Sequence(gul14::string_view label="", SequenceName name=SequenceName{}, UniqueId uid=UniqueId{})
Construct an empty sequence.
Definition: Sequence.cc:67
void set_name(SequenceName name)
Set the machine-friendly sequence name.
Definition: Sequence.h:647
const std::string & get_label() const noexcept
Return the human-readable sequence label.
Definition: Sequence.h:390
std::vector< Step >::iterator Iterator
Alias for a vector iterator.
Definition: Sequence.h:165
bool is_running_
Flag to determine if the sequence is running.
Definition: Sequence.h:721
ConstIterator cbegin() const noexcept
Return a constant iterator to the first Step in the container.
Definition: Sequence.h:224
void check_syntax() const
Validates if the Step 's token are correctly enclosed in a proper way.
Definition: Sequence.cc:92
ConstIterator erase(ConstIterator iter)
Remove a step from the sequence.
Definition: Sequence.cc:289
void assign(ConstIterator iter, const Step &step)
Assign a Step to the sequence entry at the given position.
Definition: Sequence.cc:76
SizeType size() const noexcept
Return the number of steps contained in this sequence.
Definition: Sequence.h:699
bool get_autorun() const noexcept
Return true if an automatic execution can be performed otherwise false.
Definition: Sequence.h:405
std::vector< Step >::const_iterator ConstIterator
Alias for a constant vector iterator.
Definition: Sequence.h:167
ConstIterator insert(ConstIterator iter, StepType step)
Insert the given Step into the sequence just before the specified iterator.
Definition: Sequence.h:441
A step is the main building block of a sequence.
Definition: Step.h:53
Evaluates when the clock is elapsed.
Definition: TimeoutTrigger.h:49
const Timeout get_timeout() const
Get the timeout duration.
Definition: TimeoutTrigger.h:68
bool is_elapsed() const
Evaluates if a timeout elapsed.
Definition: TimeoutTrigger.h:92
TimePoint get_start_time() const
Get start time.
Definition: TimeoutTrigger.h:83
A type for storing a timeout duration.
Definition: Timeout.h:72
An unsigned 64-bit integer for use as a unique ID.
Definition: UniqueId.h:40
Definition of the Error exception class.
Definition: VariableName.h:154
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33
std::uint16_t StepIndex
A type for storing the index of a Step in a Sequence.
Definition: StepIndex.h:34
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