Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #pragma once
16 : :
17 : : #include <hpactor/actor/typed_message.hpp>
18 : : #include <hpactor/cli/cli_types.hpp>
19 : : #include <hpactor/ref/actor_address.hpp>
20 : : #include <hpactor/sched/dispatch_policy.hpp>
21 : : #include <hpactor/types/types.hpp>
22 : :
23 : : #include <memory>
24 : : #include <string>
25 : : #include <string_view>
26 : : #include <vector>
27 : :
28 : : namespace hpactor {
29 : :
30 : : // Forward declarations
31 : : class ActorContext;
32 : : class ActorSystem;
33 : : namespace net {
34 : : enum class OpType : uint32_t;
35 : : } // namespace net
36 : :
37 : : class LifecycleActor;
38 : :
39 : : namespace sched {
40 : : class IScheduler;
41 : : } // namespace sched
42 : : namespace mailbox {
43 : : template <typename T> class MPSCActorMailbox;
44 : : } // namespace mailbox
45 : :
46 : : // -----------------------------------------------------------------------------
47 : : // AbstractActor - base class for all actors
48 : : // -----------------------------------------------------------------------------
49 : : class AbstractActor : public std::enable_shared_from_this<AbstractActor> {
50 : : public:
51 : 125 : virtual ~AbstractActor() = default;
52 : :
53 : 556 : ActorId id() const {
54 : 556 : return id_;
55 : : }
56 : 125 : const ActorId* id_ptr() const {
57 : 125 : return &id_;
58 : : }
59 : 118 : ActorType type() const {
60 : 118 : return type_;
61 : : }
62 : 1075 : ActorAddress address() const {
63 : 1075 : return address_;
64 : : }
65 : 1082 : ActorSystem& system() {
66 : 1082 : return system_;
67 : : }
68 : : const ActorSystem& system() const {
69 : : return system_;
70 : : }
71 : :
72 : : // Set actor address (called by ActorSystem during spawn)
73 : 118 : void set_address(ActorAddress addr) {
74 : 118 : address_ = addr;
75 : 118 : id_ = addr.id;
76 : 118 : type_ = addr.type;
77 : 118 : }
78 : :
79 : : // Set scheduler and mailbox (called by ActorSystem during spawn)
80 : : virtual void set_scheduler(sched::IScheduler* scheduler);
81 : : virtual void set_mailbox(mailbox::MPSCActorMailbox<TypedMessage>* mailbox);
82 : :
83 : : // Linking - death sharing
84 : : void link_to(const ActorAddr& other);
85 : : void unlink_from(const ActorAddr& other);
86 : :
87 : : // Monitoring - receive down messages
88 : : void monitor(const ActorAddr& target);
89 : : void demonitor(const ActorAddr& target);
90 : :
91 : : // Receive message (called by scheduler)
92 : : virtual void receive(TypedMessage& msg) = 0;
93 : :
94 : : // Type query for safe downcasting without RTTI
95 : 0 : virtual bool is_event_based_actor() const {
96 : 0 : return false;
97 : : }
98 : :
99 : : // Lifecycle query — RTTI-free downcast to LifecycleActor mixin.
100 : : // Returns nullptr for actors that don't opt into lifecycle management.
101 : 1070 : virtual LifecycleActor* as_lifecycle() {
102 : 1070 : return nullptr;
103 : : }
104 : 1 : virtual const LifecycleActor* as_lifecycle() const {
105 : 1 : return nullptr;
106 : : }
107 : :
108 : : // Returns true for system actors that should drain last during node
109 : : // shutdown. MetricsActor, CliActor, SpawnReceiver override this to return
110 : : // true.
111 : 13 : virtual bool is_system_actor() const {
112 : 13 : return false;
113 : : }
114 : :
115 : : // Dispatch policy — tells the scheduler how to execute this actor.
116 : : // Default: Cooperative (M:N work-stealing pool).
117 : 107 : virtual sched::DispatchPolicy dispatch_policy() const {
118 : 107 : return sched::DispatchPolicy::Cooperative;
119 : : }
120 : 13 : virtual sched::DispatchHints dispatch_hints() const {
121 : 13 : return {};
122 : : }
123 : :
124 : 110 : virtual std::string_view type_name() const {
125 : 110 : return type_name_;
126 : : }
127 : 118 : void set_type_name(std::string name) {
128 : 118 : type_name_ = std::move(name);
129 : 118 : }
130 : :
131 : 4 : virtual void set_metrics_ring_buffer(void* /*buf*/) {}
132 : 4 : virtual void set_logger(void* /*logger*/) noexcept {}
133 : :
134 : : // CLI introspection interface.
135 : : // Returns lightweight inspectable metadata. Called from actor's own thread.
136 : : virtual cli::ActorMeta to_metadata() const;
137 : :
138 : : // Returns opaque serialized state blob. Default empty.
139 : 0 : virtual std::vector<uint8_t> serialize_state() const {
140 : 0 : return {};
141 : : }
142 : :
143 : : // Returns mailbox snapshot. Default empty. Override in mailbox-owning
144 : : // actors.
145 : 0 : virtual cli::MboxSnapshot mailbox_snapshot() const {
146 : 0 : return {};
147 : : }
148 : :
149 : : protected:
150 : : AbstractActor(ActorId id, ActorType type, ActorSystem& sys);
151 : :
152 : : // Overridden by LocalActor to return the ActorContext.
153 : : // Returns nullptr for actors without a context (e.g., system actor).
154 : 0 : virtual ActorContext* actor_context() {
155 : 0 : return nullptr;
156 : : }
157 : :
158 : : private:
159 : : ActorId id_;
160 : : ActorType type_;
161 : : ActorSystem& system_;
162 : : ActorAddress address_;
163 : : std::string type_name_;
164 : : };
165 : :
166 : : } // namespace hpactor
|