My Project
Loading...
Searching...
No Matches
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 <type_traits>
33#include <utility>
34
35namespace Opm {
45template <bool cond, class T>
47{
48public:
49 typedef T type;
50 static constexpr bool condition = cond;
51
53 {}
54
55 explicit ConditionalStorage(const T& v)
56 : data_(v)
57 {}
58
60 : data_(std::move(v))
61 {}
62
63 template <class ...Args>
64 ConditionalStorage(Args... args)
65 : data_(args...)
66 {}
67
69 : data_(t.data_)
70 {};
71
73 : data_(std::move(t.data_))
74 {};
75
76 ConditionalStorage& operator=(const ConditionalStorage& v)
77 {
78 data_ = v.data_;
79 return *this;
80 }
81
83 {
84 data_ = std::move(v.data_);
85 return *this;
86 }
87
88 const T& operator*() const
89 { return data_; }
90 T& operator*()
91 { return data_; }
92
93 const T* operator->() const
94 { return &data_; }
95 T* operator->()
96 { return &data_; }
97
98private:
99 T data_{};
100};
101
102template <class T>
103class ConditionalStorage<false, T>
104{
105public:
106 typedef T type;
107 static constexpr bool condition = false;
108
110 {
111 static_assert(std::is_default_constructible_v<T>);
112 }
113
114 ConditionalStorage(const T&)
115 {
116 static_assert(std::is_copy_constructible_v<T>);
117 }
118
120 {
121 // copying an empty conditional storage object does not do anything.
122 };
123
124 template <class ...Args>
125 ConditionalStorage(Args...)
126 {
127 static_assert(std::is_constructible_v<T, Args...>);
128 }
129
130 ConditionalStorage& operator=(const ConditionalStorage&)
131 {
132 static_assert(std::is_copy_assignable_v<T>);
133 return *this;
134 }
135
136 const T& operator*() const
137 { throw std::logic_error("data member deactivated"); }
138 T& operator*()
139 { throw std::logic_error("data member deactivated"); }
140
141 const T* operator->() const
142 { throw std::logic_error("data member deactivated"); }
143 T* operator->()
144 { throw std::logic_error("data member deactivated"); }
145};
146
147} // namespace Opm
148
149#endif
A simple class which only stores a given member attribute if a boolean condition is true.
Definition ConditionalStorage.hpp:47
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30