Honeycomb  0.1
Component-Model Framework
Transfer.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 
24 template<class FromLock, class ToLock>
26 {
27 public:
28  typedef typename ToLock::Lockable Lockable;
29 
31  TransferLock(FromLock& lock) : _lock(&lock) { this->lock(); }
32 
33  ~TransferLock() { if (owns()) unlock(); }
34 
35  void lock()
36  {
37  assert(_lock, "Lock has been released");
38  assert(!owns(), "Lock already held");
39  _toLock = move(*_lock);
40  }
41 
42  void unlock()
43  {
44  assert(_lock, "Lock has been released");
45  assert(owns(), "Lock not held");
46  *_lock = move(_toLock);
47  }
48 
49  bool owns() const { return _lock && _toLock.owns(); }
50  explicit operator bool() const { return owns(); }
51 
52  Lockable& mutex()
53  {
54  assert(_lock, "Lock has been released");
55  return _toLock.mutex();
56  }
57 
58  Lockable& release()
59  {
60  Lockable& ret = mutex();
61  _lock = nullptr;
62  return ret;
63  }
64 
65 private:
66  FromLock* _lock;
67  ToLock _toLock;
68 };
69 
70 }
bool owns() const
Definition: Transfer.h:49
Scoped transfer of mutex ownership between two locks.
Definition: Transfer.h:25
Inherit to declare that class is not copyable.
Definition: Meta.h:286
TransferLock(FromLock &lock)
Transfer ownership of mutex from lock to an instance of ToLock.
Definition: Transfer.h:31
Lockable & release()
Definition: Transfer.h:58
~TransferLock()
Definition: Transfer.h:33
ToLock::Lockable Lockable
Definition: Transfer.h:28
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Lockable & mutex()
Definition: Transfer.h:52
void unlock()
Definition: Transfer.h:42
void lock()
Definition: Transfer.h:35
Global Honeycomb namespace.