/
AutoCheckout.h
81 lines (69 loc) · 1.97 KB
/
AutoCheckout.h
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
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#pragma once
#include "auto_id.h"
#include <typeinfo>
#include MEMORY_HEADER
class AutoPacket;
template<class T>
class AutoCheckout {
public:
typedef void (AutoPacket::*t_completion)(bool, const std::type_info&);
AutoCheckout(void) :
m_parent(nullptr),
m_val(nullptr),
m_ready(false),
completion(nullptr)
{}
AutoCheckout(AutoPacket& parent, const std::shared_ptr<T>& val, t_completion completion) :
m_parent(&parent),
m_val(val),
m_ready(false),
completion(completion)
{}
AutoCheckout(AutoCheckout&& rhs) :
m_parent(rhs.m_parent),
m_val(rhs.m_val),
m_ready(rhs.m_ready),
completion(rhs.completion)
{
rhs.m_parent = nullptr;
rhs.m_val.reset();
rhs.m_ready = false;
}
~AutoCheckout(void) {
if(m_val)
(m_parent->*completion)(m_ready, typeid(auto_id<T>));
}
private:
// NOTE: m_parent cannot be a shared_ptr since it may be created during the resolution
// of optional_ptr arguments, in which case the shared_ptr to m_parent will be expired.
// Using a regular pointer enables decoration during the final call.
AutoPacket* m_parent;
std::shared_ptr<T> m_val;
mutable bool m_ready;
t_completion completion;
public:
/// <summary>
/// Causes the wrapped packet to be committed when the checkout is destroyed
/// </summary>
void Ready() const {
m_ready = true;
}
// Operator overloads:
operator T*(void) const { return m_val.get(); }
T& operator*(void) const { return *m_val; }
T* operator->(void) const { return m_val.get(); }
operator bool(void) const { return !!m_val; }
operator T&(void) const { return *m_val; }
// <summary>Assignment by move</summary>
AutoCheckout& operator=(AutoCheckout&& rhs) {
m_parent = rhs.m_parent;
m_val = rhs.m_val;
m_ready = rhs.m_ready;
completion = rhs.completion;
rhs.m_parent = nullptr;
rhs.m_val.reset();
rhs.m_ready = false;
return *this;
}
};