ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ObjectManager.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __ONERT_UTIL_OBJECT_MANAGER_H__
18#define __ONERT_UTIL_OBJECT_MANAGER_H__
19
20#include "util/logging.h"
21
22#include <cassert>
23#include <functional>
24#include <list>
25#include <memory>
26#include <unordered_map>
27
28namespace onert::util
29{
30
35template <typename Index, typename Object> class ObjectManager
36{
37public:
39
40public:
47 template <class... Args> Index emplace(Args &&...args)
48 {
49 auto index = generateIndex();
50 if (!index.valid())
51 return index;
52 _objects.emplace(index, std::make_unique<Object>(std::forward<Args>(args)...));
53 return index;
54 }
55
65 Index push(std::unique_ptr<Object> &&object, Index index)
66 {
67 auto gen_index = tryIndex(index);
68 if (gen_index.valid())
69 _objects.emplace(gen_index, std::move(object));
70 return gen_index;
71 }
80 Index push(std::unique_ptr<Object> &&object)
81 {
82 auto gen_index = generateIndex();
83 if (gen_index.valid())
84 _objects.emplace(gen_index, std::move(object));
85 return gen_index;
86 }
97 Index set(Index index, std::unique_ptr<Object> &&object)
98 {
99 if (index.valid())
100 _objects[index] = std::move(object);
101 return index;
102 }
109 void remove(const Index &index) { _objects.erase(index); }
110
119 const Object &at(const Index &index) const { return *(_objects.at(index)); }
128 Object &at(const Index &index) { return *(_objects.at(index)); }
137 const Object *getRawPtr(const Index &index) const
138 {
139 auto itr = _objects.find(index);
140 if (itr == _objects.end())
141 return nullptr;
142 else
143 {
144 assert(itr->second != nullptr);
145 return itr->second.get();
146 }
147 }
156 Object *getRawPtr(const Index &index)
157 {
158 return const_cast<Object *>(
159 const_cast<const ObjectManager<Index, Object> *>(this)->getRawPtr(index));
160 }
167 bool exist(const Index &index) const
168 {
169 auto it = _objects.find(index);
170 return it != _objects.end();
171 }
177 size_t size() const { return _objects.size(); }
184 void iterate(const std::function<void(const Index &, const Object &)> &fn) const
185 {
186 for (const auto &[index, obj] : _objects)
187 {
188 fn(index, *obj);
189 }
190 }
197 void iterate(const std::function<void(const Index &, Object &)> &fn)
198 {
199 // TODO Remove this workaround
200 // This implementation is a workaround in case of adding operands while iteration
201 std::list<Index> l;
202
203 for (const auto &e : _objects)
204 {
205 l.push_back(e.first);
206 }
207
208 for (const auto &index : l)
209 {
210 fn(index, *_objects[index]);
211 }
212 }
213
214private:
215 // Try assigning the given index
216 Index tryIndex(Index index)
217 {
218 if (!index.valid())
219 return index;
220 if (_objects.find(index) == _objects.end())
221 {
222 // If the given index does not exist, update the next index and return the index
223 if (index.value() >= _next_index)
224 _next_index = index.value() + 1;
225 return index;
226 }
227 else
228 {
229 // If the given index exists already, return a non-valid index
230 return Index{};
231 }
232 }
233
234 // Generate a new index with `_next_index`
235 Index generateIndex()
236 {
237 // No need to check if there is an entry with _next_index since
238 // _next_index is always ("the highest index in the object map" + 1)
239 if (Index{_next_index}.valid())
240 return Index{_next_index++};
241 else
242 return Index{};
243 }
244
245protected:
246 std::unordered_map<Index, std::unique_ptr<Object>> _objects;
247 uint32_t _next_index;
248};
249
250} // namespace onert::util
251
252#endif // __ONERT_UTIL_OBJECT_MANAGER_H__
A wrapper class for unsigned integral Index NOTE : Max value of the underlying type is used as the in...
Definition Index.h:37
Class that owns objects and maps them with indices as a handle for them.
Index set(Index index, std::unique_ptr< Object > &&object)
Set the object in the container with given index.
Object & at(const Index &index)
Get the object that is associated with the given index.
size_t size() const
Return the number of objects that the manager contains.
Index emplace(Args &&...args)
Create an object with args and put it in the container with a newly assigned Index.
void remove(const Index &index)
Remove the object that is associated with the given index.
Index push(std::unique_ptr< Object > &&object, Index index)
Put the object in the container with given index.
Index push(std::unique_ptr< Object > &&object)
Put the object in the container with a newly assigned index.
Object * getRawPtr(const Index &index)
Get the object that is associated with the given index.
void iterate(const std::function< void(const Index &, const Object &)> &fn) const
Iterate over the container with given function.
std::unordered_map< Index, std::unique_ptr< Object > > _objects
const Object & at(const Index &index) const
Get the object that is associated with the given index.
const Object * getRawPtr(const Index &index) const
Get the object that is associated with the given index.
void iterate(const std::function< void(const Index &, Object &)> &fn)
Iterate over the container with given function.
bool exist(const Index &index) const
Get the object that is associated with the given index.