i6engine  1.0
Future.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_UTILS_FUTURE_H__
26 #define __I6ENGINE_UTILS_FUTURE_H__
27 
28 #include <atomic>
29 #include <condition_variable>
30 #include <memory>
31 #include <mutex>
32 
33 namespace i6e {
34 namespace utils {
35 
40  template<typename T>
41  class Future {
42  public:
46  Future() : _value(), _finished(false), _lockValue(), _valueCondVar() {
47  }
48 
53  T get() const {
54  if (!_finished) {
55  std::unique_lock<std::mutex> ul(_lockValue);
56  _valueCondVar.wait(ul);
57  }
58  return _value;
59  }
60 
64  void push(T value) {
65  _value = value;
66  _finished = true;
67  std::unique_lock<std::mutex> ul(_lockValue);
68  _valueCondVar.notify_one();
69  }
70 
71  private:
72  T _value;
73  std::atomic<bool> _finished;
74  mutable std::mutex _lockValue;
75  mutable std::condition_variable _valueCondVar;
76  };
77 
78 } /* namespace utils */
79 } /* namespace i6e */
80 
81 #endif /* __I6ENGINE_UTILS_FUTURE_H__ */
82 
Future()
constructor
Definition: Future.h:46
void push(T value)
sets value for this variable, notifies waiting get if existing
Definition: Future.h:64
class for returning a result before it is ready
Definition: Future.h:41