olp-cpp-sdk  1.22.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Atomic.h
1 /*
2  * Copyright (C) 2019-2021 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <functional>
23 #include <mutex>
24 #include <utility>
25 
26 namespace olp {
27 namespace thread {
39 template <class Type, typename MutexType = std::mutex,
40  typename ReadLockType = std::lock_guard<MutexType> >
41 class Atomic {
42  public:
46  using value_type = Type;
47 
51  using WriteLockType = std::lock_guard<MutexType>;
52 
59  template <class SomeType>
60  explicit Atomic(SomeType&& am) : m(std::forward<SomeType>(am)) {}
61 
65  Atomic() : m() {}
66 
75  template <class Functor>
76  auto locked(Functor&& lambda) -> decltype(lambda(std::declval<Type&>())) {
77  WriteLockType lock(m_mutex);
78  return lambda(m);
79  }
80 
91  template <class Functor>
92  auto locked(Functor&& lambda) const
93  -> decltype(lambda(std::declval<const Type&>())) const {
94  ReadLockType lock(m_mutex);
95  return lambda(m);
96  }
97 
103  Type lockedCopy() const {
104  ReadLockType lock(m_mutex);
105  return m;
106  }
107 
113  Type lockedMove() {
114  WriteLockType lock(m_mutex);
115  return std::move(m);
116  }
117 
124  template <class SomeType>
125  void lockedAssign(SomeType&& am) {
126  WriteLockType lock(m_mutex);
127  m = std::forward<SomeType>(am);
128  }
129 
135  void lockedSwap(Type& other) {
136  WriteLockType lock(m_mutex);
137  std::swap(m, other);
138  }
139 
147  WriteLockType lock(m_mutex);
148  Type other{};
149  std::swap(m, other);
150  return other;
151  }
152 
158  explicit operator bool() const {
159  ReadLockType lock(m_mutex);
160  return static_cast<bool>(m);
161  }
162 
163  protected:
167  mutable MutexType m_mutex;
168 
172  Type m;
173 };
174 
175 } // namespace thread
176 } // namespace olp
A simple atomic wrapper.
Definition: Atomic.h:41
Type lockedCopy() const
Gets a copy of the data using the lock.
Definition: Atomic.h:103
auto locked(Functor &&lambda) -> decltype(lambda(std::declval< Type & >()))
Calls the lambda function using the unique lock.
Definition: Atomic.h:76
auto locked(Functor &&lambda) const -> decltype(lambda(std::declval< const Type & >())) const
Calls the lambda using the lock.
Definition: Atomic.h:92
Type value_type
Alias for the type.
Definition: Atomic.h:46
Type m
The member type to which atomic access is required.
Definition: Atomic.h:172
std::lock_guard< MutexType > WriteLockType
Alias for the atomic write mutex lock.
Definition: Atomic.h:51
Atomic(SomeType &&am)
Creates the Atomic instance.
Definition: Atomic.h:60
MutexType m_mutex
Defines the lock type.
Definition: Atomic.h:167
Atomic()
Creates the Atomic instance.
Definition: Atomic.h:65
void lockedSwap(Type &other)
Exchanges context with the Type object using the unique lock.
Definition: Atomic.h:135
Type lockedMove()
Gets a copy of the moved data using the unique lock.
Definition: Atomic.h:113
void lockedAssign(SomeType &&am)
Assigns the data using the unique lock.
Definition: Atomic.h:125
Type lockedSwapWithDefault()
Exchanges context with the Type object using the unique lock and moves data.
Definition: Atomic.h:146
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24