DIY 3.0 data-parallel out-of-core C++ library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List

All Classes Namespaces Functions Typedefs Groups Pages
optional.hpp
1 namespace diy
2 {
3 namespace mpi
4 {
5 template<class T>
6 struct optional
7 {
8 optional():
9 init_(false) {}
10
11 optional(const T& v):
12 init_(true) { new(buf_) T(v); }
13
14 optional(const optional& o):
15 init_(o.init_) { if (init_) new(buf_) T(*o); }
16
17 ~optional() { if (init_) clear(); }
18
19 inline
20 optional& operator=(const optional& o);
21
22 operator bool() const { return init_; }
23
24 T& operator*() { return *static_cast<T*>(address()); }
25 const T& operator*() const { return *static_cast<const T*>(address()); }
26
27 T* operator->() { return &(operator*()); }
28 const T* operator->() const { return &(operator*()); }
29
30 private:
31 void clear() { static_cast<T*>(address())->~T(); }
32
33 void* address() { return buf_; }
34 const void* address() const { return buf_; }
35
36 private:
37 bool init_;
38 char buf_[sizeof(T)];
39 };
40 }
41 }
42
43 template<class T>
44 diy::mpi::optional<T>&
45 diy::mpi::optional<T>::
46 operator=(const optional& o)
47 {
48 if (init_)
49 clear();
50 init_ = o.init_;
51 if (init_)
52 new (buf_) T(*o);
53
54 return *this;
55 }
diy::mpi::optional
Definition: optional.hpp:6