TPIE

2362a60
maybe.h
Go to the documentation of this file.
1 // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
2 // vi:set ts=4 sts=4 sw=4 noet :
3 //
4 // Copyright 2011, The TPIE development team
5 //
6 // This file is part of TPIE.
7 //
8 // TPIE is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License as published by the
10 // Free Software Foundation, either version 3 of the License, or (at your
11 // option) any later version.
12 //
13 // TPIE is distributed in the hope that it will be useful, but WITHOUT ANY
14 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 // License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with TPIE. If not, see <http://www.gnu.org/licenses/>
20 
24 
25 #ifndef __TPIE_MAYBE_H__
26 #define __TPIE_MAYBE_H__
27 
28 #include <tpie/exception.h>
29 #include <tpie/config.h>
30 
31 namespace tpie {
32 
33 template <typename T>
34 class maybe {
35 public:
39  maybe() : m_constructed(false) {
40  }
41 
45  maybe(const maybe<T> & o) : m_constructed(false) {
46  o.assert_not_constructed();
47  }
48 
49  maybe(maybe<T> && o) : m_constructed(false) {
50  o.assert_not_constructed();
51  }
52 
53 private:
54  void assert_not_constructed() const {
55  if(m_constructed)
56  throw maybe_exception("Maybe object already constructed");
57  }
58 public:
59 
63  maybe & operator=(const maybe<T> &o) {
64  o.assert_not_constructed();
65  assert_not_constructed();
66 
67  return *this;
68  }
69 
70  maybe & operator==(maybe<T> &o) {
71  o.assert_not_constructed();
72  assert_not_constructed();
73 
74  return *this;
75  }
76 
80  bool is_constructed() const {
81  return m_constructed;
82  }
83 
96 
97  template <typename ... T_ARGS>
98  void construct(T_ARGS && ... t) {
99  assert_not_constructed();
100 
101  new(&object) T(std::forward<T_ARGS>(t)...);
102  m_constructed = true;
103  }
104 
108  T & operator*() {
109  return object;
110  }
111 
115  const T & operator*() const {
116  return object;
117  }
118 
122  T * operator->() {
123  return &object;
124  }
125 
129  const T * operator->() const {
130  return &object;
131  }
132 
136  void destruct() {
137  if(!m_constructed)
138  return;
139 
140  object.~T();
141  m_constructed = false;
142  }
143 
147  ~maybe() {
148  destruct();
149  }
150 private:
151  union { T object; };
152  bool m_constructed;
153 };
154 } // namespace tpie
155 
156 #endif
maybe(const maybe< T > &o)
If o is constructed throw an exception otherwise do nothing.
Definition: maybe.h:45
T * operator->()
Definition: maybe.h:122
Exception classes.
bool is_constructed() const
Definition: maybe.h:80
void destruct()
Invokes the deconstructor on the object contained.
Definition: maybe.h:136
maybe & operator=(const maybe< T > &o)
If o is constructed throw an exception otherwise return *this.
Definition: maybe.h:63
~maybe()
Calls the destruct method and destroys the instance.
Definition: maybe.h:147
void construct(T_ARGS &&...t)
Contains an element of the type given as template parameter and disallows copy constructing of the ma...
Definition: maybe.h:98
T & operator*()
Definition: maybe.h:108
const T & operator*() const
Definition: maybe.h:115
const T * operator->() const
Definition: maybe.h:129
maybe()
Construct a new unconstructed maybe object.
Definition: maybe.h:39