Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
Universal.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 <functional>
27
28#include <alien/data/Universe.h>
30#include <alien/utils/Trace.h>
31#include <iostream>
32#include <utility>
33
34/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
37namespace Alien
38{
39
40/*---------------------------------------------------------------------------*/
41/*---------------------------------------------------------------------------*/
42
48template <typename U>
50{
56 template <typename... T>
58 : m_value(Universe().dataBase().findOrCreate<U>(t...))
59 {}
60
61 std::pair<std::shared_ptr<U>, bool> m_value;
62};
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
71template <typename U>
72class Universal : private UniversalObject<U>
73, private std::shared_ptr<U>
74{
75 public:
81 template <typename... T>
82 Universal(T&... t)
83 : UniversalObject<U>(t...)
84 , std::shared_ptr<U>(this->m_value.first)
85 {
86 alien_debug([&] {
87 if (this->m_value.second)
88 cout() << "Create Universal Object";
89 else
90 cout() << "Find Universal Object";
91 });
92 }
93
94 /*
95 * \brief Initialize an object
96 * \param[in] f The initialize function
97 */
98 void first_time(std::function<void(U&)> f)
99 {
100 if (this->m_value.second) {
101 alien_debug([&] { cout() << "Initialize Universal Object"; });
102 f(*(this->get()));
103 }
104 }
105
106 using std::shared_ptr<U>::operator->;
107};
108
109/*---------------------------------------------------------------------------*/
110/*---------------------------------------------------------------------------*/
111
112} // namespace Alien
113
114/*---------------------------------------------------------------------------*/
115/*---------------------------------------------------------------------------*/
UniverseDataBase.h.
Universe.h.
Universal(T &... t)
Constructor.
Definition Universal.h:82
Alien universe. Common structure to store shared objects between all elements of the library Alien.
Definition Universe.h:53
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17
std::pair< std::shared_ptr< U >, bool > m_value
The object with its instancied flag.
Definition Universal.h:61
UniversalObject(T &... t)
Constructor.
Definition Universal.h:57