Loading...
Searching...
No Matches
PotRegistry.h
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#pragma once
13
14#include "BaseStructures.h"
15#include <array>
16#include <atomic>
17#include <chrono>
18#include <memory>
19#include <mutex>
20#include <string>
21#include <vector>
22
23namespace eonc {
24
28 std::atomic<bool> alive{true};
29};
30
31// ---------------------------------------------------------------------------
32// IPotRegistry: injectable ABI. Production uses PotRegistry::get().
33// ---------------------------------------------------------------------------
35public:
36 using Clock = std::chrono::system_clock;
37 using TimePoint = Clock::time_point;
38
39 virtual ~IPotRegistry() {
40 if (lifetime_)
41 lifetime_->alive.store(false, std::memory_order_release);
42 }
43
44 [[nodiscard]] std::shared_ptr<RegistryLifetime> lifetime() const {
45 return lifetime_;
46 }
47 [[nodiscard]] virtual uint64_t on_created(PotType t) noexcept = 0;
48 virtual void on_destroyed(uint64_t id, PotType t, size_t force_calls,
49 TimePoint created_at) = 0;
50 virtual void on_force_call(PotType t) noexcept = 0;
51
52 IPotRegistry(const IPotRegistry &) = delete;
54
55protected:
56 IPotRegistry() = default;
57
58 std::shared_ptr<RegistryLifetime> lifetime_{
59 std::make_shared<RegistryLifetime>()};
60};
61
62class PotRegistry : public IPotRegistry {
63public:
66
74
75private:
76 struct TypeStats {
77 std::atomic<size_t> force_calls{0};
78 std::atomic<size_t> created{0};
79 std::atomic<size_t> alive{0};
80 };
81 std::array<TypeStats, magic_enum::enum_count<PotType>()> m_type_stats{};
82
83 std::mutex m_records_mutex;
84 std::vector<InstanceRecord> m_records;
85
86 std::atomic<uint64_t> m_next_id{1};
87
88public:
89 PotRegistry() = default;
90 ~PotRegistry() override {
91 // Members are still intact. Mark the registry dead before they go,
92 // so a Potential destroyed from here does not lock a dead mutex.
93 if (lifetime_)
94 lifetime_->alive.store(false, std::memory_order_release);
95 }
96
100 static PotRegistry &get() noexcept;
101 void reset();
102
103 // Lifecycle events
104 [[nodiscard]] uint64_t on_created(PotType t) noexcept override;
105 void on_destroyed(uint64_t id, PotType t, size_t force_calls,
106 TimePoint created_at) override;
107 void on_force_call(PotType t) noexcept override;
108
109 // Per-type queries
110 [[nodiscard]] size_t type_force_calls(PotType t) const noexcept;
111 [[nodiscard]] size_t total_force_calls() const noexcept;
112 [[nodiscard]] size_t type_alive(PotType t) const noexcept;
113
114 // JSON output
115 void write_summary(const std::string &path = "_potcalls.json") const;
116};
117
118} // namespace eonc
IPotRegistry(const IPotRegistry &)=delete
std::shared_ptr< RegistryLifetime > lifetime() const
Definition PotRegistry.h:44
IPotRegistry & operator=(const IPotRegistry &)=delete
virtual void on_destroyed(uint64_t id, PotType t, size_t force_calls, TimePoint created_at)=0
std::shared_ptr< RegistryLifetime > lifetime_
Definition PotRegistry.h:58
IPotRegistry()=default
Clock::time_point TimePoint
Definition PotRegistry.h:37
virtual void on_force_call(PotType t) noexcept=0
std::chrono::system_clock Clock
Definition PotRegistry.h:36
virtual ~IPotRegistry()
Definition PotRegistry.h:39
virtual uint64_t on_created(PotType t) noexcept=0
std::atomic< uint64_t > m_next_id
Definition PotRegistry.h:86
std::array< TypeStats, magic_enum::enum_count< PotType >()> m_type_stats
Definition PotRegistry.h:81
PotRegistry()=default
~PotRegistry() override
Definition PotRegistry.h:90
void on_force_call(PotType t) noexcept override
static PotRegistry & get() noexcept
Process-lifetime singleton.
size_t type_force_calls(PotType t) const noexcept
size_t total_force_calls() const noexcept
std::mutex m_records_mutex
Definition PotRegistry.h:83
size_t type_alive(PotType t) const noexcept
uint64_t on_created(PotType t) noexcept override
void write_summary(const std::string &path="_potcalls.json") const
std::vector< InstanceRecord > m_records
Definition PotRegistry.h:84
IPotRegistry::Clock Clock
Definition PotRegistry.h:64
void on_destroyed(uint64_t id, PotType t, size_t force_calls, TimePoint created_at) override
IPotRegistry::TimePoint TimePoint
Definition PotRegistry.h:65
RAII resource manager for the ARTn C library with global synchronization.
std::atomic< size_t > created
Definition PotRegistry.h:78
std::atomic< size_t > force_calls
Definition PotRegistry.h:77
std::atomic< size_t > alive
Definition PotRegistry.h:79
Outlives the registry object.
Definition PotRegistry.h:27
std::atomic< bool > alive
Definition PotRegistry.h:28