载入中...
搜索中...
未找到
ProviderRegistry.h
浏览该文件的文档.
1#pragma once
2
3#include "common/Result.h"
4
5#include <algorithm>
6#include <cstdint>
7#include <limits>
8#include <memory>
9#include <mutex>
10#include <string>
11#include <string_view>
12#include <unordered_map>
13#include <variant>
14#include <vector>
15
16namespace eve::cap {
17
20 std::string id;
21 std::uint64_t generation = 0;
23 friend bool operator==(const OwnedProviderHandle&, const OwnedProviderHandle&) = default;
24};
25
37template <class I, class Metadata = std::monostate>
39public:
41 ProviderRegistry() = default;
45 struct Record {
47 std::shared_ptr<I> provider;
48 Metadata metadata;
49 };
50
53 decltype(entries_) retired;
54 {
55 std::scoped_lock lock(mutex_);
56 closing_ = true;
57 retired.swap(entries_);
58 }
59 }
60
62 [[nodiscard]] Result<OwnedProviderHandle> publish(std::string id, std::shared_ptr<I> provider,
63 Metadata metadata = {}) {
64 if (id.empty() || !provider)
66 Diagnostic::error(DiagnosticCode::InvalidArgument, "Provider requires id and implementation", "id"));
67 std::scoped_lock lock(mutex_);
68 if (closing_)
70 Diagnostic::error(DiagnosticCode::Conflict, "Provider registry is shutting down", "id"));
71 if (entries_.contains(id))
73 Diagnostic::error(DiagnosticCode::Conflict, "Provider id is already published", "id"));
74 const auto previous = generations_[id];
75 if (previous == std::numeric_limits<std::uint64_t>::max())
77 Diagnostic::error(DiagnosticCode::Conflict, "Provider generation space is exhausted", "id"));
79 entries_.emplace(id, Record{handle, std::move(provider), std::move(metadata)});
80 generations_[id] = handle.generation;
82 }
83
85 [[nodiscard]] Result<Record> acquire(const OwnedProviderHandle& handle) const {
86 std::scoped_lock lock(mutex_);
87 const auto found = entries_.find(handle.id);
88 if (found == entries_.end() || found->second.handle != handle)
91 "Provider generation is absent or stale", "handle"));
93 }
94
96 [[nodiscard]] Result<Record> acquire(std::string_view id) const {
97 std::scoped_lock lock(mutex_);
98 const auto found = entries_.find(std::string(id));
99 if (found == entries_.end())
101 Diagnostic::error(DiagnosticCode::Unsupported, "Provider is not available", "id"));
103 }
104
108 {
109 std::scoped_lock lock(mutex_);
110 const auto found = entries_.find(handle.id);
111 if (found == entries_.end() || found->second.handle != handle)
113 Diagnostic::error(DiagnosticCode::Conflict, "Provider generation is stale", "handle"));
114 removed = std::move(found->second);
115 entries_.erase(found);
116 }
118 }
119
125 [[nodiscard]] std::vector<Record> snapshot() const {
126 std::vector<Record> result;
127 {
128 std::scoped_lock lock(mutex_);
129 result.reserve(entries_.size());
130 for (const auto& [id, entry] : entries_) result.push_back(entry);
131 }
132 std::sort(result.begin(), result.end(), [](const auto& a, const auto& b) { return a.handle.id < b.handle.id; });
133 return result;
134 }
135
136private:
137 bool closing_ = false;
138 mutable std::mutex mutex_;
139 std::unordered_map<std::string, Record> entries_;
140 std::unordered_map<std::string, std::uint64_t> generations_;
141};
142
143} // namespace eve::cap
std::vector< std::unique_ptr< ScriptClimbingEventSink > > retired
std::string id
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::shared_ptr< const OceanSprayFrame > previous
PrimitiveHandle handle
Move-only, checked operation results for the common layer.
bool found
int removed
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Instance-owned publication mechanism shared by capability and domain registries. @ownership The regis...
Result< Record > acquire(std::string_view id) const
Acquire the current id.
Result< OwnedProviderHandle > publish(std::string id, std::shared_ptr< I > provider, Metadata metadata={})
Publish a unique id.
ProviderRegistry(const ProviderRegistry &)=delete
Result< Record > withdraw(const OwnedProviderHandle &handle)
Withdraw an exact generation; retained leases stay valid.
~ProviderRegistry()
Unpublish before releasing implementations; destructor reentry sees an empty, closing registry.
ProviderRegistry & operator=(const ProviderRegistry &)=delete
ProviderRegistry()=default
Construct an empty registry with its own generation namespace.
std::vector< Record > snapshot() const
Copy owning dispatch records in id order; later mutations affect the next snapshot....
Result< Record > acquire(const OwnedProviderHandle &handle) const
Acquire an exact generation.
Owning identity of one publication; generations are local to its registry.
friend bool operator==(const OwnedProviderHandle &, const OwnedProviderHandle &)=default
Compare publication identities.
Owning lease and immutable-at-publication metadata for a generation.