forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy_mutable_copy.h
117 lines (103 loc) · 4.47 KB
/
lazy_mutable_copy.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OR_TOOLS_UTIL_LAZY_MUTABLE_COPY_H_
#define OR_TOOLS_UTIL_LAZY_MUTABLE_COPY_H_
#include <memory>
namespace operations_research {
// LazyMutableCopy<T> is a helper class for making an on-demand copy of an
// object of arbitrary type T. Type T must have a copy constructor.
//
// Sample usage:
// const Proto& original_input = ...;
// LazyMutableCopy<Proto> input(original_input);
// if (input.get().foo() == BAD_VALUE) {
// input.get_mutable()->set_foo(GOOD_VALUE); // Copies the object.
// }
// // Process "input" here without worrying about BAD_VALUE.
// A good pattern is to have function taking LazyMutableCopy<> as argument:
// void ProcessProto(LazyMutableCopy<Proto> input) { // pass by copy
// ...
// }
// At the call site: ProcessProto(const_ref_to_my_proto);
//
// In basic usage, a LazyMutableCopy is in one of two states:
// - original: points to the const original. No memory allocated.
// - copy: points to a mutable copy of the original and owns it. Owning the
// copy means that the destructor will delete it, like std::unique_ptr<>.
// This is what you get by calling get_mutable() or constructing it with
// a move.
template <class T>
class LazyMutableCopy {
public:
// You can construct a LazyMutableCopy with a const reference to an object,
// which must outlive this class (unless get_mutable() was called).
LazyMutableCopy(const T& obj) // NOLINT(google-explicit-constructor)
: ptr_(&obj) {}
// The other option is to construct a LazyMutableCopy with a std::move(T).
// In this case you transfer ownership and you can mutate it for free.
LazyMutableCopy(T&& obj) // NOLINT(google-explicit-constructor)
: copy_(std::make_unique<T>(std::move(obj))), ptr_(copy_.get()) {}
// You can move a LazyMutableCopy but not copy it, much like a
// std::unique_ptr<>.
LazyMutableCopy(LazyMutableCopy&&) = default;
LazyMutableCopy(const LazyMutableCopy&) = delete;
class LazyMutableCopy<T>& operator=(LazyMutableCopy<T>&&) = default;
class LazyMutableCopy<T>& operator=(const LazyMutableCopy<T>&) = delete;
// This will copy the object if we don't already have ownership.
T* get_mutable() {
if (copy_ == nullptr && ptr_ != nullptr) {
copy_ = std::make_unique<T>(*ptr_);
ptr_ = copy_.get();
}
return copy_.get();
}
// Lazily make a copy if not already done and transfer ownership from this
// class to the returned std::unique_ptr<T>. Calling this function leaves the
// class in a state where the only valid operations is to assign it a new
// value.
//
// We force a call via
// std::move(lazy_mutable_copy).copy_or_move_as_unique_ptr() to make it
// clearer that lazy_mutable_copy shouldn't really be used after this.
std::unique_ptr<T> copy_or_move_as_unique_ptr() && {
if (copy_ == nullptr && ptr_ != nullptr) {
std::unique_ptr<T> result = std::make_unique<T>(*ptr_);
ptr_ = nullptr;
return result;
}
ptr_ = nullptr;
return std::move(copy_);
}
// True iff get_mutable() was called at least once (in which case the object
// was copied) or if we constructed this via std::move().
bool has_ownership() const { return copy_ != nullptr; }
// Standard smart pointer accessor, but only for const purpose.
// Undefined if the class contains no object.
const T* get() const { return ptr_; }
const T& operator*() const { return *ptr_; }
const T* operator->() const { return ptr_; }
// Destroys any owned value. Calling this function leaves the class in a state
// where the only valid operations is to assign it a new value.
//
// We force a call via std::move(lazy_mutable_copy).dispose() to make it
// clearer that lazy_mutable_copy shouldn't really be used after this.
void dispose() && {
ptr_ = nullptr;
copy_ = nullptr;
}
private:
std::unique_ptr<T> copy_;
const T* ptr_ = nullptr;
};
} // namespace operations_research
#endif // OR_TOOLS_UTIL_LAZY_MUTABLE_COPY_H_