25 #ifndef TASKOLIB_SEQUENCE_H_
26 #define TASKOLIB_SEQUENCE_H_
33 #include <string_view>
38 #include <gul17/cat.h>
39 #include <gul17/finalizer.h>
40 #include <gul17/traits.h>
438 template <
typename StepType, std::enable_if_t<
439 std::is_same<gul17::remove_cvref_t<StepType>,
Step>::value,
bool> =
true>
445 auto return_iter =
steps_.insert(iter, std::forward<StepType>(step));
450 if (insert_idx <= error_idx)
451 return error_idx + 1;
502 template <
typename Closure>
508 const auto it =
steps_.begin() + (iter -
steps_.cbegin());
511 auto indent_if_necessary = gul17::finally(
514 old_indentation_level = it->get_indentation_level(),
515 old_type = it->get_type(),
516 old_disabled = it->is_disabled()]()
518 if (it->get_type() != old_type
519 || it->get_indentation_level() != old_indentation_level)
526 if (it->is_disabled() ==
false && old_disabled ==
true)
528 if (it->get_type() == Step::type_if
529 || it->get_type() == Step::type_while
530 || it->get_type() == Step::type_try)
532 auto it_end = find_end_of_continuation(it);
533 std::for_each(it, it_end, [](Step& st) { st.set_disabled(false); });
540 modification_fct(*it);
570 void push_back(
const Step& step);
580 void push_back(
Step&& step);
612 void set_error(std::optional<Error> opt_error);
626 void set_label(std::string_view label);
639 void set_maintainers(std::string_view maintainers);
655 void set_running(
bool running) noexcept { is_running_ = running; }
666 void set_step_setup_script(std::string_view step_setup_script);
673 void set_tags(
const std::vector<Tag>& tags);
681 void set_autorun(
bool autorun);
684 void set_disabled(
bool disabled =
true);
716 bool autorun_{
false } ;
717 bool is_disabled_{
false };
720 bool is_running_{
false };
783 void correct_error_index(
798 void enforce_consistency_of_disabled_flags() noexcept;
806 void enforce_invariants();
919 std::string_view exec_block_name,
934 void throw_if_full() const;
937 void throw_if_running() const;
940 void throw_if_disabled() const;
946 void throw_syntax_error_for_step(
ConstIterator it,
std::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:43
A sequence of steps that can be modified and executed.
Definition: Sequence.h:159
const SequenceName & get_name() const noexcept
Return the machine-friendly name of the sequence.
Definition: Sequence.h:395
std::string step_setup_script_
Step setup script.
Definition: Sequence.h:714
ConstReverseIterator rbegin() const noexcept
Return a constant reverse iterator to the last Step in the container.
Definition: Sequence.h:588
std::optional< Error > get_error() const
Return an optional Error object explaining why the sequence stopped prematurely.
Definition: Sequence.h:371
const Step & operator[](SizeType idx) const noexcept
Access the step at a given index.
Definition: Sequence.h:550
Timeout get_timeout() const
Return the timeout duration for executing the sequence.
Definition: Sequence.h:420
void enforce_invariants()
Make sure that all class invariants are upheld.
Definition: Sequence.cc:282
std::string maintainers_
One or more maintainers.
Definition: Sequence.h:713
bool is_disabled() const noexcept
Return the disable flag. When set to true it will prohibit any execution.
Definition: Sequence.h:407
std::string indentation_error_
Empty if indentation is correct and complete, error message otherwise.
Definition: Sequence.h:708
std::optional< Error > execute(Context &context, CommChannel *comm_channel, OptionalStepIndex opt_step_index=std::nullopt)
Execute the sequence (or just one of its steps) within a given context.
Definition: Sequence.cc:337
void throw_if_full() const
Throw an Error if no further steps can be inserted into the sequence.
Definition: Sequence.cc:771
ConstReverseIterator rend() const noexcept
Return a constant reverse iterator pointing before the first Step in the container.
Definition: Sequence.h:596
UniqueId get_unique_id() const noexcept
Return the unique ID of the sequence.
Definition: Sequence.h:423
void set_running(bool running) noexcept
Set the sequence into the state "is running" (true) or "is not running" (false).
Definition: Sequence.h:655
void throw_if_running() const
When the sequence is executed it rejects with an Error exception.
Definition: Sequence.cc:777
Sequence(std::string_view label="", SequenceName name=SequenceName{}, UniqueId uid=UniqueId{})
Construct an empty sequence.
Definition: Sequence.cc:68
bool is_running() const noexcept
Retrieve if the sequence is executed.
Definition: Sequence.h:465
ConstReverseIterator crbegin() const noexcept
Return a constant reverse iterator to the last Step in the container.
Definition: Sequence.h:239
TimeoutTrigger timeout_trigger_
Logic to check for elapsed sequence timeout.
Definition: Sequence.h:722
bool is_disabled_
Disabled sequence. Used for execution control.
Definition: Sequence.h:717
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:386
TimePoint get_time_of_last_execution() const
Determine when the sequence was last executed.
Definition: Sequence.h:414
SequenceName name_
Machine-readable name.
Definition: Sequence.h:711
StepIndex SizeType
Alias for step type.
Definition: Sequence.h:162
void modify(ConstIterator iter, Closure modification_fct)
Modify a step inside the sequence.
Definition: Sequence.h:503
const std::string & get_maintainers() const noexcept
Return a string listing the maintainer(s) of the sequence.
Definition: Sequence.h:392
bool autorun_
Flag for automatic execution.
Definition: Sequence.h:716
void set_timeout(Timeout timeout)
Set the timeout duration for executing the sequence.
Definition: Sequence.h:687
const std::string & get_step_setup_script() const noexcept
Return the step setup script.
Definition: Sequence.h:398
void set_unique_id(UniqueId uid)
Set the unique ID of the sequence.
Definition: Sequence.h:695
ConstReverseIterator crend() const noexcept
Return a constant reverse iterator pointing before the first Step in the container.
Definition: Sequence.h:247
const std::vector< Tag > & get_tags() const noexcept
Return the tags associated with this sequence in alphabetical order.
Definition: Sequence.h:401
bool empty() const noexcept
Determine whether the sequence contains no steps.
Definition: Sequence.h:269
std::vector< Tag > tags_
Tags for categorizing the sequence.
Definition: Sequence.h:715
static constexpr std::size_t max_label_length
Maximum number of bytes of a Sequence label.
Definition: Sequence.h:171
std::vector< Step >::const_reverse_iterator ConstReverseIterator
Alias for a constant reverse vector iterator.
Definition: Sequence.h:168
static SizeType max_size() noexcept
Return the maximum number of steps that a sequence can hold.
Definition: Sequence.h:471
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:222
std::vector< Step > steps_
Collection of steps.
Definition: Sequence.h:718
ConstIterator end() const noexcept
Return a constant iterator pointing past the last Step in the container.
Definition: Sequence.h:277
UniqueId unique_id_
Unique ID.
Definition: Sequence.h:710
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:235
bool is_timeout_elapsed() const
Return true if the timeout is elapsed otherwise false.
Definition: Sequence.h:468
std::filesystem::path get_folder() const
Return the (relative) folder name associated with this sequence.
Definition: Sequence.cc:368
std::string label_
Human-readable sequence label.
Definition: Sequence.h:712
ConstIterator begin() const noexcept
Return a constant iterator to the first Step in the container.
Definition: Sequence.h:215
ConstIterator cend() const noexcept
Return a constant iterator pointing past the last Step in the container.
Definition: Sequence.h:231
void set_name(SequenceName name)
Set the machine-friendly sequence name.
Definition: Sequence.h:646
const std::string & get_label() const noexcept
Return the human-readable sequence label.
Definition: Sequence.h:389
std::vector< Step >::iterator Iterator
Alias for a vector iterator.
Definition: Sequence.h:164
bool is_running_
Flag to determine if the sequence is running.
Definition: Sequence.h:720
ConstIterator cbegin() const noexcept
Return a constant iterator to the first Step in the container.
Definition: Sequence.h:223
void check_syntax() const
Validates if the Step 's token are correctly enclosed in a proper way.
Definition: Sequence.cc:91
ConstIterator erase(ConstIterator iter)
Remove a step from the sequence.
Definition: Sequence.cc:288
void assign(ConstIterator iter, const Step &step)
Assign a Step to the sequence entry at the given position.
Definition: Sequence.cc:75
SizeType size() const noexcept
Return the number of steps contained in this sequence.
Definition: Sequence.h:698
bool get_autorun() const noexcept
Return true if an automatic execution can be performed otherwise false.
Definition: Sequence.h:404
std::optional< Error > error_
An optional Error object describing why the Sequence stopped prematurely (if it has a value) or that ...
Definition: Sequence.h:705
std::vector< Step >::const_iterator ConstIterator
Alias for a constant vector iterator.
Definition: Sequence.h:166
ConstIterator insert(ConstIterator iter, StepType step)
Insert the given Step into the sequence just before the specified iterator.
Definition: Sequence.h:440
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:39
Definition of the Error exception class.
Definition: VariableName.h:156
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
std::optional< StepIndex > OptionalStepIndex
An optional step index (std::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