Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LazyObject.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_LAZYOBJECT_H
21 #define GALOIS_LAZYOBJECT_H
22 
23 #include <type_traits>
24 #include <utility>
25 
26 #include "galois/config.h"
27 #include "galois/gIO.h"
28 
29 namespace galois {
30 
36 template <typename T>
37 class StrictObject {
38  T data;
39 
40 public:
41  typedef T value_type;
42  typedef T& reference;
43  typedef const T& const_reference;
44  const static bool has_value = true;
45 
47  StrictObject(const_reference t) : data(t) {}
48  const_reference get() const { return data; }
49  reference get() { return data; }
50 };
51 
52 template <>
53 struct StrictObject<void> {
54  typedef void* value_type;
55  typedef void* reference;
56  typedef void* const_reference;
57  const static bool has_value = false;
58 
61  reference get() const { return 0; }
62 };
63 
70 template <typename T>
71 class LazyObject {
72  typedef
73  typename std::aligned_storage<sizeof(T),
74  std::alignment_of<T>::value>::type CharData;
75 
76  union Data {
77  CharData buf;
78  T value_;
79 
80  // Declare constructor explicitly because Data must be default
81  // constructable regardless of the constructability of T.
82  Data() {} // NOLINT(modernize-use-equals-default)
83  ~Data() {} // NOLINT(modernize-use-equals-default)
84 
85  T& value() { return value_; }
86  const T& value() const { return value_; }
87  };
88 
89  Data data_;
90 
91  T* cast() { return &data_.value(); }
92  const T* cast() const { return &data_.value(); }
93 
94 public:
95  typedef T value_type;
96  typedef T& reference;
97  typedef const T& const_reference;
98  const static bool has_value = true;
99  // Can't support incomplete T's but provide same interface as
100  // {@link galois::LargeArray} for consistency
101  struct size_of {
102  const static size_t value = sizeof(T);
103  };
104 
105  void destroy() { cast()->~T(); }
106  void construct(const_reference x) { new (cast()) T(x); }
107 
108  template <typename... Args>
109  void construct(Args&&... args) {
110  new (cast()) T(std::forward<Args>(args)...);
111  }
112 
113  const_reference get() const { return *cast(); }
114  reference get() { return *cast(); }
115 };
116 
117 template <>
118 struct LazyObject<void> {
119  typedef void* value_type;
120  typedef void* reference;
121  typedef void* const_reference;
122  const static bool has_value = false;
123  struct size_of {
124  const static size_t value = 0;
125  };
126 
127  void destroy() {}
129 
130  template <typename... Args>
131  void construct(Args&&...) {}
132 
133  const_reference get() const { return 0; }
134 };
135 
136 } // namespace galois
137 #endif
StrictObject(const_reference t)
Definition: LazyObject.h:47
Single (uninitialized) object with specialization for void type.
Definition: LazyObject.h:71
static const bool has_value
Definition: LazyObject.h:44
T value_type
Definition: LazyObject.h:95
void construct(const_reference x)
Definition: LazyObject.h:106
const T & const_reference
Definition: LazyObject.h:43
Single object with specialization for void type.
Definition: LazyObject.h:37
Definition: LazyObject.h:101
T & reference
Definition: LazyObject.h:96
void * value_type
Definition: LazyObject.h:54
T value_type
Definition: LazyObject.h:41
T & reference
Definition: LazyObject.h:42
void * value_type
Definition: LazyObject.h:119
void * const_reference
Definition: LazyObject.h:56
void destroy()
Definition: LazyObject.h:127
void destroy()
Definition: LazyObject.h:105
void construct(Args &&...)
Definition: LazyObject.h:131
void construct(Args &&...args)
Definition: LazyObject.h:109
StrictObject()
Definition: LazyObject.h:59
const T & const_reference
Definition: LazyObject.h:97
void * const_reference
Definition: LazyObject.h:121
StrictObject(const_reference)
Definition: LazyObject.h:60
void construct(const_reference)
Definition: LazyObject.h:128
StrictObject()
Definition: LazyObject.h:46
static const size_t value
Definition: LazyObject.h:102
void * reference
Definition: LazyObject.h:55
void * reference
Definition: LazyObject.h:120
static const bool has_value
Definition: LazyObject.h:98