cgv
Loading...
Searching...
No Matches
object_registry.h
1#pragma once
2
3#include <algorithm>
4#include <map>
5#include <string>
6#include <vector>
7
8namespace cgv {
9namespace data {
10
16template<typename T>
18public:
19 using entry_type = std::pair<std::string, T>;
20 using const_iterator = typename std::vector<entry_type>::const_iterator;
21 using const_reverse_iterator = typename std::vector<entry_type>::const_reverse_iterator;
22
23 size_t size() const {
24 return entries_.size();
25 }
26
27 bool empty() const {
28 return entries_.empty();
29 }
30
31 const T& get(size_t index) const {
32 return entries_[index].second;
33 }
34
35 std::string get_name(size_t index) const {
36 return entries_[index].first;
37 }
38
39 std::vector<std::string> get_names() const {
40 std::vector<std::string> names;
41 names.reserve(entries_.size());
42 std::transform(entries_.begin(), entries_.end(), std::back_inserter(names), [](const entry_type& entry) { return entry.first; });
43 return names;
44 }
45
46 bool add(const std::string& name, const T& object) {
47 if(index_by_name_.find(name) != index_by_name_.end())
48 return false;
49
50 index_by_name_[name] = entries_.size();
51 entries_.push_back({ name, object });
52 return true;
53 }
54
55 void clear() {
56 entries_.clear();
57 index_by_name_.clear();
58 }
59
60 const_iterator begin() const noexcept {
61 return entries_.begin();
62 }
63
64 const_iterator end() const noexcept {
65 return entries_.end();
66 }
67
68 const_reverse_iterator rbegin() const noexcept {
69 return entries_.rbegin();
70 }
71
72 const_reverse_iterator rend() const noexcept {
73 return entries_.rend();
74 }
75
76 const_iterator cbegin() const noexcept {
77 return begin();
78 }
79
80 const_iterator cend() const noexcept {
81 return end();
82 }
83
84 const_reverse_iterator crbegin() const noexcept {
85 return rbegin();
86 }
87
88 const_reverse_iterator crend() const noexcept {
89 return rend();
90 }
91
92 const_iterator find(const std::string& name) const {
93 auto it = index_by_name_.find(name);
94 if(it != index_by_name_.end())
95 return entries_.begin() + it->second;
96 return entries_.end();
97 }
98
99 template<typename UnaryPredicate>
100 const_iterator find_first(UnaryPredicate predicate) const {
101 for(auto it = entries_.begin(); it != entries_.end(); ++it) {
102 if(predicate(it->first, it->second))
103 return it;
104 }
105 return entries_.end();
106 }
107
108 template<typename UnaryPredicate>
109 std::vector<const_iterator> find_all(UnaryPredicate predicate) const {
110 std::vector<const_iterator> result;
111 for(auto it = entries_.begin(); it != entries_.end(); ++it) {
112 if(predicate(it->first, it->second))
113 result.push_back(it);
114 }
115 return result;
116 }
117
118private:
119 std::vector<entry_type> entries_;
120 std::map<std::string, size_t> index_by_name_;
121};
122
123} // namespace data
124} // namespace cgv
An object registry allows registering and querying objects identified by a unique name.
this header is dependency free
Definition print.h:11