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/config/actor_factory.hpp>
18 : :
19 : : #include <memory>
20 : : #include <string>
21 : : #include <unordered_map>
22 : : #include <vector>
23 : :
24 : : namespace hpactor::config {
25 : :
26 : : // -----------------------------------------------------------------------------
27 : : // ActorFactoryRegistry — singleton registry mapping behavior name strings to
28 : : // ActorFactory functions.
29 : : //
30 : : // Populated at static init time via HPACTOR_REGISTER_ACTOR. Read-only after
31 : : // main() starts (no runtime registration). Thread-safe by immutability.
32 : : // -----------------------------------------------------------------------------
33 : : class ActorFactoryRegistry {
34 : : public:
35 : : static ActorFactoryRegistry& instance();
36 : :
37 : : template <typename T>
38 : : void register_factory(const std::string& name);
39 : :
40 : : ActorFactory get_factory(const std::string& name) const;
41 : : bool has(const std::string& name) const;
42 : : std::vector<std::string> known_names() const;
43 : :
44 : : private:
45 : : std::unordered_map<std::string, ActorFactory> factories_;
46 : : };
47 : :
48 : : // Template implementation
49 : : template <typename T>
50 : 5 : void ActorFactoryRegistry::register_factory(const std::string& name) {
51 : 5 : factories_.emplace(name, [](ActorContext* ctx, ActorSystem& sys) {
52 : 9 : return std::make_shared<T>(ctx, sys);
53 : : });
54 : 5 : }
55 : :
56 : : } // namespace hpactor::config
57 : :
58 : : // -----------------------------------------------------------------------------
59 : : // HPACTOR_REGISTER_ACTOR — static registration macro
60 : : //
61 : : // Place in an actor's .cpp file to register it for TOML-based bootstrapping.
62 : : // Registration happens before main() via static initialization.
63 : : //
64 : : // Example:
65 : : // HPACTOR_REGISTER_ACTOR("TcpGatewayActor", TcpGatewayActor);
66 : : // -----------------------------------------------------------------------------
67 : : #define HPACTOR_REGISTER_ACTOR(Name, ActorClass) \
68 : : namespace { \
69 : : [[maybe_unused]] static const bool _hpactor_reg_##ActorClass = [] { \
70 : : ::hpactor::config::ActorFactoryRegistry::instance() \
71 : : .register_factory<ActorClass>(Name); \
72 : : return true; \
73 : : }(); \
74 : : }
|