-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActorSystem.h
96 lines (72 loc) · 2.97 KB
/
ActorSystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#pragma once
#include "RxActor/CommonDefines.h"
#include "RxActor/Supervisor.h"
#include "RxActor/Export.h"
#include "Actor.h"
namespace RxActor {
typedef Templates::Key2<DomainId, ScopeId> ActorSystemHandle;
typedef Templates::ObjectsManager<ActorPath, ActorBasePtr, ActorSystemHandle> ActorScopeManager;
typedef std::shared_ptr<ActorScopeManager> ActorScopeManagerPtr;
typedef Collection::details::StdMapCollectionType<ActorSystemHandle, ActorScopeManagerPtr> LookupActorManager;
class DLL_STATE ActorSystem
: public Templates::FactorySingleton<ActorSystem>
, protected Templates::ContextDataShared<LookupActorManager>
{
public:
ActorSystem() : Templates::ContextDataShared<LookupActorManager>()
{ }
virtual ~ActorSystem()
{ }
// -------------------------------------------------
// Activation functions. TODO: Which vocabulary makes sense for Actors?
// -------------------------------------------------
bool Activate(ActorId handle);
bool Deactivate(ActorId handle);
bool IsActive(ActorId handle);
bool Activate(ActorPath handle);
bool Deactivate(ActorPath handle);
bool IsActive(ActorPath handle);
// -------------------------------------------------
// Create functions
// -------------------------------------------------
template <typename T>
std::shared_ptr<details::Actor<T>> ActorOf(ChannelHandle handle, Supervisor supervisor, ActorPolicy policy)
{
ActorScopeManagerPtr manager = this->context()->GetOrCreate(
ActorSystemHandle{handle.first(), handle.second()},
[&handle]()
{
std::shared_ptr<ActorScopeManager> instance = std::make_shared<ActorScopeManager>(ActorSystemHandle{handle.first(), handle.second()});
bool initialized = instance->Initialize();
ASSERT(initialized);
return instance;
}
);
ActorBasePtr actor = manager->GetOrCreate(
handle.third(),
[&handle, &supervisor, &policy]()
{
std::shared_ptr<details::Actor<T>> actor = std::make_shared<details::Actor<T>>(handle, ActorId::Create(), supervisor, policy);
bool initialized = actor->Initialize();
ASSERT(initialized);
return actor;
}
);
return std::dynamic_pointer_cast<details::Actor<T>>(actor);
}
// ---------------------------------------------
// Print data
// ---------------------------------------------
std::string ToString() const;
friend std::ostream& operator<<(std::ostream& ostr, const ActorSystem& proxy)
{
ostr << proxy.ToString();
return ostr;
}
friend std::ostream& operator<<(std::ostream& ostr, const std::shared_ptr<ActorSystem>& proxy)
{
ostr << proxy->ToString();
return ostr;
}
};
}