载入中...
搜索中...
未找到
CapabilityOwned.h
浏览该文件的文档.
1#pragma once
2
13#include "common/Capability.h"
14#include "common/Diagnostic.h"
15#include "common/Export.h"
17#include "common/Result.h"
18#include "common/Status.h"
19
20#include <cstdint>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <string_view>
25#include <unordered_map>
26#include <utility>
27
28namespace eve::cap {
29
37template <class I>
39public:
40 OwnedProviderLease() = default;
41
43 OwnedProviderLease(const OwnedProviderLease& other) = default;
48 : handle_(std::exchange(other.handle_, OwnedProviderHandle{})), provider_(std::move(other.provider_)) {}
51 if (this != &other) {
52 handle_ = std::exchange(other.handle_, OwnedProviderHandle{});
53 provider_ = std::move(other.provider_);
54 }
55 return *this;
56 }
57
58 [[nodiscard]] const OwnedProviderHandle& handle() const noexcept { return handle_; }
59 [[nodiscard]] explicit operator bool() const noexcept { return static_cast<bool>(provider_); }
61 [[nodiscard]] I* get() const noexcept { return provider_.get(); }
62 [[nodiscard]] I& operator*() const noexcept { return *provider_; }
63 [[nodiscard]] I* operator->() const noexcept { return provider_.get(); }
64
65private:
66 template <class>
68
70 : handle_(std::move(handle)), provider_(std::move(provider)) {}
71
72 OwnedProviderHandle handle_;
73 std::shared_ptr<I> provider_;
74};
75
76namespace detail {
77
80EVENGINE_API_FOUNDATION ProviderRegistry<void>& ownedRegistry(const char* capabilityName);
83
84} // namespace detail
85
91template <class I>
93public:
100 [[nodiscard]] static Result<OwnedProviderHandle> provide(std::string id, std::shared_ptr<I> provider) {
101 std::unique_lock lock(detail::ownedPublicationMutex());
102 auto result = detail::ownedRegistry(I::capabilityName).publish(std::move(id), provider);
103 if (!result.ok()) return result;
104 detail::provideRaw(I::capabilityName, provider.get());
105 lock.unlock();
106 return result;
107 }
108
114 auto record = detail::ownedRegistry(I::capabilityName).acquire(handle);
115 if (!record.ok()) return Result<OwnedProviderLease<I>>::failure(record.status());
116 return Result<OwnedProviderLease<I>>::success(
117 OwnedProviderLease<I>(record.value().handle, std::static_pointer_cast<I>(record.value().provider)),
119 }
120
124 [[nodiscard]] static Result<OwnedProviderLease<I>> acquire(std::string_view id) {
125 auto record = detail::ownedRegistry(I::capabilityName).acquire(id);
126 if (!record.ok()) return Result<OwnedProviderLease<I>>::failure(record.status());
127 return Result<OwnedProviderLease<I>>::success(
128 OwnedProviderLease<I>(record.value().handle, std::static_pointer_cast<I>(record.value().provider)),
130 }
131
135 [[nodiscard]] static Result<void> unload(const OwnedProviderHandle& handle) {
136 std::unique_lock lock(detail::ownedPublicationMutex());
137 auto removed = detail::ownedRegistry(I::capabilityName).withdraw(handle);
138 if (!removed.ok()) return Result<void>::failure(removed.status());
139 detail::revokeRaw(I::capabilityName, removed.value().provider.get());
140 lock.unlock();
142 }
143};
144
145} // namespace eve::cap
Stable, structured diagnostics shared by engine modules.
ScriptComponentRecord record
Definition ECS.cpp:228
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
Poco::JSON::Object::Ptr result
PrimitiveHandle handle
int detail
Move-only, checked operation results for the common layer.
int removed
Structured operation status used by the common Result foundation.
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
Owning lease that keeps a shared_ptr provider alive while borrowed.
I * get() const noexcept
Borrowed interface pointer valid while this lease remains alive.
OwnedProviderLease & operator=(const OwnedProviderLease &other)=default
Share other, releasing this lease's previous ownership.
I * operator->() const noexcept
OwnedProviderLease(OwnedProviderLease &&other) noexcept
Transfer ownership and identity, leaving other empty with generation zero.
const OwnedProviderHandle & handle() const noexcept
I & operator*() const noexcept
OwnedProviderLease & operator=(OwnedProviderLease &&other) noexcept
Transfer other and empty it; self-move preserves this lease.
OwnedProviderLease(const OwnedProviderLease &other)=default
Share the provider and its generation identity with other.
Thread-safe owned-provider table keyed by capability name + provider id.
static Result< void > unload(const OwnedProviderHandle &handle)
Unpublish one exact provider generation and clear the borrowed slot when it matches.
static Result< OwnedProviderLease< I > > acquire(std::string_view id)
Acquire the current generation published under id.
static Result< OwnedProviderLease< I > > acquire(const OwnedProviderHandle &handle)
Acquire an owning lease for an exact handle generation.
static Result< OwnedProviderHandle > provide(std::string id, std::shared_ptr< I > provider)
Publish an owning provider under id, replacing only after explicit unload.
ProviderRegistry< void > & ownedRegistry(const char *capabilityName)
void clearOwnedRaw()
Clear publications for isolated tests after borrowed dispatch has stopped.
void provideRaw(const char *name, void *impl)
Provides raw.
std::mutex & ownedPublicationMutex()
Process-owned type-erased store; one canonical instance across link groups.
void revokeRaw(const char *name, void *impl)
Revoke raw.
Owning identity of one publication; generations are local to its registry.