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 <mutex>
19#include <string>
20#include <vector>
21
22namespace eonc {
23
24// ---------------------------------------------------------------------------
25// IPotRegistry: injectable ABI. Production uses PotRegistry::get().
26// ---------------------------------------------------------------------------
28public:
29 using Clock = std::chrono::system_clock;
30 using TimePoint = Clock::time_point;
31
32 virtual ~IPotRegistry() = default;
33 [[nodiscard]] virtual uint64_t on_created(PotType t) noexcept = 0;
34 virtual void on_destroyed(uint64_t id, PotType t, size_t force_calls,
35 TimePoint created_at) = 0;
36 virtual void on_force_call(PotType t) noexcept = 0;
37
38 IPotRegistry(const IPotRegistry &) = delete;
40
41protected:
42 IPotRegistry() = default;
43};
44
45class PotRegistry : public IPotRegistry {
46public:
49
57
58private:
59 struct TypeStats {
60 std::atomic<size_t> force_calls{0};
61 std::atomic<size_t> created{0};
62 std::atomic<size_t> alive{0};
63 };
64 std::array<TypeStats, magic_enum::enum_count<PotType>()> m_type_stats{};
65
66 std::mutex m_records_mutex;
67 std::vector<InstanceRecord> m_records;
68
69 std::atomic<uint64_t> m_next_id{1};
70
71public:
72 PotRegistry() = default;
73
77 static PotRegistry &get() noexcept;
78 void reset();
79
80 // Lifecycle events
81 [[nodiscard]] uint64_t on_created(PotType t) noexcept override;
82 void on_destroyed(uint64_t id, PotType t, size_t force_calls,
83 TimePoint created_at) override;
84 void on_force_call(PotType t) noexcept override;
85
86 // Per-type queries
87 [[nodiscard]] size_t type_force_calls(PotType t) const noexcept;
88 [[nodiscard]] size_t total_force_calls() const noexcept;
89 [[nodiscard]] size_t type_alive(PotType t) const noexcept;
90
91 // JSON output
92 void write_summary(const std::string &path = "_potcalls.json") const;
93};
94
95} // namespace eonc
IPotRegistry(const IPotRegistry &)=delete
virtual ~IPotRegistry()=default
IPotRegistry & operator=(const IPotRegistry &)=delete
virtual void on_destroyed(uint64_t id, PotType t, size_t force_calls, TimePoint created_at)=0
IPotRegistry()=default
Clock::time_point TimePoint
Definition PotRegistry.h:30
virtual void on_force_call(PotType t) noexcept=0
std::chrono::system_clock Clock
Definition PotRegistry.h:29
virtual uint64_t on_created(PotType t) noexcept=0
std::atomic< uint64_t > m_next_id
Definition PotRegistry.h:69
std::array< TypeStats, magic_enum::enum_count< PotType >()> m_type_stats
Definition PotRegistry.h:64
PotRegistry()=default
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:66
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:67
IPotRegistry::Clock Clock
Definition PotRegistry.h:47
void on_destroyed(uint64_t id, PotType t, size_t force_calls, TimePoint created_at) override
IPotRegistry::TimePoint TimePoint
Definition PotRegistry.h:48
RAII resource manager for the ARTn C library with global synchronization.
std::atomic< size_t > created
Definition PotRegistry.h:61
std::atomic< size_t > force_calls
Definition PotRegistry.h:60
std::atomic< size_t > alive
Definition PotRegistry.h:62