MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_element_registry.hpp
Go to the documentation of this file.
1/**
2 * @file meshx_element_registry.hpp
3 * @brief Registry for tracking MeshX element instances.
4 *
5 * This file provides a global registry to map element indices to element instances,
6 * eliminating the need for per-variant static instance arrays.
7 *
8 * @author Pranjal Chanda
9 * @date 2024-2025
10 * @copyright Copyright 2024 - 2025 MeshX
11 */
12
13#ifndef __MESHX_ELEMENT_REGISTRY_HPP__
14#define __MESHX_ELEMENT_REGISTRY_HPP__
15
16#include <meshx_fwd_decl.hpp>
17#include <map>
18#include <mutex>
19
20/**
21 * @class meshXElementRegistry
22 * @brief Singleton registry for MeshX elements.
23 */
25{
26private:
27 std::map<uint16_t, meshXElementIF*> element_map;
28 std::mutex registry_mutex;
29
31
32public:
34 {
35 static meshXElementRegistry instance;
36 return instance;
37 }
38
39 /**
40 * @brief Register an element instance.
41 * @param element Pointer to the element interface.
42 */
44 {
45 if (!element) return;
46 std::lock_guard<std::mutex> lock(registry_mutex);
47 element_map[element->get_element_idx()] = element;
48 }
49
50 /**
51 * @brief Unregister an element instance.
52 * @param element_idx Index of the element to unregister.
53 */
54 void unregister_element(uint16_t element_idx)
55 {
56 std::lock_guard<std::mutex> lock(registry_mutex);
57 element_map.erase(element_idx);
58 }
59
60 /**
61 * @brief Find an element instance by its index.
62 * @param element_idx Index of the element.
63 * @return meshXElementIF* Pointer to the element instance, or nullptr if not found.
64 */
65 meshXElementIF* find_element(uint16_t element_idx)
66 {
67 std::lock_guard<std::mutex> lock(registry_mutex);
68 auto it = element_map.find(element_idx);
69 if (it != element_map.end())
70 {
71 return it->second;
72 }
73 return nullptr;
74 }
75
76 /**
77 * @brief Find and cast an element instance.
78 * @tparam T The target element type.
79 * @param element_idx Index of the element.
80 * @param expected_variant The expected variant type discriminator.
81 * @return T* Pointer to the casted element instance, or nullptr if not found or type mismatch.
82 */
83 template <typename T>
84 T* find_and_cast(uint16_t element_idx, meshx_element_type_t expected_variant)
85 {
86 meshXElementIF* base = find_element(element_idx);
87 if (base && base->get_element_variant() == expected_variant)
88 {
89 return static_cast<T*>(base);
90 }
91 return nullptr;
92 }
93
94 /**
95 * @brief Get all registered elements.
96 * @return std::map<uint16_t, meshXElementIF*> A copy of the element map.
97 */
98 std::map<uint16_t, meshXElementIF*> get_all_elements()
99 {
100 std::lock_guard<std::mutex> lock(registry_mutex);
101 return element_map;
102 }
103
104 /* Prevent copying */
107};
108
109#endif /* __MESHX_ELEMENT_REGISTRY_HPP__ */
Interface class for MeshX elements.
Definition meshx_fwd_decl.hpp:82
uint16_t get_element_idx(void) const
Definition meshx_fwd_decl.hpp:99
virtual meshx_element_type_t get_element_variant(void) const =0
Get the element variant.
T * find_and_cast(uint16_t element_idx, meshx_element_type_t expected_variant)
Find and cast an element instance.
Definition meshx_element_registry.hpp:84
void unregister_element(uint16_t element_idx)
Unregister an element instance.
Definition meshx_element_registry.hpp:54
void register_element(meshXElementIF *element)
Register an element instance.
Definition meshx_element_registry.hpp:43
std::mutex registry_mutex
Definition meshx_element_registry.hpp:28
std::map< uint16_t, meshXElementIF * > get_all_elements()
Get all registered elements.
Definition meshx_element_registry.hpp:98
meshXElementIF * find_element(uint16_t element_idx)
Find an element instance by its index.
Definition meshx_element_registry.hpp:65
static meshXElementRegistry & get_instance()
Definition meshx_element_registry.hpp:33
meshXElementRegistry()=default
std::map< uint16_t, meshXElementIF * > element_map
Definition meshx_element_registry.hpp:27
meshXElementRegistry & operator=(const meshXElementRegistry &)=delete
meshXElementRegistry(const meshXElementRegistry &)=delete
Forward declaration of MeshX classes.