Merge pull request 'Added ALL_STATES macro' (#24) from ALL_STATES into main

Reviewed-on: https://git.intern.spaceteamaachen.de/ALPAKA/TACOS/pulls/24
Reviewed-by: dario <dario@noreply.git.intern.spaceteamaachen.de>
This commit is contained in:
dario 2024-02-01 08:08:49 +00:00
commit 1a80082a0b
4 changed files with 28 additions and 3 deletions

View File

@ -68,7 +68,7 @@ namespace sta
* @ingroup tacos_api * @ingroup tacos_api
*/ */
template<typename T, typename ... Args> template<typename T, typename ... Args>
std::shared_ptr<T> addThread(std::list<uint16_t> states, Args ... args) std::shared_ptr<T> addThread(std::set<uint16_t> states, Args ... args)
{ {
std::shared_ptr<T> thread_ptr = std::make_shared<T>(args...); std::shared_ptr<T> thread_ptr = std::make_shared<T>(args...);

View File

@ -59,7 +59,7 @@ namespace sta
/** /**
* @brief Register a thread to be managed by the manager. * @brief Register a thread to be managed by the manager.
*/ */
void registerThread(std::shared_ptr<TacosThread> thread, std::list<uint16_t> states); void registerThread(std::shared_ptr<TacosThread> thread, std::set<uint16_t> states);
void init() override; void init() override;

View File

@ -69,6 +69,23 @@
#include <functional> #include <functional>
#include <tuple> #include <tuple>
#include <set>
#include <algorithm>
/**
* @brief Addresses all states from 0 to STA_TACOS_NUM_STATES-1.
*
* @ingroup tacos_statemachine
*/
#define ALL_STATES std::set<uint16_t>{[]{ std::set<uint16_t> states; for (uint16_t i = 0; i < STA_TACOS_NUM_STATES; ++i) states.insert(i); return states; }() }
/**
* @brief Shorthand to make own sets of states. Primarily for exlcusion via "-" operator.
*
* @ingroup tacos_statemachine
*/
#define state_set std::set<uint16_t>
namespace sta namespace sta
{ {
@ -204,4 +221,12 @@ namespace sta
#endif // STA_TACOS_NUM_STATES #endif // STA_TACOS_NUM_STATES
/// Overwrite "-" operator for set.
template <typename T>
std::set<T> operator-(const std::set<T>& set1, const std::set<T>& set2) {
std::set<T> result;
std::set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), std::inserter(result, result.begin()));
return result;
}
#endif /* INCLUDE_TACOS_STATEMACHINE_HPP_ */ #endif /* INCLUDE_TACOS_STATEMACHINE_HPP_ */

View File

@ -18,7 +18,7 @@ namespace sta
{ {
namespace tacos namespace tacos
{ {
void Manager::registerThread(std::shared_ptr<TacosThread> thread, std::list<uint16_t> states) void Manager::registerThread(std::shared_ptr<TacosThread> thread, std::set<uint16_t> states)
{ {
for (uint16_t state : states) for (uint16_t state : states)
{ {