Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Future.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * Future.h
17  * An experimental implementation of Futures.
18  * Copyright (C) 2010 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_THREAD_FUTURE_H_
22 #define INCLUDE_OLA_THREAD_FUTURE_H_
23 
24 #include <ola/thread/FuturePrivate.h>
25 
26 namespace ola {
27 namespace thread {
28 
32 template <typename T>
33 class Future {
34  public:
35  Future() : m_impl(new FutureImpl<T>()) {}
36 
37  ~Future() {
38  m_impl->DeRef();
39  }
40 
41  Future(const Future &other) : m_impl(other.m_impl) {
42  other.m_impl->Ref();
43  }
44 
45  Future& operator=(const Future<T> &other) {
46  if (this != &other) {
47  m_impl->DeRef();
48  other.m_impl->Ref();
49  m_impl = other.m_impl;
50  }
51  return *this;
52  }
53 
54  bool IsComplete() const {
55  return m_impl->IsComplete();
56  }
57 
58  const T& Get() {
59  return m_impl->Get();
60  }
61 
62  void Set(const T &t) {
63  m_impl->Set(t);
64  }
65 
66  private:
67  class FutureImpl<T> *m_impl;
68 };
69 
73 template <>
74 class Future<void> {
75  public:
76  Future() : m_impl(new FutureImpl<void>()) {}
77 
78  ~Future() {
79  m_impl->DeRef();
80  }
81 
82  Future(const Future &other) : m_impl(other.m_impl) {
83  other.m_impl->Ref();
84  }
85 
86  Future& operator=(const Future<void> &other) {
87  if (this != &other) {
88  m_impl->DeRef();
89  other.m_impl->Ref();
90  m_impl = other.m_impl;
91  }
92  return *this;
93  }
94 
95  bool IsComplete() const {
96  return m_impl->IsComplete();
97  }
98 
99  void Get() {
100  m_impl->Get();
101  }
102 
103  void Set() {
104  m_impl->Set();
105  }
106 
107  private:
108  class FutureImpl<void> *m_impl;
109 };
110 } // namespace thread
111 } // namespace ola
112 #endif // INCLUDE_OLA_THREAD_FUTURE_H_
Definition: FuturePrivate.h:32
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: Future.h:74
Definition: FuturePrivate.h:99
Definition: Future.h:33