MIRA
ScopedAccess.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_SCOPEDACCESS_H_
49 #define _MIRA_SCOPEDACCESS_H_
50 
51 #ifndef Q_MOC_RUN
52 #include <boost/noncopyable.hpp>
53 #include <boost/thread/mutex.hpp>
54 #endif
55 
56 namespace mira {
57 
59 
61 template <typename Protectee>
62 class ScopedAccessBase : boost::noncopyable
63 {
64 
65 public:
66 
67  ScopedAccessBase(Protectee* p) : mObject(p) {
68  assert(mObject!=NULL);
69  }
70 
71  ScopedAccessBase(ScopedAccessBase&& other) noexcept : mObject(NULL)
72  {
73  std::swap(mObject,other.mObject);
74  }
75 
77  {
78  std::swap(mObject,other.mObject);
79  return *this;
80  }
81 
82 public:
83 
84  Protectee* operator->() {
85  return mObject;
86  }
87 
88  const Protectee* operator->() const {
89  return mObject;
90  }
91 
92  Protectee& operator*() {
93  return *mObject;
94  }
95 
96  const Protectee& operator*() const {
97  return *mObject;
98  }
99 
100 protected:
101  Protectee* mObject;
102 };
103 
105 
118 template <typename Protectee, typename Mutex = void>
119 class ScopedAccess : public ScopedAccessBase<Protectee>
120 {
122 
123 public:
124 
134  ScopedAccess(Protectee* p, Mutex* m, bool lockedAlready = false)
135  : Base(p), mMutex(m) {
136  assert(mMutex!=NULL);
137  if(!lockedAlready)
138  mMutex->lock();
139  }
140 
142  // unlock the object when we go out of scope
143  if (mMutex)
144  mMutex->unlock();
145  }
146 
147  ScopedAccess(ScopedAccess&& other) noexcept : Base(std::move(other)), mMutex(NULL)
148  {
149  std::swap(mMutex, other.mMutex);
150  }
151 
153  {
154  Base::operator=(other);
155  std::swap(mMutex, other.mMutex);
156  return *this;
157  }
158 
159 protected:
160  Mutex* mMutex;
161 };
162 
180 template <typename Protectee>
181 class ScopedAccess<Protectee, void> : public ScopedAccessBase<Protectee>
182 {
184 
185 public:
186 
195  ScopedAccess(Protectee* p, bool lockedAlready = false) : Base(p) {
196  if(!lockedAlready)
197  this->mObject->lock();
198  }
199 
201  // unlock the object when we go out of scope
202  if (this->mObject)
203  this->mObject->unlock();
204  }
205 
206  ScopedAccess(ScopedAccess&& other) noexcept : Base(std::move(other)) {}
207  ScopedAccess& operator=(ScopedAccess&& other) noexcept { Base::operator=(other); }
208 };
209 
211 
232 template <typename T>
233 class ProtecteeMixin : public T, boost::noncopyable
234 {
235 public:
236 
237  void lock() {
238  mMutex.lock();
239  }
240 
241  void unlock() {
242  mMutex.unlock();
243  }
244 
245 private:
246 
247  boost::mutex mMutex;
248 };
249 
251 
252 } // namespace
253 
254 #endif
~ScopedAccess()
Definition: ScopedAccess.h:200
ScopedAccess(Protectee *p, Mutex *m, bool lockedAlready=false)
This is used to create an access object to a protected object, which is passed as pointer...
Definition: ScopedAccess.h:134
void unlock()
Definition: ScopedAccess.h:241
Mixin class that can be used to add the Protectee concept used by ScopedAccess<Protectee(, void)> to an existing class.
Definition: ScopedAccess.h:233
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Protectee * operator->()
Definition: ScopedAccess.h:84
ScopedAccess & operator=(ScopedAccess &&other) noexcept
Definition: ScopedAccess.h:207
Protectee & operator*()
Definition: ScopedAccess.h:92
const Protectee * operator->() const
Definition: ScopedAccess.h:88
Grants thread-safe access to an object (the Protectee) that should be protected from concurrent acces...
Definition: ScopedAccess.h:119
ScopedAccessBase(ScopedAccessBase &&other) noexcept
Definition: ScopedAccess.h:71
ScopedAccess(ScopedAccess &&other) noexcept
Definition: ScopedAccess.h:147
ScopedAccessBase & operator=(ScopedAccessBase &&other) noexcept
Definition: ScopedAccess.h:76
Mutex * mMutex
Definition: ScopedAccess.h:160
ScopedAccessBase(Protectee *p)
Definition: ScopedAccess.h:67
ScopedAccess & operator=(ScopedAccess &&other) noexcept
Definition: ScopedAccess.h:152
void lock()
Definition: ScopedAccess.h:237
const Protectee & operator*() const
Definition: ScopedAccess.h:96
ScopedAccess(Protectee *p, bool lockedAlready=false)
This is used to create an access object to a protected object, which is passed as pointer...
Definition: ScopedAccess.h:195
~ScopedAccess()
Definition: ScopedAccess.h:141
base class for ScopedAccess with external or Protectee-internal mutex
Definition: ScopedAccess.h:62
ScopedAccess(ScopedAccess &&other) noexcept
Definition: ScopedAccess.h:206
Protectee * mObject
Definition: ScopedAccess.h:101