iceoryx_doc  1.0.1
smart_lock.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex AI Inc. All rights reserved.
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 #ifndef IOX_UTILS_CONCURRENT_SMART_LOCK_HPP
18 #define IOX_UTILS_CONCURRENT_SMART_LOCK_HPP
19 
20 #include <mutex>
21 
22 namespace iox
23 {
24 namespace concurrent
25 {
51 template <typename T, typename MutexType = ::std::mutex>
52 class smart_lock
53 {
54  private:
55  class Proxy
56  {
57  public:
58  Proxy(T* base, MutexType* lock) noexcept;
59  ~Proxy() noexcept;
60 
61  T* operator->() noexcept;
62  T* operator->() const noexcept;
63 
64  private:
65  T* base;
66  MutexType* lock;
67  };
68 
69  public:
71  smart_lock() noexcept;
72 
74  smart_lock(const T& t) noexcept;
75 
77  template <typename... ArgTypes>
78  smart_lock(ArgTypes&&... args) noexcept;
79 
80  smart_lock(const smart_lock& rhs) noexcept;
81  smart_lock(smart_lock&& rhs) noexcept;
82  smart_lock& operator=(const smart_lock& rhs) noexcept;
83  smart_lock& operator=(smart_lock&& rhs) noexcept;
84  ~smart_lock() noexcept = default;
85 
95  Proxy operator->() noexcept;
96 
106  Proxy operator->() const noexcept;
107 
129  Proxy GetScopeGuard() noexcept;
130 
152  Proxy GetScopeGuard() const noexcept;
153 
155  T GetCopy() const noexcept;
156 
157  private:
158  T base;
159  mutable MutexType lock;
160 };
161 } // namespace concurrent
162 } // namespace iox
163 
164 #include "iceoryx_utils/internal/concurrent/smart_lock.inl"
165 
166 #endif // IOX_UTILS_CONCURRENT_SMART_LOCK_HPP
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28