summaryrefslogtreecommitdiff
path: root/attic/LockingPtr.hpp
blob: c373129ade8776d99be06098bfc795fbfe548de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2016  ZeroTier, Inc.  https://www.zerotier.com/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef ZT_LOCKINGPTR_HPP
#define ZT_LOCKINGPTR_HPP

#include "Mutex.hpp"

namespace ZeroTier {

/**
 * A simple pointer that locks and holds a mutex until destroyed
 *
 * Care must be taken when using this. It's not very sophisticated and does
 * not handle being copied except for the simple return use case. When it is
 * copied it hands off the mutex to the copy and clears it in the original,
 * meaning that the mutex is unlocked when the last LockingPtr<> in a chain
 * of such handoffs is destroyed. If this chain of handoffs "forks" (more than
 * one copy is made) then non-determinism may ensue.
 *
 * This does not delete or do anything else with the pointer. It also does not
 * take care of locking the lock. That must be done beforehand.
 */
template<typename T>
class LockingPtr
{
public:
	LockingPtr() :
		_ptr((T *)0),
		_lock((Mutex *)0)
	{
	}

	LockingPtr(T *obj,Mutex *lock) :
		_ptr(obj),
		_lock(lock)
	{
	}

	LockingPtr(const LockingPtr &p) :
		_ptr(p._ptr),
		_lock(p._lock)
	{
		const_cast<LockingPtr *>(&p)->_lock = (Mutex *)0;
	}

	~LockingPtr()
	{
		if (_lock)
			_lock->unlock();
	}

	inline LockingPtr &operator=(const LockingPtr &p)
	{
		_ptr = p._ptr;
		_lock = p._lock;
		const_cast<LockingPtr *>(&p)->_lock = (Mutex *)0;
		return *this;
	}

	inline operator bool() const throw() { return (_ptr != (T *)0); }
	inline T &operator*() const throw() { return *_ptr; }
	inline T *operator->() const throw() { return _ptr; }

	/**
	 * @return Raw pointer to held object
	 */
	inline T *ptr() const throw() { return _ptr; }

	inline bool operator==(const LockingPtr &sp) const throw() { return (_ptr == sp._ptr); }
	inline bool operator!=(const LockingPtr &sp) const throw() { return (_ptr != sp._ptr); }
	inline bool operator>(const LockingPtr &sp) const throw() { return (_ptr > sp._ptr); }
	inline bool operator<(const LockingPtr &sp) const throw() { return (_ptr < sp._ptr); }
	inline bool operator>=(const LockingPtr &sp) const throw() { return (_ptr >= sp._ptr); }
	inline bool operator<=(const LockingPtr &sp) const throw() { return (_ptr <= sp._ptr); }

private:
	T *_ptr;
	Mutex *_lock;
};

} // namespace ZeroTier

#endif