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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
#pragma once
#include <array>
#include <cstdint>
#include <functional>
#include <string>
#include "caf/detail/comparable.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/fwd.hpp"
#include "caf/hash/fnv.hpp"
#include "caf/inspector_access.hpp"
#include "caf/intrusive_ptr.hpp"
#include "caf/none.hpp"
#include "caf/ref_counted.hpp"
#include "caf/uri.hpp"
#include "caf/variant.hpp"
namespace caf {
class CAF_CORE_EXPORT hashed_node_id {
public:
// -- member types -----------------------------------------------------------
/// Represents a 160 bit hash.
using host_id_type = std::array<uint8_t, 20>;
// -- constructors, destructors, and assignment operators --------------------
hashed_node_id() noexcept;
hashed_node_id(uint32_t process_id, const host_id_type& host) noexcept;
// -- properties -------------------------------------------------------------
bool valid() const noexcept;
// -- comparison -------------------------------------------------------------
int compare(const hashed_node_id& other) const noexcept;
// -- conversion -------------------------------------------------------------
void print(std::string& dst) const;
// -- static utility functions -----------------------------------------------
static bool valid(const host_id_type& x) noexcept;
static bool can_parse(string_view str) noexcept;
static node_id local(const actor_system_config&);
// -- member variables -------------------------------------------------------
uint32_t process_id;
host_id_type host;
// -- friend functions -------------------------------------------------------
template <class Inspector>
friend bool inspect(Inspector& f, hashed_node_id& x) {
return f.object(x).fields(f.field("process_id", x.process_id),
f.field("host", x.host));
}
};
class CAF_CORE_EXPORT node_id_data : public ref_counted {
public:
// -- member types -----------------------------------------------------------
using variant_type = variant<uri, hashed_node_id>;
// -- constructors, destructors, and assignment operators --------------------
explicit node_id_data(variant_type value) : content(std::move(value)) {
// nop
}
explicit node_id_data(uri value) : content(std::move(value)) {
// nop
}
explicit node_id_data(const hashed_node_id& value)
: content(std::move(value)) {
// nop
}
node_id_data() = default;
node_id_data(node_id_data&&) = default;
node_id_data(const node_id_data&) = default;
node_id_data& operator=(node_id_data&&) = default;
node_id_data& operator=(const node_id_data&) = default;
virtual ~node_id_data();
// -- member variables -------------------------------------------------------
variant_type content;
};
/// A node ID is an opaque value for representing CAF instances in the network.
class CAF_CORE_EXPORT node_id {
public:
// -- member types -----------------------------------------------------------
using default_data = hashed_node_id;
// -- constructors, destructors, and assignment operators --------------------
constexpr node_id() noexcept {
// nop
}
explicit node_id(hashed_node_id data) {
if (data.valid())
data_.emplace(data);
}
explicit node_id(uri data) {
if (data.valid())
data_.emplace(std::move(data));
}
node_id& operator=(const none_t&);
// -- properties -------------------------------------------------------------
/// Queries whether this node is not default-constructed.
explicit operator bool() const noexcept {
return static_cast<bool>(data_);
}
/// Queries whether this node is default-constructed.
bool operator!() const noexcept {
return !data_;
}
/// Compares this instance to `other`.
/// @returns -1 if `*this < other`, 0 if `*this == other`, and 1 otherwise.
int compare(const node_id& other) const noexcept;
/// Exchanges the value of this object with `other`.
void swap(node_id& other) noexcept;
/// Returns whether `parse` would produce a valid node ID.
static bool can_parse(string_view str) noexcept;
// -- friend functions -------------------------------------------------------
template <class Inspector>
friend bool inspect(Inspector& f, node_id& x) {
auto is_present = [&x] { return x.data_ != nullptr; };
auto get = [&]() -> const auto& {
return x.data_->content;
};
auto reset = [&x] { x.data_.reset(); };
auto set = [&x](node_id_data::variant_type&& val) {
if (x.data_ && x.data_->unique())
x.data_->content = std::move(val);
else
x.data_.emplace(std::move(val));
return true;
};
return f.object(x).fields(f.field("data", is_present, get, reset, set));
}
// -- private API ------------------------------------------------------------
/// @cond PRIVATE
auto* operator->() noexcept {
return data_.get();
}
const auto* operator->() const noexcept {
return data_.get();
}
auto& operator*() noexcept {
return *data_;
}
const auto& operator*() const noexcept {
return *data_;
}
/// @endcond
private:
intrusive_ptr<node_id_data> data_;
};
/// Returns whether `x` contains an URI.
/// @relates node_id
inline bool wraps_uri(const node_id& x) noexcept {
return x && holds_alternative<uri>(x->content);
}
/// @relates node_id
inline bool operator==(const node_id& x, const node_id& y) noexcept {
return x.compare(y) == 0;
}
/// @relates node_id
inline bool operator!=(const node_id& x, const node_id& y) noexcept {
return x.compare(y) != 0;
}
/// @relates node_id
inline bool operator<(const node_id& x, const node_id& y) noexcept {
return x.compare(y) < 0;
}
/// @relates node_id
inline bool operator<=(const node_id& x, const node_id& y) noexcept {
return x.compare(y) <= 0;
}
/// @relates node_id
inline bool operator>(const node_id& x, const node_id& y) noexcept {
return x.compare(y) > 0;
}
/// @relates node_id
inline bool operator>=(const node_id& x, const node_id& y) noexcept {
return x.compare(y) >= 0;
}
/// @relates node_id
inline bool operator==(const node_id& x, const none_t&) noexcept {
return !x;
}
/// @relates node_id
inline bool operator==(const none_t&, const node_id& x) noexcept {
return !x;
}
/// @relates node_id
inline bool operator!=(const node_id& x, const none_t&) noexcept {
return static_cast<bool>(x);
}
/// @relates node_id
inline bool operator!=(const none_t&, const node_id& x) noexcept {
return static_cast<bool>(x);
}
/// Appends `x` in human-readable string representation to `str`.
/// @relates node_id
CAF_CORE_EXPORT void append_to_string(std::string& str, const node_id& x);
/// Converts `x` into a human-readable string representation.
/// @relates node_id
CAF_CORE_EXPORT std::string to_string(const node_id& x);
/// Creates a node ID from the URI `from`.
/// @relates node_id
CAF_CORE_EXPORT node_id make_node_id(uri from);
/// Creates a node ID from `process_id` and `host_id`.
/// @param process_id System-wide unique process identifier.
/// @param host_id Unique hash value representing a single CAF node.
/// @relates node_id
CAF_CORE_EXPORT node_id make_node_id(
uint32_t process_id, const node_id::default_data::host_id_type& host_id);
/// Creates a node ID from `process_id` and `host_hash`.
/// @param process_id System-wide unique process identifier.
/// @param host_hash Unique node ID as hexadecimal string representation.
/// @relates node_id
CAF_CORE_EXPORT optional<node_id> make_node_id(uint32_t process_id,
string_view host_hash);
/// @relates node_id
CAF_CORE_EXPORT error parse(string_view str, node_id& dest);
} // namespace caf
namespace std {
template <>
struct hash<caf::node_id> {
size_t operator()(const caf::node_id& x) const noexcept {
return caf::hash::fnv<size_t>::compute(x);
}
};
} // namespace std
|