Skip to main content

TypeIndexer.ixx File

Utility for generating unique type indices at runtime. More...

Included Headers

#include <atomic>

Namespaces Index

namespacehelios
namespacecore

Core utilities shared across the helios engine. More...

namespacedata

Core data structures for type indexing. More...

Classes Index

classTypeIndexer<Group>

Utility class to generate unique type indices at runtime. More...

Description

Utility for generating unique type indices at runtime.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file TypeIndexer.ixx
3 * @brief Utility for generating unique type indices at runtime.
4 */
5module;
6
7#include <atomic>
8
9export module helios.core.data.TypeIndexer;
10
11export namespace helios::core::data {
12
13 /**
14 * @class TypeIndexer
15 * @brief Utility class to generate unique type indices at runtime.
16 *
17 * @details This class provides a mechanism to generate and retrieve unique indices
18 * for different types. It ensures that each type gets a distinct index
19 * which remains constant throughout the program's execution.
20 *
21 * The indices are generated in a thread-safe manner using atomic operations.
22 *
23 * @tparam Group A tag type used to create separate index spaces. Different Group
24 * types maintain independent counters, allowing the same type T to have different
25 * indices in different contexts.
26 *
27 * Example usage:
28 * ```cpp
29 * class EventBus {
30 *
31 * struct EventTypeGroup {};
32 *
33 * using EventTypeIndexer = helios::core::TypeIndexer<EventTypeGroup>;
34 *
35 * public:
36 *
37 * template<typename T>
38 * EventBuffer<T>& getOrAddBuffer() {
39 * const size_t index = EventTypeIndexer::typeIndex<T>();
40 * auto& buffer = lookupBufferAt(index);
41 * // ...
42 * }
43 * };
44 * ```
45 */
46 template<typename Group>
47 class TypeIndexer {
48
49 /**
50 * @brief A static atomic counter used for generating unique type indices.
51 */
52 static inline std::atomic<size_t> counter_{0};
53
54 public:
55
56 /**
57 * @brief Generates and returns a unique type index for a specific type.
58 *
59 * @details This method assigns a unique index to each type within
60 * the scope of the associated TypeIndexer.
61 *
62 * @tparam T The type for which to retrieve the unique index.
63 *
64 * @return A unique index representing the specific type T within this Group.
65 */
66 template<typename T>
67 static size_t typeIndex() {
68 static const size_t typeIndex = counter_.fetch_add(1, std::memory_order::relaxed);
69 return typeIndex;
70 }
71
72 };
73}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.