Created
May 15, 2023 01:08
-
-
Save amencke/3f5529bc0d87ef053d2c5c22dd34d476 to your computer and use it in GitHub Desktop.
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
struct NullType | |
{ | |
}; | |
template <typename... Ts> | |
struct TypeList; | |
template <typename Head, typename... Tail> | |
struct TypeList<Head, Tail...> | |
{ | |
using H = Head; | |
using T = TypeList<Tail...>; | |
}; | |
template <typename Head> | |
struct TypeList<Head> | |
{ | |
using H = Head; | |
using T = NullType; | |
}; | |
template<> | |
struct TypeList<> | |
{}; | |
template <template <typename M> typename B, class TList> | |
class ActorBehaviorScatterHierarchy; | |
template <template <typename M> typename B, class T1, class... Ts> | |
class ActorBehaviorScatterHierarchy<B, TypeList<T1, Ts...>> | |
: public ActorBehaviorScatterHierarchy<B, T1> | |
, public ActorBehaviorScatterHierarchy<B, TypeList<Ts...>> | |
{ | |
public: | |
using TList = TypeList<T1, Ts...>; | |
using LeftBase = ActorBehaviorScatterHierarchy<B, T1>; | |
using RightBase = ActorBehaviorScatterHierarchy<B, TypeList<Ts...>>; | |
}; | |
template <template <typename> typename B, typename AtomicType> | |
class ActorBehaviorScatterHierarchy: public B<AtomicType> | |
{ | |
using LeftBase = B<AtomicType>; | |
}; | |
template <template <typename M> typename B> | |
class ActorBehaviorScatterHierarchy<B, NullType> | |
{ | |
}; | |
// static_actor_test.cpp | |
#include "behavior_scatter_hierarchy_variadic.h" | |
#include <iostream> | |
struct Message1 | |
{ | |
uint32_t x = 5; | |
}; | |
struct Message2 | |
{ | |
float f = 0.123; | |
}; | |
struct Message3 | |
{ | |
std::string s = "hello!"; | |
}; | |
struct Message4 | |
{ | |
const char* s = "something"; | |
}; | |
template <typename Msg> | |
struct Behavior | |
{ | |
Msg message_; | |
}; | |
int main(void) | |
{ | |
using MyActorBehaviorHierarchy = ActorBehaviorScatterHierarchy<Behavior, TypeList<Message1, Message2, Message3, Message4>>; | |
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; | |
std::cout << static_cast<Behavior<Message4>>(a).message_.s << std::endl; | |
// ➜ static_actor ./build/static_actor | |
// 5 | |
// 0.123 | |
// hello! | |
// something | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment