20 template <
class Filter>
53 using Lock = std::recursive_mutex;
54 using Locker = std::lock_guard<Lock>;
63 std::map<Filter, std::set<RE::VMHandle>>
_regs;
68 template <
class Enable,
class... Args>
71 template <
class... Args>
75 RE::BSScript::is_return_convertible<Args>...>>,
96 inline void SendEvent(Filter a_filter, Args... a_args)
101 if (
auto it = this->_regs.find(a_filter); it != this->_regs.end()) {
102 for (
auto& handle : it->second) {
104 vm->SendEvent(handle, eventName, args);
112 std::tuple args(
VMArg(std::forward<Args>(a_args))...);
116 task->AddTask([a_filter, args,
this]()
mutable {
123 template <
class Tuple, std::size_t... I>
124 inline void SendEvent_Tuple(Filter a_filter, Tuple&& a_tuple, std::index_sequence<I...>)
126 SendEvent(a_filter, std::get<I>(std::forward<Tuple>(a_tuple)).Unpack()...);
155 if (
auto it = this->_regs.find(a_filter); it != this->_regs.end()) {
156 for (
auto& handle : it->second) {
158 vm->SendEvent(handle, eventName, args);
168 task->AddTask([a_filter,
this]() {
169 SendEvent(std::move(a_filter));
175 template <
class Filter>
178 _eventName(a_eventName),
182 template <
class Filter>
185 _eventName(a_rhs._eventName),
190 a_rhs.
_lock.unlock();
193 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
194 for (
auto& reg :
_regs) {
195 for (
auto& handle : reg.second) {
196 policy->PersistHandle(handle);
202 template <
class Filter>
205 _eventName(a_rhs._eventName),
208 Locker locker(a_rhs._lock);
209 _regs = std::move(a_rhs._regs);
213 template <
class Filter>
217 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
218 for (
auto& reg : _regs) {
219 for (
auto& handle : reg.second) {
220 policy->ReleaseHandle(handle);
226 template <
class Filter>
229 if (
this == &a_rhs) {
243 if (
auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr) {
244 for (
auto& reg : _regs) {
245 for (
auto& handle : reg.second) {
246 policy->PersistHandle(handle);
254 template <
class Filter>
257 if (
this == &a_rhs) {
262 Locker rhsLocker(a_rhs._lock);
266 _eventName = a_rhs._eventName;
268 _regs = std::move(a_rhs._regs);
274 template <
class Filter>
281 template <
class Filter>
285 return Register(a_alias, std::move(a_filter), a_alias->
GetVMTypeID());
288 template <
class Filter>
291 assert(a_activeEffect);
295 template <
class Filter>
302 template <
class Filter>
306 return Unregister(a_alias, std::move(a_filter), a_alias->
GetVMTypeID());
309 template <
class Filter>
312 assert(a_activeEffect);
316 template <
class Filter>
323 template <
class Filter>
330 template <
class Filter>
333 assert(a_activeEffect);
337 template <
class Filter>
341 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
343 log::error(
"Failed to get handle policy!");
348 for (
auto& reg : _regs) {
349 if (
auto result = reg.second.erase(a_handle); result != 0) {
350 policy->ReleaseHandle(a_handle);
355 template <
class Filter>
359 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
362 for (
auto& reg : _regs) {
363 for (
auto& handle : reg.second) {
364 policy->ReleaseHandle(handle);
371 template <
class Filter>
375 if (!a_intfc->
OpenRecord(a_type, a_version)) {
376 log::error(
"Failed to open record!");
380 return Save(a_intfc);
383 template <
class Filter>
386 if constexpr (std::is_same_v<std::string, Filter>) {
387 std::size_t length = a_filter.length() + 1;
397 template <
class Filter>
404 const std::size_t numRegs = _regs.size();
406 log::error(
"Failed to save reg count ({})!", numRegs);
410 for (
auto& reg : _regs) {
412 if (!SaveFilter(a_intfc, reg.first)) {
416 std::size_t numHandles = reg.second.size();
418 log::error(
"Failed to save handle count ({})!", numHandles);
422 for (
auto& handle : reg.second) {
424 log::error(
"Failed to save handle ({})", handle);
433 template <
class Filter>
436 if constexpr (std::is_same_v<std::string, Filter>) {
437 std::size_t length = 0;
441 a_filter.reserve(length);
442 return a_intfc->
ReadRecordData(a_filter.data(),
static_cast<std::uint32_t
>(length));
443 }
else if constexpr (std::is_same_v<RE::FormID, Filter>) {
453 template <
class Filter>
465 std::size_t numHandles;
469 for (std::size_t i = 0; i < numRegs; ++i) {
470 if (!LoadFilter(a_intfc, filter)) {
474 for (std::size_t j = 0; j < numHandles; ++j) {
477 _regs[filter].insert(handle);
485 template <
class Filter>
491 template <
class Filter>
496 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
498 log::error(
"Failed to get handle policy!");
502 const auto invalidHandle = policy->EmptyHandle();
503 auto handle = policy->GetHandleForObject(a_typeID, a_object);
504 if (handle == invalidHandle) {
505 log::error(
"Failed to create handle!");
510 auto result = _regs[a_filter].insert(handle);
514 policy->PersistHandle(handle);
517 return result.second;
520 template <
class Filter>
525 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
527 log::error(
"Failed to get handle policy!");
531 const auto invalidHandle = policy->EmptyHandle();
532 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
533 if (handle == invalidHandle) {
534 log::error(
"Failed to create handle!");
539 if (
auto it = _regs.find(a_filter); it != _regs.end()) {
540 if (
auto result = it->second.erase(handle); result != 0) {
541 policy->ReleaseHandle(handle);
549 template <
class Filter>
554 auto policy = vm ? vm->GetObjectHandlePolicy() :
nullptr;
556 log::error(
"Failed to get handle policy!");
560 const auto invalidHandle = policy->EmptyHandle();
561 const auto handle = policy->GetHandleForObject(a_typeID, a_object);
562 if (handle == invalidHandle) {
563 log::error(
"Failed to create handle!");
568 for (
auto& reg : _regs) {
569 if (
auto result = reg.second.erase(handle); result != 0) {
570 policy->ReleaseHandle(handle);
576 template <
class Filter,
class... Args>
Definition ActiveEffect.h:27
static constexpr auto VMTYPEID
Definition ActiveEffect.h:31
Definition BGSBaseAlias.h:12
VMTypeID GetVMTypeID() const
static VirtualMachine * GetSingleton()
~RegistrationMap()=default
RegistrationMap & operator=(RegistrationMap &&)=default
RegistrationMap & operator=(const RegistrationMap &)=default
void SendEvent(Filter a_filter, Args... a_args)
Definition RegistrationMap.h:96
void QueueEvent(Filter a_filter, Args... a_args)
Definition RegistrationMap.h:110
RegistrationMap(RegistrationMap &&)=default
RegistrationMap(const RegistrationMap &)=default
RegistrationMap(const std::string_view &a_eventName)
Definition RegistrationMap.h:87
~RegistrationMap()=default
RegistrationMap & operator=(const RegistrationMap &)=default
void QueueEvent(Filter a_filter)
Definition RegistrationMap.h:164
RegistrationMap(const RegistrationMap &)=default
void SendEvent(Filter a_filter)
Definition RegistrationMap.h:150
RegistrationMap(RegistrationMap &&)=default
RegistrationMap(const std::string_view &a_eventName)
Definition RegistrationMap.h:141
RegistrationMap & operator=(RegistrationMap &&)=default
Definition RegistrationMap.h:25
bool Load(SerializationInterface *a_intfc)
Definition RegistrationMap.h:454
RegistrationMapBase & operator=(const RegistrationMapBase &a_rhs)
Definition RegistrationMap.h:227
RegistrationMapBase()=delete
bool LoadFilter(SerializationInterface *a_intfc, Filter &a_filter)
Definition RegistrationMap.h:434
bool Unregister(const RE::TESForm *a_form, Filter a_filter)
Definition RegistrationMap.h:296
void Revert(SerializationInterface *)
Definition RegistrationMap.h:486
~RegistrationMapBase()
Definition RegistrationMap.h:214
std::string _eventName
Definition RegistrationMap.h:64
std::lock_guard< Lock > Locker
Definition RegistrationMap.h:54
Lock _lock
Definition RegistrationMap.h:65
std::map< Filter, std::set< RE::VMHandle > > _regs
Definition RegistrationMap.h:63
bool Register(const RE::TESForm *a_form, Filter a_filter)
Definition RegistrationMap.h:275
bool SaveFilter(SerializationInterface *a_intfc, Filter a_filter)
Definition RegistrationMap.h:384
void Clear()
Definition RegistrationMap.h:356
std::recursive_mutex Lock
Definition RegistrationMap.h:53
void UnregisterAll(const RE::TESForm *a_form)
Definition RegistrationMap.h:317
bool Save(SerializationInterface *a_intfc, std::uint32_t a_type, std::uint32_t a_version)
Definition RegistrationMap.h:372
Definition RegistrationMap.h:69
Definition RegistrationMap.h:22
Definition RegistrationTraits.h:45
Definition Interfaces.h:71
bool ResolveHandle(RE::VMHandle a_oldHandle, RE::VMHandle &a_newHandle) const
std::uint32_t ReadRecordData(void *a_buf, std::uint32_t a_length) const
bool WriteRecordData(const void *a_buf, std::uint32_t a_length) const
bool ResolveFormID(RE::FormID a_oldFormID, RE::FormID &a_newFormID) const
bool OpenRecord(std::uint32_t a_type, std::uint32_t a_version) const
BSScript::IFunctionArguments * MakeFunctionArguments()
Definition FunctionArguments.h:86
std::uint32_t VMTypeID
Definition BSCoreTypes.h:9
std::uint64_t VMHandle
Definition BSCoreTypes.h:7
typename Impl::EventFilter< Filter >::template RegistrationMap< void, Args... > RegistrationMap
Definition RegistrationMap.h:577
const TaskInterface * GetTaskInterface() noexcept
Definition ActorValueList.h:28
Definition RegistrationTraits.h:40