Alien  1.3.0
User documentation
Loading...
Searching...
No Matches
UniverseDataBase.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 IFPEN-CEA
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 * SPDX-License-Identifier: Apache-2.0
17 */
18
19/*!
20 * \file UniverseDataBase.h
21 * \brief UniverseDataBase.h
22 */
23
24#pragma once
25
26#include <list>
27#include <utility>
28#include <vector>
29
30#include <alien/utils/Precomp.h>
31
32/*---------------------------------------------------------------------------*/
33/*---------------------------------------------------------------------------*/
34
35namespace Alien
36{
37
38/*---------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------*/
40
41/*!
42 * \brief Data base for universe objects
43 */
44class ALIEN_EXPORT UniverseDataBase final
45{
46 public:
47 //! Key object interface
48 class IKey
49 {
50 protected:
51 //! Constructor
52 IKey() {}
53
54 public:
55 //! Free resources
56 virtual ~IKey() {}
57
58 private:
59 IKey(const IKey&) = delete;
60
61 IKey(IKey&&) = delete;
62
63 void operator=(const IKey&) = delete;
64
65 void operator=(IKey&&) = delete;
66 };
67
68 //! Object interface
69 class IObject
70 {
71 protected:
72 //! Constructor
74
75 public:
76 //! Free resources
77 virtual ~IObject() {}
78
79 private:
80 IObject(const IObject&) = delete;
81
82 IObject(IObject&&) = delete;
83
84 void operator=(const IObject&) = delete;
85
86 void operator=(IObject&&) = delete;
87 };
88
89 private:
90 template <typename T>
91 using key_type =
92 typename std::conditional<std::is_copy_constructible<T>::value, T, T&>::type;
93
94 /*!
95 * \brief Key object
96 * \tparam T The type of the key
97 */
98 template <typename T>
99 struct Key : public IKey
100 {
101 //! Free resources
102 virtual ~Key() {}
103
104 /*!
105 * \brief Constructor
106 * \para[in] t The key
107 */
108 Key(T&& t)
109 : value(std::move(t))
110 {}
111
112 //! The key
113 T value;
114 };
115
116 /*!
117 * \brief Object
118 * \tparam T The type of the key
119 */
120 template <typename T>
121 struct Object : public IObject
122 {
123 //! Free resources
124 virtual ~Object() {}
125
126 /*!
127 * \brief Constructor
128 * \param[in] v The object
129 */
130 Object(std::shared_ptr<T> v)
131 : value(v)
132 {}
133 //! The object
134 std::shared_ptr<T> value;
135 };
136
137 public:
138 /*!
139 * \brief List of objects
140 */
142 {
143 public:
144 //! Constructor
146
147 /*!
148 * \brief Find or create an object
149 * \tparam U The type of the object
150 * \tparam T The type of the objects
151 * \param[in] t The objects
152 * \returns The object and the initialization flag
153 */
154 template <typename U, typename... T>
155 std::pair<std::shared_ptr<U>, bool> findOrCreate(T&... t)
156 {
157 using object_type = Object<U>;
158 for (auto& o : m_objects) {
159 auto _o = std::dynamic_pointer_cast<object_type>(o);
160 if (_o) {
161 return std::make_pair(_o->value, false);
162 }
163 }
164 return std::make_pair(create<U>(t...), true);
165 }
166
167 /*!
168 * \brief Creates an object
169 * \tparam U The type of the object
170 * \tparam T The type of the objects
171 * \param[in] t The objects
172 * \returns The object created
173 */
174 template <typename U, typename... T>
175 std::shared_ptr<U> create(T&... t)
176 {
177 auto u = std::make_shared<U>(t...);
178 std::shared_ptr<IObject> o(new Object<U>(u));
179 m_objects.push_back(o);
180 return u;
181 }
182
183 private:
184 //! The list of objects
185 std::list<std::shared_ptr<IObject>> m_objects;
186 };
187
188 public:
189 //! Constructor
191
192 /*!
193 * \brief Finds or creates an object
194 * \tparam U The type of the object
195 * \tparam T The type of the objects
196 * \param[in] t The objects
197 * \returns The object and the initialization flag
198 */
199 template <typename U, typename... T>
200 std::pair<std::shared_ptr<U>, bool> findOrCreate(T&... t)
201 {
202 using type = std::tuple<key_type<T>...>;
203 using key_type = Key<type>;
204 type v{ t... };
205 for (auto i = 0u; i < m_keys.size(); ++i) {
206 auto _k = std::dynamic_pointer_cast<key_type>(m_keys[i]);
207 if (_k && std::operator==(_k->value, v)) {
208 return m_objects[i]->findOrCreate<U>(t...);
209 }
210 }
211 m_keys.push_back(std::shared_ptr<IKey>(new key_type{ std::move(v) }));
212 auto objects = std::make_shared<ObjectList>();
213 m_objects.push_back(objects);
214 return std::make_pair(objects->create<U>(t...), true);
215 }
216
217 private:
218 //! The list of keys
219 std::vector<std::shared_ptr<IKey>> m_keys;
220 //! The list of objects
221 std::vector<std::shared_ptr<ObjectList>> m_objects;
222};
223
224/*---------------------------------------------------------------------------*/
225/*---------------------------------------------------------------------------*/
226
227} // namespace Alien
228
229/*---------------------------------------------------------------------------*/
230/*---------------------------------------------------------------------------*/
virtual ~IKey()
Free resources.
virtual ~IObject()
Free resources.
std::shared_ptr< U > create(T &... t)
Creates an object.
std::pair< std::shared_ptr< U >, bool > findOrCreate(T &... t)
Find or create an object.
std::pair< std::shared_ptr< U >, bool > findOrCreate(T &... t)
Finds or creates an object.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17