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 <functional>
18 : : #include <hpactor/types/types.hpp>
19 : :
20 : : namespace hpactor {
21 : :
22 : : // -----------------------------------------------------------------------------
23 : : // ActorAddress - unique identifier for an actor across the distributed system
24 : : // -----------------------------------------------------------------------------
25 : : struct ActorAddress {
26 : : EndPoint endpoint; // Network location
27 : : ActorType type = 0; // Actor type identifier
28 : : ActorId id; // Unique instance ID
29 : : uint64_t incarnation = 0; // Increments on restart
30 : :
31 : 1243480 : ActorAddress() : endpoint(Ipv4Endpoint{0x7F000001, 0}) {}
32 : 187 : ActorAddress(EndPoint ep, ActorType t, ActorId i, uint64_t inc)
33 : 187 : : endpoint(std::move(ep)), type(t), id(i), incarnation(inc) {}
34 : :
35 : 25 : bool operator==(const ActorAddress& other) const noexcept {
36 : 75 : return endpoint == other.endpoint && type == other.type &&
37 : 75 : id == other.id && incarnation == other.incarnation;
38 : : }
39 : 2 : bool operator!=(const ActorAddress& other) const noexcept {
40 : 2 : return !(*this == other);
41 : : }
42 : : bool is_local() const noexcept;
43 : : [[nodiscard]] std::string to_string() const;
44 : 14 : explicit operator bool() const {
45 : 14 : return id.value() != 0;
46 : : }
47 : :
48 : : public:
49 : 12 : static void hash_combine(size_t& seed, size_t value) noexcept {
50 : 12 : seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2);
51 : 12 : }
52 : : };
53 : :
54 : 2 : inline bool ActorAddress::is_local() const noexcept {
55 : 2 : if (auto* ipv4 = std::get_if<Ipv4Endpoint>(&endpoint)) {
56 : 2 : return ipv4->is_loopback();
57 : : }
58 : 0 : if (auto* ipv6 = std::get_if<Ipv6Endpoint>(&endpoint)) {
59 : 0 : return ipv6->is_loopback();
60 : : }
61 : 0 : return true; // Empty variant is considered local
62 : : }
63 : :
64 : : inline std::string ActorAddress::to_string() const {
65 : : return endpoint_ops::to_string(endpoint);
66 : : }
67 : :
68 : : using ActorAddr = ActorAddress;
69 : : inline const ActorAddr invalid_actor_addr{};
70 : :
71 : : } // namespace hpactor
72 : :
73 : : // -----------------------------------------------------------------------------
74 : : // std::hash specialization for ActorAddress
75 : : // -----------------------------------------------------------------------------
76 : : template <> struct std::hash<hpactor::ActorAddress> {
77 : 4 : std::size_t operator()(const hpactor::ActorAddress& addr) const noexcept {
78 : 4 : std::size_t seed = std::hash<hpactor::EndPoint>{}(addr.endpoint);
79 : 4 : hpactor::ActorAddress::hash_combine(
80 : 4 : seed, std::hash<hpactor::ActorType>{}(addr.type));
81 : 4 : hpactor::ActorAddress::hash_combine(
82 : 4 : seed, std::hash<hpactor::ActorId>{}(addr.id));
83 : 4 : hpactor::ActorAddress::hash_combine(
84 : 4 : seed, std::hash<uint64_t>{}(addr.incarnation));
85 : 4 : return seed;
86 : : }
87 : : };
|