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>
32 #include <utility>
33 
34 namespace Opm {
44 template <bool cond, class T>
46 {
47 public:
48  typedef T type;
49  static constexpr bool condition = cond;
50 
52  {}
53 
54  ConditionalStorage(const T& v)
55  : data_(v)
56  {}
57 
58  ConditionalStorage(T&& v)
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 
75  ConditionalStorage& operator=(const ConditionalStorage& v)
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_; }
89  T& operator*()
90  { return data_; }
91 
92  const T* operator->() const
93  { return &data_; }
94  T* operator->()
95  { return &data_; }
96 
97 private:
98  T data_;
99 };
100 
101 template <class T>
102 class ConditionalStorage<false, T>
103 {
104 public:
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 
116  ConditionalStorage(const T& v)
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 
139  ConditionalStorage& operator=(const ConditionalStorage&)
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"); }
153  T& operator*()
154  { throw std::logic_error("data member deactivated"); }
155 
156  const T* operator->() const
157  { throw std::logic_error("data member deactivated"); }
158  T* operator->()
159  { throw std::logic_error("data member deactivated"); }
160 };
161 
162 } // namespace Opm
163 
164 #endif
A simple class which only stores a given member attribute if a boolean condition is true.
Definition: ConditionalStorage.hpp:46