Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
optional.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_OPTIONAL_H
21 #define GALOIS_OPTIONAL_H
22 
23 #include <cassert>
24 
25 #include "galois/config.h"
26 #include "galois/LazyObject.h"
27 
28 namespace galois {
29 
33 template <typename T>
34 class optional {
35  LazyObject<T> data_;
36  bool initialized_;
37 
38  void construct(const T& val) {
39  data_.construct(val);
40  initialized_ = true;
41  }
42 
43  void assign_impl(const T& val) { get_impl() = val; }
44 
45  void destroy() {
46  if (initialized_) {
47  data_.destroy();
48  initialized_ = false;
49  }
50  }
51 
52  T& get_impl() { return data_.get(); }
53  const T& get_impl() const { return data_.get(); }
54 
55 public:
56  typedef bool (optional::*unspecified_bool_type)() const;
57 
58  optional() : initialized_(false) {}
59 
60  optional(const T& val) : initialized_(false) { construct(val); }
61 
62  optional(const optional& rhs) : initialized_(false) {
63  if (rhs.is_initialized())
64  construct(rhs.get_impl());
65  }
66 
67  template <typename U>
68  explicit optional(const optional<U>& rhs) : initialized_(false) {
69  assign(rhs);
70  }
71 
72  ~optional() { destroy(); }
73 
74  void assign(const optional& rhs) {
75  if (is_initialized()) {
76  if (rhs.is_initialized())
77  assign_impl(rhs.get_impl());
78  else
79  destroy();
80  } else {
81  if (rhs.is_initialized())
82  construct(rhs.get_impl());
83  }
84  }
85 
86  template <typename U>
87  void assign(const optional<U>& rhs) {
88  if (is_initialized()) {
89  if (rhs.is_initialized())
90  assign_impl(rhs.get_impl());
91  else
92  destroy();
93  } else {
94  if (rhs.is_initialized())
95  construct(rhs.get_impl());
96  }
97  }
98 
99  void assign(const T& val) {
100  if (is_initialized())
101  assign_impl(val);
102  else
103  construct(val);
104  }
105 
106  bool is_initialized() const { return initialized_; }
107 
108  optional& operator=(const optional& rhs) {
109  assign(rhs);
110  return *this;
111  }
112 
113  template <typename U>
115  assign(rhs);
116  return *this;
117  }
118 
119  optional& operator=(const T& val) {
120  assign(val);
121  return *this;
122  }
123 
124  T& get() {
125  assert(initialized_);
126  return get_impl();
127  }
128  const T& get() const {
129  assert(initialized_);
130  return get_impl();
131  }
132  T& operator*() { return get(); }
133  const T& operator*() const { return get(); }
134  T* operator->() {
135  assert(initialized_);
136  return &get_impl();
137  }
138  const T* operator->() const {
139  assert(initialized_);
140  return &get_impl();
141  }
142 
143  operator unspecified_bool_type() const {
144  return initialized_ ? &optional::is_initialized : 0;
145  }
146 };
147 
148 } // namespace galois
149 
150 #endif
void assign(const optional< U > &rhs)
Definition: optional.h:87
optional()
Definition: optional.h:58
Single (uninitialized) object with specialization for void type.
Definition: LazyObject.h:71
optional(const optional< U > &rhs)
Definition: optional.h:68
optional & operator=(const optional< U > &rhs)
Definition: optional.h:114
optional & operator=(const optional &rhs)
Definition: optional.h:108
bool is_initialized() const
Definition: optional.h:106
optional(const T &val)
Definition: optional.h:60
optional(const optional &rhs)
Definition: optional.h:62
T & operator*()
Definition: optional.h:132
~optional()
Definition: optional.h:72
void assign(const T &val)
Definition: optional.h:99
Galois version of boost::optional.
Definition: optional.h:34
const T & operator*() const
Definition: optional.h:133
bool(optional::* unspecified_bool_type)() const
Definition: optional.h:56
optional & operator=(const T &val)
Definition: optional.h:119
void assign(const optional &rhs)
Definition: optional.h:74
T * operator->()
Definition: optional.h:134
const T * operator->() const
Definition: optional.h:138