DART  6.10.1
LockableReference-impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2021, The DART development contributors
3  * All rights reserved.
4  *
5  * The list of contributors can be found at:
6  * https://github.com/dartsim/dart/blob/master/LICENSE
7  *
8  * This file is provided under the following "BSD-style" License:
9  * Redistribution and use in source and binary forms, with or
10  * without modification, are permitted provided that the following
11  * conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef DART_COMMON_LOCKABLEREFERENCE_IMPL_HPP_
34 #define DART_COMMON_LOCKABLEREFERENCE_IMPL_HPP_
35 
37 
38 namespace dart {
39 namespace common {
40 
41 //==============================================================================
42 template <typename Lockable>
44  std::weak_ptr<const void> lockableHolder, Lockable& lockable) noexcept
45  : mLockableHolder(std::move(lockableHolder)), mLockable(lockable)
46 {
47  // Do nothing
48 }
49 
50 //==============================================================================
51 template <typename Lockable>
53 {
54  if (mLockableHolder.expired())
55  return;
56 
57  mLockable.lock();
58 }
59 
60 //==============================================================================
61 template <typename Lockable>
63 {
64  if (mLockableHolder.expired())
65  return false;
66 
67  return mLockable.try_lock();
68 }
69 
70 //==============================================================================
71 template <typename Lockable>
73 {
74  if (mLockableHolder.expired())
75  return;
76 
77  mLockable.unlock();
78 }
79 
80 //==============================================================================
81 template <typename Lockable>
82 template <typename InputIterator>
84  std::weak_ptr<const void> lockableHolder,
85  InputIterator first,
86  InputIterator last)
87  : mLockableHolder(std::move(lockableHolder)), mLockables(first, last)
88 {
89  using IteratorValueType =
90  typename std::iterator_traits<InputIterator>::value_type;
91  using IteratorLockable = typename std::remove_pointer<
93 
94  static_assert(
95  std::is_same<Lockable, IteratorLockable>::value,
96  "Lockable of this class and the lockable of InputIterator are not the "
97  "same.");
98 }
99 
100 //==============================================================================
101 template <typename Lockable>
103 {
104  if (mLockableHolder.expired())
105  return;
106 
107  for (auto lockable : mLockables)
108  lockable->lock();
109 }
110 
111 //==============================================================================
112 template <typename Lockable>
114 {
115  if (mLockableHolder.expired())
116  return false;
117 
118  for (auto lockable : mLockables)
119  {
120  if (!lockable->try_lock())
121  return false;
122  }
123 
124  return true;
125 }
126 
127 //==============================================================================
128 template <typename Lockable>
130 {
131  if (mLockableHolder.expired())
132  return;
133 
134  for (auto it = mLockables.rbegin(); it != mLockables.rend(); ++it)
135  (*it)->unlock();
136 }
137 
138 //==============================================================================
139 template <typename Lockable>
140 template <typename T>
142 {
143  return &obj;
144 }
145 
146 //==============================================================================
147 template <typename Lockable>
148 template <typename T>
150 {
151  return obj;
152 }
153 
154 } // namespace common
155 } // namespace dart
156 
157 #endif // DART_COMMON_LOCKABLEREFERENCE_IMPL_HPP_
std::string type
Definition: SdfParser.cpp:82
bool try_lock() noexcept override
Tries to lock the lockables that this class references; returns false if one of the lockables is not ...
Definition: LockableReference-impl.hpp:113
MultiLockableReference(std::weak_ptr< const void > lockableHolder, InputIterator first, InputIterator last)
Constructs from multiple lockables.
Definition: LockableReference-impl.hpp:83
void unlock() noexcept override
Unlocks the lockables.
Definition: LockableReference-impl.hpp:129
void lock() override
Locks lockable that this class references; blocks if one of the lockables are not avaliable.
Definition: LockableReference-impl.hpp:102
T * ptr(T &obj)
Converts reference to pointer.
Definition: LockableReference-impl.hpp:141
bool try_lock() noexcept override
Tries to lock the lockables that this class references; returns false if one of the lockables is not ...
Definition: LockableReference-impl.hpp:62
LockableT Lockable
Definition: LockableReference.hpp:82
void unlock() noexcept override
Unlocks the lockables.
Definition: LockableReference-impl.hpp:72
void lock() override
Locks lockable that this class references; blocks if one of the lockables are not avaliable.
Definition: LockableReference-impl.hpp:52
SingleLockableReference(std::weak_ptr< const void > lockableHolder, Lockable &lockable) noexcept
Constructor from a single lockable.
Definition: LockableReference-impl.hpp:43
Definition: BulletCollisionDetector.cpp:65
Definition: SharedLibraryManager.hpp:46