summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/include/util/ObjectManager.h
blob: fd2c3f295759723ea8e85f977e1d07f3ff9644b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __NEURUN_UTIL_OBJECT_MANAGER_H__
#define __NEURUN_UTIL_OBJECT_MANAGER_H__

#include <unordered_map>
#include <memory>

namespace neurun
{
namespace util
{

/**
 * @brief Class that owns objects and maps them with indices as a handle for them
 *
 */
template <typename Index, typename Object> class ObjectManager
{
public:
  ObjectManager() : _index_count{0u} {}

public:
  /**
   * @brief Create an object with args and put it in the container with a new Index for that
   *
   * @param[in] args Arguments for creating Operand object
   * @return Created index that is associated to the object
   */
  template <class... Args> Index emplace(Args &&... args)
  {
    auto index = generateIndex();
    _objects.emplace(index, nnfw::cpp14::make_unique<Object>(std::forward<Args>(args)...));
    return index;
  }

  /**
   * @brief Put object in the container with a new Index for that
   *
   * @param[in] object Object to be pushed
   * @return Created index that is associated to the object
   */
  Index push(std::unique_ptr<Object> &&object)
  {
    auto index = generateIndex();
    _objects.emplace(index, std::move(object));
    return index;
  }

  /**
   * @brief Remove the object that is associated with the given index
   *
   * @param[in] index Index of the object to be removed
   * @return N/A
   */
  void remove(const Index &index) { _objects.erase(index); };

  /**
   * @brief Get the object that is associated with the given index
   *
   * @param[in] index Index of the object to be returned
   * @return Object
   */
  const Object &at(const Index &index) const { return *(_objects.at(index)); }
  /**
   * @brief Get the object that is associated with the given index
   *
   * @param[in] index Index of the object to be returned
   * @return Object
   */
  Object &at(const Index &index) { return *(_objects.at(index)); }
  /**
   * @brief Get the object that is associated with the given index
   *
   * @param[in] index Index of the object to be returned
   * @return true if such entry exists otherwise false
   */
  bool exist(const Index &index) const
  {
    auto it = _objects.find(index);
    return it != _objects.end();
  }
  /**
   * @brief Iterate over the container with given function
   *
   * @param[in] fn Function to be run for every container entry
   * @return N/A
   */
  void iterate(const std::function<void(const Index &, const Object &)> &fn) const
  {
    for (const auto &e : _objects)
    {
      fn(e.first, *e.second);
    }
  }
  /**
   * @brief Iterate over the container with given function
   *
   * @param[in] fn Function to be run for every container entry
   * @return N/A
   */
  void iterate(const std::function<void(const Index &, Object &)> &fn)
  {
    // TODO Remove this workaround
    // This implementation is a workaround in case of adding operands while iteration
    std::list<Index> l;

    for (auto &e : _objects)
    {
      l.push_back(e.first);
    }

    for (auto index : l)
    {
      fn(index, *_objects[index]);
    }
  }

private:
  Index generateIndex() { return Index{_index_count++}; }

private:
  std::unordered_map<Index, std::unique_ptr<Object>> _objects;
  uint32_t _index_count;
};

} // namespace util
} // namespace neurun

#endif // __NEURUN_UTIL_OBJECT_MANAGER_H__