opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
component_registry.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <concepts>
7#include <cstddef>
8#include <stdexcept>
9#include <string_view>
10#include <type_traits>
11#include <utility>
12
13namespace opyn
14{
15 // Represents a sequence of named/described `OpenSim::Component`s of type `T`.
16 template<typename T>
18 public:
22 using const_iterator = const value_type*;
23
25 std::string_view name_,
26 std::string_view description_) :
27
28 ComponentRegistryBase{name_, description_}
29 {}
30
32 {
33 const auto& base = static_cast<const ComponentRegistryBase&>(*this);
34 return static_cast<const_iterator>(base.begin());
35 }
36
38 {
39 const auto& base = static_cast<const ComponentRegistryBase&>(*this);
40 return static_cast<const_iterator>(base.end());
41 }
42
43 const_reference operator[](size_t pos) const
44 {
45 const auto& base = static_cast<const ComponentRegistryBase&>(*this);
46 return static_cast<const_reference>(base[pos]);
47 }
48
49 template<typename... Args>
50 requires std::constructible_from<value_type, Args&&...>
52 {
53 auto& erased = emplace_back_erased(std::forward<Args>(args)...);
54 return static_cast<reference>(erased);
55 }
56 };
57
58 template<typename T>
59 const ComponentRegistryEntry<T>& At(const ComponentRegistry<T>& registry, size_t i)
60 {
61 if (i >= registry.size()) {
62 throw std::out_of_range{"attempted to access an out-of-bounds registry entry"};
63 }
64 return registry[i];
65 }
66
67 template<typename T>
68 const ComponentRegistryEntry<T>& Get(ComponentRegistry<T> const& registry, const T& el)
69 {
70 if (auto i = IndexOf(registry, el)) {
71 return registry[*i];
72 }
73 else {
74 throw std::out_of_range{"attempted to get an element from the registry that does not exist"};
75 }
76 }
77
78 template<typename T>
79 const ComponentRegistryEntry<T>& Get(const ComponentRegistry<T>& registry, std::string_view componentClassName)
80 {
81 if (auto i = IndexOf(registry, componentClassName)) {
82 return registry[*i];
83 }
84 else {
85 throw std::out_of_range{"attempted to get an element from a component registry that does not exist"};
86 }
87 }
88}
Definition component_registry_base.h:20
reference emplace_back_erased(Args &&... args)
Definition component_registry_base.h:47
size_type size() const
Definition component_registry_base.h:33
Definition component_registry_entry.h:12
Definition component_registry.h:17
const_iterator begin() const
Definition component_registry.h:31
const_reference operator[](size_t pos) const
Definition component_registry.h:43
ComponentRegistryEntry< T > value_type
Definition component_registry.h:19
ComponentRegistry(std::string_view name_, std::string_view description_)
Definition component_registry.h:24
const_iterator end() const
Definition component_registry.h:37
const_reference emplace_back(Args &&... args)
Definition component_registry.h:51
Definition component_registry.h:14
const ComponentRegistryEntry< T > & At(const ComponentRegistry< T > &registry, size_t i)
Definition component_registry.h:59
std::optional< size_t > IndexOf(const ComponentRegistryBase &, std::string_view componentClassName)
const ComponentRegistryEntry< T > & Get(ComponentRegistry< T > const &registry, const T &el)
Definition component_registry.h:68