Taskolib
1.3.3
|
Namespace task contains all Taskolib functions and classes.
Namespaces | |
literals | |
Classes | |
struct | CommChannel |
A struct combining a message queue and several atomic flags. More... | |
struct | Context |
A context stores information that influences the execution of steps and sequences, namely: More... | |
class | Error |
An exception class carrying an error message and, optionally, the index of the step in which the error occurred. More... | |
class | Executor |
An executor runs a copy of a given Sequence (or just a single step within it) in a separate thread, receives messages from it, and updates the local instance of the Sequence accordingly. More... | |
class | LockedQueue |
A thread-safe locking message queue. More... | |
class | Message |
A message carrying some text, a timestamp, and a type, to be transported with a message queue between threads. More... | |
class | Sequence |
A sequence of steps that can be modified and executed. More... | |
class | SequenceManager |
A class for listing, loading, storing, and renaming sequences in a given file system directory. More... | |
class | SequenceName |
The machine-readable name of a Sequence. More... | |
class | Step |
A step is the main building block of a sequence. More... | |
class | Tag |
A tag used for categorizing sequences. More... | |
class | Timeout |
A type for storing a timeout duration. More... | |
class | TimeoutTrigger |
Evaluates when the clock is elapsed. More... | |
class | UniqueId |
An unsigned 64-bit integer for use as a unique ID. More... | |
class | VariableName |
A variable name is a string with limited functionality and some limitations on the allowed characters. More... | |
Typedefs | |
using | LuaInteger = LUA_INTEGER |
The integer type used by the Lua interpreter. More... | |
using | LuaFloat = LUA_NUMBER |
The floating point type used by the Lua interpreter. More... | |
using | LuaString = std::string |
The string type used by the Lua interpreter. More... | |
using | LuaBool = bool |
The boolean type used by the Lua interpreter. More... | |
using | VarInteger = long long |
Storage type for integral numbers. More... | |
using | VarFloat = double |
Storage type for floatingpoint number. More... | |
using | VarString = std::string |
Storage type for strings. More... | |
using | VarBool = bool |
Storage type for booleans. More... | |
using | VariableValue = std::variant< VarInteger, VarFloat, VarString, VarBool > |
A VariableValue is a variant over all Variable types. More... | |
using | VariableTable = std::unordered_map< VariableName, VariableValue > |
Associative table that holds Lua variable names and their value. More... | |
using | MessageCallback = std::function< void(const Message &)> |
A message callback function receives a Message object as a parameter. More... | |
using | VariableNames = std::set< VariableName > |
using | ExecutionSteps = std::set< Step::Type > |
Alias for a step type collection that executes a script. More... | |
using | StepIndex = std::uint16_t |
A type for storing the index of a Step in a Sequence. More... | |
using | OptionalStepIndex = gul14::optional< StepIndex > |
An optional step index (gul14::optional<StepIndex>). More... | |
using | Clock = std::chrono::system_clock |
using | TimePoint = std::chrono::time_point< Clock > |
Enumerations | |
enum class | ErrorCause { terminated_by_script , aborted , uncaught_error } |
An enum detailing the possible causes of the termination of a sequence. More... | |
Functions | |
void | default_message_callback (const Message &msg) |
Default callback function for messages. More... | |
gul14::expected< sol::object, std::string > | execute_lua_script (sol::state &lua, sol::string_view script) |
Execute a Lua script safely, intercepting all possible exceptions that may occur during its execution. More... | |
gul14::expected< sol::load_result, std::string > | load_lua_script (sol::state &lua, sol::string_view script) |
Load a Lua script into the given Lua state and check its syntax without running it. More... | |
constexpr unsigned long | hash_djb2a (gul14::string_view sv) |
Adapt switch statement with stringify items: https://learnmoderncpp.com/2020/06/01/strings-as-switch-case-labels/ Version 1.4: 2022/01/15, MIT license, (c) 2020-22 Richard Spencer. More... | |
constexpr unsigned long | operator""_sh (const char *str, std::size_t len) |
Adapt switch statement with stringify items: https://learnmoderncpp.com/2020/06/01/strings-as-switch-case-labels/ Version 1.4: 2022/01/15, MIT license, (c) 2020-22 Richard Spencer. More... | |
std::string | to_string (Step::Type type) |
Return a lower-case name for a step type ("action", "if", "end"). More... | |
bool | executes_script (Step::Type step_type) |
Determine if a script is executed. More... | |
bool | requires_bool_return_value (Step::Type step_type) noexcept |
Determine if a certain step type requires a boolean return value from the script. More... | |
std::string | to_string (task::TimePoint t) |
std::time_t | timegm (const std::tm &t) |
std::ostream & | operator<< (std::ostream &stream, const Timeout &timeout) |
std::istream & | operator>> (std::istream &stream, Step &step) |
Deserialize parameters of Step from the input stream. More... | |
Step | load_step (const std::filesystem::path &lua_file) |
Read a Step from a file and return it. More... | |
void | load_sequence_parameters (const std::filesystem::path &folder, Sequence &sequence) |
Load sequence parameters like the step setup script and the sequence timeout. More... | |
std::vector< Tag > | parse_tags (gul14::string_view str) |
Parse a whitespace-separated string into a list of tags. More... | |
bool | parse_bool (gul14::string_view str) |
Parse a bool expression from a string. More... | |
Timeout | parse_timeout (gul14::string_view str) |
Parse a string into a Timeout value. More... | |
std::string | beautify_message (gul14::string_view msg) |
void | check_for_control_characters (gul14::string_view str) |
Throw an exception if the string contains control characters. More... | |
std::pair< std::string, ErrorCause > | remove_abort_markers (gul14::string_view error_message) |
Remove abort markers from the given error message, beautify it, and determine the cause of the error. More... | |
void | abort_script_with_error (lua_State *lua_state, const std::string &msg) |
void | check_immediate_termination_request (lua_State *lua_state) |
void | check_script_timeout (lua_State *lua_state) |
CommChannel * | get_comm_channel_ptr_from_registry (lua_State *lua_state) |
Retrieve a pointer to the used CommChannel from the Lua registry. More... | |
const Context & | get_context_from_registry (lua_State *lua_state) |
Retrieve a reference to the used Context from the Lua registry. More... | |
OptionalStepIndex | get_step_idx_from_registry (lua_State *lua_state) |
Get the index of the currently executed Step from the Lua registry. More... | |
LuaInteger | get_ms_since_epoch (TimePoint t0, std::chrono::milliseconds dt) |
void | hook_check_timeout_and_termination_request (lua_State *lua_state, lua_Debug *) |
void | hook_abort_with_error (lua_State *lua_state, lua_Debug *) |
void | install_custom_commands (sol::state &lua) |
Install implementations for some custom functions in the given Lua state. More... | |
void | install_timeout_and_termination_request_hook (sol::state &lua, TimePoint now, std::chrono::milliseconds timeout, OptionalStepIndex step_idx, const Context &context, CommChannel *comm_channel, TimeoutTrigger *sequence_timeout) |
void | open_safe_library_subset (sol::state &lua) |
void | print_fct (sol::this_state sol, sol::variadic_args va) |
void | sleep_fct (double seconds, sol::this_state sol) |
void | send_message (Message::Type type, gul14::string_view text, TimePoint timestamp, OptionalStepIndex index, const Context &context, CommChannel *comm_channel=nullptr) |
Call the message callback and enqueue the message in the given communication channel, if any. More... | |
std::string | make_sequence_filename (SequenceName sequence_name, UniqueId unique_id) |
Return a filename for the given combination of machine-friendly sequence name and unique ID. More... | |
std::ostream & | operator<< (std::ostream &stream, const Step &step) |
Serialize parameters of Step to the output stream. More... | |
void | store_step (const std::filesystem::path &lua_file, const Step &step) |
Store a Step in a file. More... | |
std::ostream & | operator<< (std::ostream &stream, const Sequence &sequence) |
Serialize parameters of Sequence to the output stream. More... | |
std::ostream & | operator<< (std::ostream &stream, const Tag &tag) |
std::string | to_string (UniqueId uid) |
Variables | |
const ExecutionSteps | execution_steps |
Step types that execute a script. More... | |
const gul14::string_view | abort_marker { u8"\U0001F6D1ABORT\U0001F6D1" } |
A marker string (the word "ABORT" surrounded by Unicode stop signs) whose presence anywhere in an error message signals that the execution of a script should be stopped. More... | |
const char | sequence_lua_filename [] = "sequence.lua" |
Define the Lua sequence filename for storing and loading Lua script. More... | |
using task::Clock = typedef std::chrono::system_clock |
using task::ExecutionSteps = typedef std::set<Step::Type> |
Alias for a step type collection that executes a script.
using task::LuaBool = typedef bool |
The boolean type used by the Lua interpreter.
using task::LuaFloat = typedef LUA_NUMBER |
The floating point type used by the Lua interpreter.
using task::LuaInteger = typedef LUA_INTEGER |
The integer type used by the Lua interpreter.
using task::LuaString = typedef std::string |
The string type used by the Lua interpreter.
using task::MessageCallback = typedef std::function<void(const Message&)> |
A message callback function receives a Message object as a parameter.
It is called on the main thread whenever a message is being processed.
using task::OptionalStepIndex = typedef gul14::optional<StepIndex> |
An optional step index (gul14::optional<StepIndex>).
using task::StepIndex = typedef std::uint16_t |
using task::TimePoint = typedef std::chrono::time_point<Clock> |
using task::VarBool = typedef bool |
Storage type for booleans.
using task::VarFloat = typedef double |
Storage type for floatingpoint number.
using task::VariableNames = typedef std::set<VariableName> |
using task::VariableTable = typedef std::unordered_map<VariableName, VariableValue> |
Associative table that holds Lua variable names and their value.
The keys are of type VariableName and the values VariableValue.
using task::VariableValue = typedef std::variant< VarInteger, VarFloat, VarString, VarBool> |
A VariableValue is a variant over all Variable types.
Variable names are associated with these values via a map in the Context class.
Be careful when assigning a string to a VariableValue: Do not use a char* to pass the string, it might be converted to bool instead of the expected std::string. The conversion depends on the used compiler (version).
using task::VarInteger = typedef long long |
Storage type for integral numbers.
using task::VarString = typedef std::string |
Storage type for strings.
|
strong |
void task::abort_script_with_error | ( | lua_State * | lua_state, |
const std::string & | msg | ||
) |
References abort_marker, and hook_abort_with_error().
Referenced by check_immediate_termination_request(), check_script_timeout(), install_custom_commands(), and print_fct().
std::string task::beautify_message | ( | gul14::string_view | msg | ) |
Referenced by remove_abort_markers().
void task::check_for_control_characters | ( | gul14::string_view | str | ) |
Throw an exception if the string contains control characters.
Error | is thrown if the string contains any control characters. |
Referenced by task::Sequence::set_label(), and task::Sequence::set_maintainers().
void task::check_immediate_termination_request | ( | lua_State * | lua_state | ) |
void task::check_script_timeout | ( | lua_State * | lua_state | ) |
References abort_script_with_error().
Referenced by hook_check_timeout_and_termination_request().
void task::default_message_callback | ( | const Message & | msg | ) |
Default callback function for messages.
It sends "output" messages to stdout. All other message types are ignored.
References task::Message::get_text(), task::Message::get_type(), and task::Message::output.
gul14::expected< sol::object, std::string > task::execute_lua_script | ( | sol::state & | lua, |
sol::string_view | script | ||
) |
Execute a Lua script safely, intercepting all possible exceptions that may occur during its execution.
This function returns a gul14::expected object: If the Lua script finishes without error, it contains a sol::object representing the return value of the script. If a Lua error or C++ exception occurs, the returned object contains a string with an error message. The error message is pre-processed to a certain degree: Unhelpful parts like the chunk name of the script ([string "..."]:
) are removed, and a few known special messages are replaced by more readable explanations.
Referenced by task::Step::execute_impl().
bool task::executes_script | ( | Step::Type | step_type | ) |
Determine if a script is executed.
step_type | step type to check |
References execution_steps.
Referenced by task::Step::execute_impl().
CommChannel * task::get_comm_channel_ptr_from_registry | ( | lua_State * | lua_state | ) |
Retrieve a pointer to the used CommChannel from the Lua registry.
The pointer can be null to indicate that no CommChannel is used.
Error | is thrown if the appropriate registry key is not found. |
Referenced by check_immediate_termination_request(), and print_fct().
const Context & task::get_context_from_registry | ( | lua_State * | lua_state | ) |
Retrieve a reference to the used Context from the Lua registry.
Error | is thrown if the appropriate registry key is not found or if it contains a null pointer. |
Referenced by print_fct().
LuaInteger task::get_ms_since_epoch | ( | TimePoint | t0, |
std::chrono::milliseconds | dt | ||
) |
Referenced by install_timeout_and_termination_request_hook().
OptionalStepIndex task::get_step_idx_from_registry | ( | lua_State * | lua_state | ) |
Get the index of the currently executed Step from the Lua registry.
This function has three different outcomes:
Error | is thrown if the appropriate registry key is not found. |
Referenced by print_fct().
|
inlineconstexpr |
Adapt switch statement with stringify items: https://learnmoderncpp.com/2020/06/01/strings-as-switch-case-labels/ Version 1.4: 2022/01/15, MIT license, (c) 2020-22 Richard Spencer.
Referenced by operator""_sh(), and operator>>().
void task::hook_abort_with_error | ( | lua_State * | lua_state, |
lua_Debug * | |||
) |
Referenced by abort_script_with_error().
void task::hook_check_timeout_and_termination_request | ( | lua_State * | lua_state, |
lua_Debug * | |||
) |
References check_immediate_termination_request(), and check_script_timeout().
Referenced by install_timeout_and_termination_request_hook(), and sleep_fct().
void task::install_custom_commands | ( | sol::state & | lua | ) |
Install implementations for some custom functions in the given Lua state.
References abort_script_with_error(), print_fct(), and sleep_fct().
Referenced by task::Step::execute_impl().
void task::install_timeout_and_termination_request_hook | ( | sol::state & | lua, |
TimePoint | now, | ||
std::chrono::milliseconds | timeout, | ||
OptionalStepIndex | step_idx, | ||
const Context & | context, | ||
CommChannel * | comm_channel, | ||
TimeoutTrigger * | sequence_timeout | ||
) |
References get_ms_since_epoch(), and hook_check_timeout_and_termination_request().
Referenced by task::Step::execute_impl().
gul14::expected< sol::load_result, std::string > task::load_lua_script | ( | sol::state & | lua, |
sol::string_view | script | ||
) |
Load a Lua script into the given Lua state and check its syntax without running it.
This function returns a gul14::expected object: If the syntax of the Lua script is OK, it contains a sol::load_result proxy object that can be called to run the script or cast to a sol::function/sol::protected_function. If the syntax check fails, a string with an error message is returned instead. This error message is pre-processed to a certain degree: Unhelpful parts like the chunk name of the script ([string "..."]:
) are removed, and a few known special messages are replaced by more readable explanations.
void task::load_sequence_parameters | ( | const std::filesystem::path & | folder, |
Sequence & | sequence | ||
) |
Load sequence parameters like the step setup script and the sequence timeout.
folder | of the Sequence. |
sequence | to store the loaded step setup script. |
References parse_bool(), parse_tags(), parse_timeout(), sequence_lua_filename, task::Sequence::set_autorun(), task::Sequence::set_disabled(), task::Sequence::set_label(), task::Sequence::set_maintainers(), task::Sequence::set_step_setup_script(), task::Sequence::set_tags(), and task::Sequence::set_timeout().
Referenced by task::SequenceManager::load_sequence().
Step task::load_step | ( | const std::filesystem::path & | lua_file | ) |
Read a Step from a file and return it.
lua_file | The filename from which to read the step. The file must be a Lua script and should have the extension 'lua'. |
It will throw an Error exception if an I/O error occurs on the external filename object or the file does not exist.
Lua comments in the header of the file are used to read metadata. To load a Step, it must have at least the following properties:
The following properties are optional:
If one of the optional parameters is not set the following is provided as default:
Here is one example of a stored Step step_001_while.lua :
The label is explicitly escaped on storing and unescaped on loading.
time of last modification
is not provided in the file, it is set to the time of loading the step.
[] .Referenced by task::SequenceManager::load_sequence().
std::string task::make_sequence_filename | ( | SequenceName | sequence_name, |
UniqueId | unique_id | ||
) |
Return a filename for the given combination of machine-friendly sequence name and unique ID.
It is guaranteed that the filename does not contain filesystem-unsafe characters like / or .
References task::SequenceName::string(), and to_string().
Referenced by task::Sequence::get_folder(), and task::SequenceManager::rename_sequence().
void task::open_safe_library_subset | ( | sol::state & | lua | ) |
Referenced by task::Step::execute_impl().
|
inlineconstexpr |
Adapt switch statement with stringify items: https://learnmoderncpp.com/2020/06/01/strings-as-switch-case-labels/ Version 1.4: 2022/01/15, MIT license, (c) 2020-22 Richard Spencer.
References hash_djb2a().
std::ostream & task::operator<< | ( | std::ostream & | stream, |
const Sequence & | sequence | ||
) |
Serialize parameters of Sequence to the output stream.
No checking of any stream failure is done and should be performed by the caller.
stream | to serialize the Step |
sequence | to serialize |
References task::Sequence::get_step_setup_script().
std::ostream & task::operator<< | ( | std::ostream & | stream, |
const Step & | step | ||
) |
Serialize parameters of Step to the output stream.
No checking of any stream failure is done and should be performed by the caller.
stream | to serialize the Step |
step | to serialize |
References task::Step::get_label(), task::Step::get_script(), task::Step::get_time_of_last_execution(), task::Step::get_time_of_last_modification(), task::Step::get_timeout(), task::Step::get_type(), task::Step::get_used_context_variable_names(), task::Step::is_disabled(), and to_string().
std::ostream& task::operator<< | ( | std::ostream & | stream, |
const Tag & | tag | ||
) |
|
inline |
std::istream & task::operator>> | ( | std::istream & | stream, |
Step & | step | ||
) |
Deserialize parameters of Step from the input stream.
No checking of any stream failure is done and should be performed by the caller.
stream | input stream |
step | Step to be deserialized. |
References hash_djb2a(), parse_timeout(), task::Step::set_script(), task::Step::set_time_of_last_modification(), and task::Step::set_timeout().
bool task::parse_bool | ( | gul14::string_view | str | ) |
Parse a bool expression from a string.
Referenced by load_sequence_parameters().
std::vector< Tag > task::parse_tags | ( | gul14::string_view | str | ) |
Parse a whitespace-separated string into a list of tags.
Error | is thrown if a tag with an invalid name is encountered. |
Referenced by load_sequence_parameters().
Timeout task::parse_timeout | ( | gul14::string_view | str | ) |
Parse a string into a Timeout value.
The string may contain a positive, integral number of milliseconds or the string "infinite" in lower- or uppercase letters. It may be surrounded by whitespace.
Referenced by load_sequence_parameters(), and operator>>().
void task::print_fct | ( | sol::this_state | sol, |
sol::variadic_args | va | ||
) |
std::pair< std::string, ErrorCause > task::remove_abort_markers | ( | gul14::string_view | error_message | ) |
Remove abort markers from the given error message, beautify it, and determine the cause of the error.
If at least two abort markers are present, the message is truncated to the text between the first two of these markers. Otherwise, all markers are simply removed.
The cause of the error is determined as follows:
If the message contains a Lua stack trace, the function highlights its start by including a UTF-8 bullet point symbol to visually separate the main message text from the stack trace.
References abort_marker, aborted, beautify_message(), terminated_by_script, and uncaught_error.
Referenced by task::Step::execute(), and task::Sequence::handle_execution().
|
noexcept |
Determine if a certain step type requires a boolean return value from the script.
Referenced by task::Step::execute(), and task::Step::execute_impl().
void task::send_message | ( | Message::Type | type, |
gul14::string_view | text, | ||
TimePoint | timestamp, | ||
OptionalStepIndex | index, | ||
const Context & | context, | ||
CommChannel * | comm_channel = nullptr |
||
) |
Call the message callback and enqueue the message in the given communication channel, if any.
type | Message type (see Message::Type) |
text | Message text |
timestamp | Timestamp of the message |
index | An optional index (of a Step in its parent Sequence) |
context | Context with a message callback function |
comm_channel | Pointer to the communication channel. If this is null, the function does not attempt to push the message into any message queue. |
References task::Context::message_callback_function, and task::CommChannel::queue_.
Referenced by task::Step::execute(), task::Sequence::handle_execution(), and print_fct().
void task::sleep_fct | ( | double | seconds, |
sol::this_state | sol | ||
) |
References hook_check_timeout_and_termination_request().
Referenced by install_custom_commands().
void task::store_step | ( | const std::filesystem::path & | lua_file, |
const Step & | step | ||
) |
Store a Step in a file.
This function saves the script of this step in a Lua file. Metadata like the step type or the label are stored as comments in the header of the file:
The Lua script can be omitted for type try, catch, and end as it has no meaning for execution. See Step::execution for more information. The list of context variable names can also be empty.
Here is one example of a stored Step step_001_action.lua
:
The label is explicitly escaped on storing and unescaped on loading.
lua_file | filename under which the step should be stored |
step | the Step object that should be serialized |
Referenced by task::SequenceManager::write_sequence_to_disk().
std::time_t task::timegm | ( | const std::tm & | t | ) |
std::string task::to_string | ( | Step::Type | type | ) |
Return a lower-case name for a step type ("action", "if", "end").
Referenced by task::SequenceManager::copy_sequence(), task::SequenceManager::create_sequence(), task::Sequence::execute(), task::Step::execute_impl(), task::SequenceManager::find_sequence_on_disk(), fmt::formatter< task::TimePoint >::format(), task::SequenceManager::import_sequence(), make_sequence_filename(), operator<<(), and task::SequenceManager::remove_sequence().
std::string task::to_string | ( | task::TimePoint | t | ) |
std::string task::to_string | ( | UniqueId | uid | ) |
const gul14::string_view task::abort_marker { u8"\U0001F6D1ABORT\U0001F6D1" } |
A marker string (the word "ABORT" surrounded by Unicode stop signs) whose presence anywhere in an error message signals that the execution of a script should be stopped.
Referenced by abort_script_with_error(), task::Sequence::execute_range(), task::Sequence::execute_try_block(), and remove_abort_markers().
const ExecutionSteps task::execution_steps |
Step types that execute a script.
Referenced by executes_script().
const char task::sequence_lua_filename[] = "sequence.lua" |
Define the Lua sequence filename for storing and loading Lua script.
Referenced by load_sequence_parameters(), and task::SequenceManager::write_sequence_to_disk().