DART 6.7.3
Loading...
Searching...
No Matches
LockableReference-impl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2019, 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
38namespace dart {
39namespace common {
40
41//==============================================================================
42template <typename Lockable>
44 std::weak_ptr<const void> lockableHolder, Lockable& lockable) noexcept
45 : mLockableHolder(std::move(lockableHolder)),
46 mLockable(lockable)
47{
48 // Do nothing
49}
50
51//==============================================================================
52template <typename Lockable>
54{
55 if (mLockableHolder.expired())
56 return;
57
58 mLockable.lock();
59}
60
61//==============================================================================
62template <typename Lockable>
64{
65 if (mLockableHolder.expired())
66 return false;
67
68 return mLockable.try_lock();
69}
70
71//==============================================================================
72template <typename Lockable>
74{
75 if (mLockableHolder.expired())
76 return;
77
78 mLockable.unlock();
79}
80
81//==============================================================================
82template <typename Lockable>
83template <typename InputIterator>
85 std::weak_ptr<const void> lockableHolder,
86 InputIterator first,
87 InputIterator last)
88 : mLockableHolder(std::move(lockableHolder)), mLockables(first, last)
89{
90 using IteratorValueType =
91 typename std::iterator_traits<InputIterator>::value_type;
92 using IteratorLockable = typename std::remove_pointer<
93 typename std::remove_reference<IteratorValueType>::type>::type;
94
95 static_assert(
96 std::is_same<Lockable, IteratorLockable>::value,
97 "Lockable of this class and the lockable of InputIterator are not the "
98 "same.");
99}
100
101//==============================================================================
102template <typename Lockable>
104{
105 if (mLockableHolder.expired())
106 return;
107
108 for (auto lockable : mLockables)
109 lockable->lock();
110}
111
112//==============================================================================
113template <typename Lockable>
115{
116 if (mLockableHolder.expired())
117 return false;
118
119 for (auto lockable : mLockables)
120 {
121 if (!lockable->try_lock())
122 return false;
123 }
124
125 return true;
126}
127
128//==============================================================================
129template <typename Lockable>
131{
132 if (mLockableHolder.expired())
133 return;
134
135 for (auto it = mLockables.rbegin(); it != mLockables.rend(); ++it)
136 (*it)->unlock();
137}
138
139//==============================================================================
140template <typename Lockable>
141template <typename T>
143{
144 return &obj;
145}
146
147//==============================================================================
148template <typename Lockable>
149template <typename T>
151{
152 return obj;
153}
154
155} // namespace common
156} // namespace dart
157
158#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:114
MultiLockableReference(std::weak_ptr< const void > lockableHolder, InputIterator first, InputIterator last)
Constructs from multiple lockables.
Definition LockableReference-impl.hpp:84
void unlock() noexcept override
Unlocks the lockables.
Definition LockableReference-impl.hpp:130
void lock() override
Locks lockable that this class references; blocks if one of the lockables are not avaliable.
Definition LockableReference-impl.hpp:103
T * ptr(T &obj)
Converts reference to pointer.
Definition LockableReference-impl.hpp:142
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:63
LockableT Lockable
Definition LockableReference.hpp:82
void unlock() noexcept override
Unlocks the lockables.
Definition LockableReference-impl.hpp:73
void lock() override
Locks lockable that this class references; blocks if one of the lockables are not avaliable.
Definition LockableReference-impl.hpp:53
SingleLockableReference(std::weak_ptr< const void > lockableHolder, Lockable &lockable) noexcept
Constructor from a single lockable.
Definition LockableReference-impl.hpp:43
Definition BulletCollisionDetector.cpp:63
Definition SharedLibraryManager.hpp:43