CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
memberStore.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <new>
38#include <utility>
39
40#include "../config.h"
41#include "../misc/macros.hpp"
42
44namespace codi {
45
55 template<typename T_Type, typename T_Parent, bool T_storeStatic = false>
56 struct MemberStore {
57 public:
58
60 using Type = T_Type; // Default declaration breaks auto completion.
61 using Parent = CODI_DD(T_Parent, CODI_ANY);
62
63 static bool constexpr storeStatic = T_storeStatic;
64
65 private:
66
67 Type member;
68
69 public:
70
72 template<typename... Args>
73 MemberStore(Args&&... args) : member(std::forward<Args>(args)...) {}
74
76 Type& get() {
77 return member;
78 }
79
81 Type const& get() const {
82 return member;
83 }
84 };
85
87 template<typename T_Type, typename T_Parent>
88 struct MemberStore<T_Type, T_Parent, true> {
89 public:
90
91 using Type = T_Type;
92 using Parent = CODI_DD(T_Parent, CODI_ANY);
93
94 static bool constexpr storeStatic = true;
95
96 private:
97
98 static char member[sizeof(Type)];
99 static bool isInitialized;
100
101 public:
102
104 template<typename... Args>
105 MemberStore(Args&&... args) {
106 if (!isInitialized) {
107 isInitialized = true;
108 new (member) Type(std::forward<Args>(args)...);
109 }
110 }
111
114 return *((Type*)MemberStore::member);
115 }
116
118 CODI_INLINE Type const& get() const {
119 return *((Type const*)MemberStore::member);
120 }
121 };
122
123#ifndef DOXYGEN_DISABLE
124 template<typename Type, typename Parent>
125 char MemberStore<Type, Parent, true>::member[sizeof(Type)] = {};
126
127 template<typename Type, typename Parent>
128 bool MemberStore<Type, Parent, true>::isInitialized = false;
129#endif
130}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_ANY
Used in default declarations of expression templates.
Definition macros.hpp:98
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
T_Type Type
See MemberStore.
Definition memberStore.hpp:91
T_Parent Parent
See MemberStore.
Definition memberStore.hpp:92
Type const & get() const
Get a reference to the actual member.
Definition memberStore.hpp:118
MemberStore(Args &&... args)
Arguments are forwarded to the constructor of the member.
Definition memberStore.hpp:105
Type & get()
Get a reference to the actual member.
Definition memberStore.hpp:113
Defines a member that can either be static or local to the struct.
Definition memberStore.hpp:56
static bool constexpr storeStatic
See MemberStore.
Definition memberStore.hpp:63
MemberStore(Args &&... args)
Arguments are forwarded to the constructor of the member.
Definition memberStore.hpp:73
T_Type Type
< See MemberStore
Definition memberStore.hpp:60
T_Parent Parent
See MemberStore.
Definition memberStore.hpp:61
Type & get()
Get a reference to the actual member.
Definition memberStore.hpp:76
Type const & get() const
Get a reference to the actual member.
Definition memberStore.hpp:81