Alien  1.3.0
Developer 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
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
44class ALIEN_EXPORT UniverseDataBase final
45{
46 public:
48 class IKey
49 {
50 protected:
52 IKey() {}
53
54 public:
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
69 class IObject
70 {
71 protected:
74
75 public:
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
98 template <typename T>
99 struct Key : public IKey
100 {
102 virtual ~Key() {}
103
108 Key(T&& t)
109 : value(std::move(t))
110 {}
111
114 };
115
120 template <typename T>
121 struct Object : public IObject
122 {
124 virtual ~Object() {}
125
130 Object(std::shared_ptr<T> v)
131 : value(v)
132 {}
133
134 std::shared_ptr<T> value;
135 };
136
137 public:
142 {
143 public:
146
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
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:
185 std::list<std::shared_ptr<IObject>> m_objects;
186 };
187
188 public:
191
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:
219 std::vector<std::shared_ptr<IKey>> m_keys;
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::list< std::shared_ptr< IObject > > m_objects
The list of objects.
std::pair< std::shared_ptr< U >, bool > findOrCreate(T &... t)
Find or create an object.
std::vector< std::shared_ptr< ObjectList > > m_objects
The list of objects.
std::pair< std::shared_ptr< U >, bool > findOrCreate(T &... t)
Finds or creates an object.
std::vector< std::shared_ptr< IKey > > m_keys
The list of keys.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17
Key(T &&t)
Constructor \para[in] t The key.
virtual ~Key()
Free resources.
virtual ~Object()
Free resources.
Object(std::shared_ptr< T > v)
Constructor.
std::shared_ptr< T > value
The object.