summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/AtomicUniquePtr.h
blob: 5b18763d39f3fc8352d45e738ffb1b9f6cf2ccf8 (plain) (blame)
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


#pragma once


/** An RAII wrapper for std::atomic<T*>. */
template <typename T>
class cAtomicUniquePtr
{
public:
	static_assert(!std::is_array<T>::value, "cAtomicUniquePtr does not support arrays");
	DISALLOW_COPY_AND_ASSIGN(cAtomicUniquePtr);

	cAtomicUniquePtr() NOEXCEPT:
		m_Ptr(nullptr)
	{
	}


	cAtomicUniquePtr(std::unique_ptr<T> a_Ptr) NOEXCEPT:
		m_Ptr(a_Ptr.release())
	{
	}

	cAtomicUniquePtr & operator = (std::unique_ptr<T> a_Ptr) NOEXCEPT
	{
		store(std::move(a_Ptr));
		return *this;
	}

	~cAtomicUniquePtr() NOEXCEPT
	{
		delete load();
	}

	operator T * () const NOEXCEPT
	{
		return load();
	}

	bool compare_exchange_weak(T *& a_Expected, std::unique_ptr<T> && a_Desired, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT
	{
		bool DidExchange = m_Ptr.compare_exchange_weak(a_Expected, a_Desired.get(), a_Order);
		if (DidExchange)
		{
			// Only release ownership from the caller if the exchange occurred
			a_Desired.release();
		}
		return DidExchange;
	}

	bool compare_exchange_strong(T *& a_Expected, std::unique_ptr<T> && a_Desired, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT
	{
		bool DidExchange = m_Ptr.compare_exchange_strong(a_Expected, a_Desired.get(), a_Order);
		if (DidExchange)
		{
			// Only release ownership from the caller if the exchange occurred
			a_Desired.release();
		}
		return DidExchange;
	}

	std::unique_ptr<T> exchange(std::unique_ptr<T> a_Ptr, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT
	{
		return std::unique_ptr<T>{ m_Ptr.exchange(a_Ptr.release(), a_Order) };
	}

	T * load(std::memory_order a_Order = std::memory_order_seq_cst) const NOEXCEPT
	{
		return m_Ptr.load(a_Order);
	}

	void store(std::unique_ptr<T> a_Ptr, std::memory_order a_Order = std::memory_order_seq_cst) NOEXCEPT
	{
		// Store new value and delete old value
		delete m_Ptr.exchange(a_Ptr.release(), a_Order);
	}

private:
	std::atomic<T*> m_Ptr;
};