ConditionalStorage.hpp
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef OPM_CONDITIONAL_STORAGE_HH
29#define OPM_CONDITIONAL_STORAGE_HH
30
31#include <stdexcept>
32#include <utility>
33
34namespace Opm {
44template <bool cond, class T>
46{
47public:
48 typedef T type;
49 static constexpr bool condition = cond;
50
52 {}
53
55 : data_(v)
56 {}
57
59 : data_(std::move(v))
60 {}
61
62 template <class ...Args>
63 ConditionalStorage(Args... args)
64 : data_(args...)
65 {}
66
68 : data_(t.data_)
69 {};
70
72 : data_(std::move(t.data_))
73 {};
74
76 {
77 data_ = v.data_;
78 return *this;
79 }
80
82 {
83 data_ = std::move(v.data_);
84 return *this;
85 }
86
87 const T& operator*() const
88 { return data_; }
90 { return data_; }
91
92 const T* operator->() const
93 { return &data_; }
95 { return &data_; }
96
97private:
98 T data_;
99};
100
101template <class T>
102class ConditionalStorage<false, T>
103{
104public:
105 typedef T type;
106 static constexpr bool condition = false;
107
109 {
110 // ensure that T has a default constructor without actually calling it
111 if (false) {
112 [[maybe_unused]] T dummy; // <- if the compiler bails out here, T does not have a default constructor
113 }
114 }
115
117 {
118 // ensure that T has a default constructor without actually calling it
119 if (false) {
120 [[maybe_unused]] T dummy(v); // <- if the compiler bails out here, T does not have a copy constructor
121 }
122 }
123
125 {
126 // copying an empty conditional storage object does not do anything.
127 };
128
129 template <class ...Args>
130 ConditionalStorage(Args... args)
131 {
132 // ensure that the arguments are valid without actually calling the constructor
133 // of T
134 if (false) {
135 [[maybe_unused]] T dummy(args...); // <- if the compiler bails out here, T does not have the requested constructor
136 }
137 }
138
140 {
141 // ensure that the stored object can actually be assined to but not actually do
142 // anything
143 if (false) {
144 T *dummy;
145 (*dummy) = (*dummy); // <- if the compiler bails out here, T does not have an assignment operator
146 }
147
148 return *this;
149 }
150
151 const T& operator*() const
152 { throw std::logic_error("data member deactivated"); }
154 { throw std::logic_error("data member deactivated"); }
155
156 const T* operator->() const
157 { throw std::logic_error("data member deactivated"); }
159 { throw std::logic_error("data member deactivated"); }
160};
161
162} // namespace Opm
163
164#endif
ConditionalStorage & operator=(const ConditionalStorage &)
Definition: ConditionalStorage.hpp:139
ConditionalStorage()
Definition: ConditionalStorage.hpp:108
T & operator*()
Definition: ConditionalStorage.hpp:153
ConditionalStorage(const ConditionalStorage &)
Definition: ConditionalStorage.hpp:124
ConditionalStorage(Args... args)
Definition: ConditionalStorage.hpp:130
T type
Definition: ConditionalStorage.hpp:105
const T * operator->() const
Definition: ConditionalStorage.hpp:156
ConditionalStorage(const T &v)
Definition: ConditionalStorage.hpp:116
T * operator->()
Definition: ConditionalStorage.hpp:158
const T & operator*() const
Definition: ConditionalStorage.hpp:151
A simple class which only stores a given member attribute if a boolean condition is true.
Definition: ConditionalStorage.hpp:46
const T * operator->() const
Definition: ConditionalStorage.hpp:92
static constexpr bool condition
Definition: ConditionalStorage.hpp:49
ConditionalStorage(const ConditionalStorage &t)
Definition: ConditionalStorage.hpp:67
ConditionalStorage()
Definition: ConditionalStorage.hpp:51
ConditionalStorage & operator=(const ConditionalStorage &v)
Definition: ConditionalStorage.hpp:75
ConditionalStorage(Args... args)
Definition: ConditionalStorage.hpp:63
ConditionalStorage(const T &v)
Definition: ConditionalStorage.hpp:54
ConditionalStorage(ConditionalStorage &&t)
Definition: ConditionalStorage.hpp:71
ConditionalStorage(T &&v)
Definition: ConditionalStorage.hpp:58
T & operator*()
Definition: ConditionalStorage.hpp:89
T * operator->()
Definition: ConditionalStorage.hpp:94
T type
Definition: ConditionalStorage.hpp:48
ConditionalStorage & operator=(ConditionalStorage &&v)
Definition: ConditionalStorage.hpp:81
const T & operator*() const
Definition: ConditionalStorage.hpp:87
Definition: Air_Mesitylene.hpp:34