MRPT  2.0.1
lock_helper.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <type_traits>
13 
14 namespace mrpt
15 {
16 /** Auxiliary helper structure for mrpt::lockHelper()
17  * \ingroup mrpt_core_grp
18  */
19 template <class T>
21 {
22  using Tnc = std::remove_const_t<T>;
23 
24  public:
25  LockHelper(const Tnc* l) : l_{const_cast<Tnc*>(l)} { l_->lock(); }
27  {
28  if (l_) l_->unlock();
29  }
30 
31  LockHelper(const LockHelper& o) = delete;
32  LockHelper& operator=(const LockHelper& o) = delete;
33 
34  LockHelper(LockHelper&& o) : l_{o.l} { o.l = nullptr; }
36  {
37  l_ = o.l;
38  o.l = nullptr;
39  return *this;
40  }
41 
42  private:
43  Tnc* l_{nullptr};
44 };
45 
46 /** Syntactic sugar to easily create a locker to any kind of std::mutex
47  * \ingroup mrpt_core_grp
48  */
49 template <class T>
51 {
52  return LockHelper<T>(&t);
53 }
54 } // namespace mrpt
LockHelper & operator=(LockHelper &&o)
Definition: lock_helper.h:35
LockHelper(LockHelper &&o)
Definition: lock_helper.h:34
Auxiliary helper structure for mrpt::lockHelper()
Definition: lock_helper.h:20
LockHelper< T > lockHelper(T &t)
Syntactic sugar to easily create a locker to any kind of std::mutex.
Definition: lock_helper.h:50
LockHelper(const Tnc *l)
Definition: lock_helper.h:25
std::remove_const_t< T > Tnc
Definition: lock_helper.h:22
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
LockHelper & operator=(const LockHelper &o)=delete



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020