Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
value-latch.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Contact: Lukasz Kostyra <l.kostyra@samsung.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
17 */
18 
25 #ifndef COMMON_UTILS_VALUE_LATCH_H
26 #define COMMON_UTILS_VALUE_LATCH_H
27 
28 #include "utils/exception.hpp"
29 
30 #include <memory>
31 #include <mutex>
32 #include <condition_variable>
33 
34 namespace utils {
35 
36 template <typename T>
37 class ValueLatch {
38 public:
44  void set(const T& value);
45 
51  void set(T&& value);
52 
58  T get();
59 
67  T get(const unsigned int timeoutMs);
68 
69 private:
70  std::mutex mMutex;
71  std::condition_variable mCondition;
72  std::unique_ptr<T> mValue;
73 };
74 
75 template <typename T>
76 void ValueLatch<T>::set(const T& value)
77 {
78  std::unique_lock<std::mutex> lock(mMutex);
79  if (mValue) {
80  throw UtilsException("Cannot set value multiple times");
81  }
82  mValue.reset(new T(value));
83  mCondition.notify_one();
84 }
85 
86 template <typename T>
87 void ValueLatch<T>::set(T&& value)
88 {
89  std::unique_lock<std::mutex> lock(mMutex);
90  if (mValue) {
91  throw UtilsException("Cannot set value multiple times");
92  }
93  mValue.reset(new T(std::move(value)));
94  mCondition.notify_one();
95 }
96 
97 template <typename T>
99 {
100  std::unique_lock<std::mutex> lock(mMutex);
101  mCondition.wait(lock, [this]() {
102  return (bool)mValue;
103  });
104  std::unique_ptr<T> retValue(std::move(mValue));
105  return T(std::move(*retValue));
106 }
107 
108 template <typename T>
109 T ValueLatch<T>::get(const unsigned int timeoutMs)
110 {
111  std::unique_lock<std::mutex> lock(mMutex);
112  if (mCondition.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this]() {
113  return (bool)mValue;
114  }) ) {
115  std::unique_ptr<T> retValue(std::move(mValue));
116  return T(std::move(*retValue));
117  } else {
118  throw UtilsException("Timeout occured");
119  }
120 }
121 
122 } // namespace utils
123 
124 #endif // COMMON_UTILS_VALUE_LATCH_H
std::unique_ptr< T > mValue
Definition: value-latch.hpp:72
Base class for exceptions in utils.
Definition: exception.hpp:37
T get()
Waits until set() is called, then set value is moved to caller.
Definition: value-latch.hpp:98
std::condition_variable mCondition
Definition: value-latch.hpp:71
Exceptions for the server.
void set(const T &value)
Assigns value to kept variable and sets Latch.
Definition: value-latch.hpp:76
Definition: value-latch.hpp:37
std::mutex mMutex
Definition: value-latch.hpp:70