@@ -25,13 +25,14 @@ class ThreadUnsafeOwnershipFlags {
25
25
public:
26
26
ThreadUnsafeOwnershipFlags (bool flag) : flag_(flag) {}
27
27
28
- ThreadUnsafeOwnershipFlags (const ThreadUnsafeOwnershipFlags& o ) = delete ;
29
- ThreadUnsafeOwnershipFlags& operator =(const ThreadUnsafeOwnershipFlags& o) =
30
- delete ;
31
- ThreadUnsafeOwnershipFlags (ThreadUnsafeOwnershipFlags&& o ) = default ;
28
+ ThreadUnsafeOwnershipFlags (const ThreadUnsafeOwnershipFlags& other ) = delete ;
29
+ ThreadUnsafeOwnershipFlags& operator =(
30
+ const ThreadUnsafeOwnershipFlags& other) = delete ;
31
+ ThreadUnsafeOwnershipFlags (ThreadUnsafeOwnershipFlags&& other ) = default ;
32
32
33
33
void SetOwnership (bool flag) { flag_ = flag; }
34
34
35
+ // Invoke the callback if it is not owned.
35
36
template <typename Callback>
36
37
void AcquireOwnershipOnce (Callback acquire) {
37
38
if (!flag_) {
@@ -44,48 +45,51 @@ class ThreadUnsafeOwnershipFlags {
44
45
bool flag_;
45
46
};
46
47
47
- // Copy On Write pointer.
48
+ // Copy-On- Write pointer.
48
49
// It will hold a T* pointer, and only copy once when `MutableData` is invoked.
49
50
//
50
51
// The template parameter OwnershipFlags should have:
51
52
// * a constructor takes a bool. True if own.
52
53
// * SetOwnership(bool flag).
53
54
// * AcquireOwnershipOnce(Callback). It will invoke the callback if it is not
54
55
// owned.
56
+ //
57
+ // https://en.wikipedia.org/wiki/Copy-on-write
55
58
template <typename T, typename OwnershipFlags = ThreadUnsafeOwnershipFlags>
56
59
class COWPtr {
57
60
public:
58
61
// Ctor from raw pointer.
59
62
explicit COWPtr (T* ptr) : payload_(ptr), ownership_{true } {}
60
63
61
64
// Move methods. Steal ownership from origin
62
- COWPtr (COWPtr&& o )
63
- : payload_(o .payload_), ownership_{std::move (o .ownership_ )} {}
65
+ COWPtr (COWPtr&& other )
66
+ : payload_(other .payload_), ownership_{std::move (other .ownership_ )} {}
64
67
COWPtr& operator =(COWPtr&& origin) = default ;
65
68
66
69
// Copy methods. Not own payload
67
- COWPtr (const COWPtr& o ) : payload_(o .payload_), ownership_{false } {}
68
- COWPtr& operator =(const COWPtr& o ) {
69
- payload_ = o .payload_ ;
70
+ COWPtr (const COWPtr& other ) : payload_(other .payload_), ownership_{false } {}
71
+ COWPtr& operator =(const COWPtr& other ) {
72
+ payload_ = other .payload_ ;
70
73
ownership_.SetOwnership (false );
71
74
return *this ;
72
75
}
73
76
77
+ // Access read only data.
74
78
const T& Data () const { return *payload_; }
75
79
80
+ // Access mutable data. If the data is not owned, the data will be copied
81
+ // before.
76
82
T* MutableData () {
77
83
ownership_.AcquireOwnershipOnce (
78
84
[this ] { payload_.reset (new T (*payload_)); });
79
85
return payload_.get ();
80
86
}
81
87
82
- void Reset () {
83
- ownership_.AcquireOwnershipOnce ([this ] { payload_.reset (); });
84
- payload_.reset (new T ());
85
- }
86
-
87
88
private:
89
+ // Actual data pointer.
88
90
std::shared_ptr<T> payload_;
91
+
92
+ // Ownership flag.
89
93
OwnershipFlags ownership_;
90
94
};
91
95
0 commit comments