Last active
May 13, 2023 22:15
-
-
Save amencke/a0a624dfb4fdfbff352d3b0dec6dae80 to your computer and use it in GitHub Desktop.
Scatter hierarchy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// *************** behavior_scatter_hierarchy.h *************** | |
template <class T, class U> | |
struct TypeList | |
{ | |
using Head = T; | |
using Tail = U; | |
}; | |
struct NullType | |
{ | |
}; | |
template<typename T1> using TYPELIST_1 = TypeList<T1, NullType>; | |
template<typename T1, typename T2> using TYPELIST_2 = TypeList<T1, TYPELIST_1<T2>>; | |
template<typename T1, typename T2, typename T3> using TYPELIST_3 = TypeList<T1, TYPELIST_2<T2, T3>>; | |
template <class TList, template <typename M> typename B> | |
class ActorBehaviorScatterHierarchy; | |
template <class T1, class T2, template <typename M> typename B> | |
class ActorBehaviorScatterHierarchy<TypeList<T1, T2>, B> | |
: public ActorBehaviorScatterHierarchy<T1, B> | |
, public ActorBehaviorScatterHierarchy<T2, B> | |
{ | |
public: | |
using TList = TypeList<T1, T2>; | |
using LeftBase = ActorBehaviorScatterHierarchy<T1, B>; | |
using RightBase = ActorBehaviorScatterHierarchy<T2, B>; | |
}; | |
template <typename AtomicType, template <typename M> typename B> | |
class ActorBehaviorScatterHierarchy: public B<AtomicType> | |
{ | |
using LeftBase = B<AtomicType>; | |
}; | |
template <template <typename M> typename B> | |
class ActorBehaviorScatterHierarchy<NullType, B> | |
{ | |
}; | |
template <typename Msg> | |
struct Behavior | |
{ | |
Msg message_; | |
}; | |
// *************** static_actor_test.cpp *************** | |
#include "behavior_scatter_hierarchy.h" | |
#include <iostream> | |
struct Message1 | |
{ | |
uint32_t x = 5; | |
}; | |
struct Message2 | |
{ | |
float f = 0.123; | |
}; | |
struct Message3 | |
{ | |
std::string s = "hello!"; | |
}; | |
int main(void) | |
{ | |
using MyActorBehaviorHierarchy = ActorBehaviorScatterHierarchy<TYPELIST_3<Message1, Message2, Message3>, Behavior>; | |
MyActorBehaviorHierarchy a; | |
std::cout << static_cast<Behavior<Message1>>(a).message_.x << std::endl; | |
std::cout << static_cast<Behavior<Message2>>(a).message_.f << std::endl; | |
std::cout << static_cast<Behavior<Message3>>(a).message_.s << std::endl; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment