My Project
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>
33 #include <utility>
34 
35 namespace Opm {
45 template <bool cond, class T>
47 {
48 public:
49  typedef T type;
50  static constexpr bool condition = cond;
51 
53  {}
54 
55  ConditionalStorage(const T& v)
56  : data_(v)
57  {}
58 
59  ConditionalStorage(T&& v)
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 
98 private:
99  T data_;
100 };
101 
102 template <class T>
103 class ConditionalStorage<false, T>
104 {
105 public:
106  typedef T type;
107  static constexpr bool condition = false;
108 
110  {
111  // ensure that T has a default constructor without actually calling it
112  if (false) {
113  [[maybe_unused]] T dummy; // <- if the compiler bails out here, T does not have a default constructor
114  }
115  }
116 
117  ConditionalStorage(const T& v)
118  {
119  // ensure that T has a default constructor without actually calling it
120  if (false) {
121  [[maybe_unused]] T dummy(v); // <- if the compiler bails out here, T does not have a copy constructor
122  }
123  }
124 
126  {
127  // copying an empty conditional storage object does not do anything.
128  };
129 
130  template <class ...Args>
131  ConditionalStorage(Args... args)
132  {
133  // ensure that the arguments are valid without actually calling the constructor
134  // of T
135  if (false) {
136  [[maybe_unused]] T dummy(args...); // <- if the compiler bails out here, T does not have the requested constructor
137  }
138  }
139 
140  ConditionalStorage& operator=(const ConditionalStorage&)
141  {
142  // ensure that the stored object can actually be assined to but not actually do
143  // anything
144  if (false) {
145  T *dummy;
146  (*dummy) = (*dummy); // <- if the compiler bails out here, T does not have an assignment operator
147  }
148 
149  return *this;
150  }
151 
152  const T& operator*() const
153  { throw std::logic_error("data member deactivated"); }
154  T& operator*()
155  { throw std::logic_error("data member deactivated"); }
156 
157  const T* operator->() const
158  { throw std::logic_error("data member deactivated"); }
159  T* operator->()
160  { throw std::logic_error("data member deactivated"); }
161 };
162 
163 } // namespace Opm
164 
165 #endif
Provides the OPM_UNUSED macro.
A simple class which only stores a given member attribute if a boolean condition is true.
Definition: ConditionalStorage.hpp:47