ReUseX  0.0.1
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
Registry.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Povl Filip Sonne-Frederiksen
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#pragma once
6
7#include <boost/property_map/property_map.hpp>
8#include <spdlog/spdlog.h>
9
10#include <map>
11#include <memory>
12#include <string>
13#include <typeindex>
14#include <utility>
15
16namespace ReUseX::geometry {
17class Registry {
18 private:
19 // Keyed by name and type_index
20 std::map<std::pair<std::string, std::type_index>, std::shared_ptr<void>>
21 registry;
22
23 public:
24 virtual ~Registry() = default;
25 template <typename Key, typename T>
26 std::pair<boost::associative_property_map<std::map<Key, T>>, bool>
27 add_property_map(const std::string &name) {
28 auto key = std::make_pair(name, std::type_index(typeid(T)));
29 auto it = registry.find(key);
30 if (it != registry.end()) {
31 // Already exists: recover the map and wrap it in a property_map
32 auto map_ptr = std::static_pointer_cast<std::map<Key, T>>(it->second);
33 return {boost::associative_property_map<std::map<Key, T>>(*map_ptr),
34 false};
35 } else {
36 // Create new map and store in registry
37 auto m = std::make_shared<std::map<Key, T>>();
38 registry[key] = m;
39 return {boost::associative_property_map<std::map<Key, T>>(*m), true};
40 }
41 }
42
43 template <typename Key, typename T>
44 const boost::associative_property_map<std::map<Key, T>>
45 property_map(const std::string &name) const {
46 auto key = std::make_pair(name, std::type_index(typeid(T)));
47 auto it = registry.find(key);
48 if (it == registry.end()) {
49 spdlog::error("Property map not found: {}", name);
50 throw std::runtime_error("Property map not found");
51 }
52 auto map_ptr = std::static_pointer_cast<std::map<Key, T>>(it->second);
53 return boost::associative_property_map<std::map<Key, T>>(*map_ptr);
54 }
55};
56} // namespace ReUseX::geometry
virtual ~Registry()=default
const boost::associative_property_map< std::map< Key, T > > property_map(const std::string &name) const
Definition Registry.hpp:45
std::pair< boost::associative_property_map< std::map< Key, T > >, bool > add_property_map(const std::string &name)
Definition Registry.hpp:27