aboutsummaryrefslogtreecommitdiff
path: root/include/llvm/ADT/Optional.h
diff options
context:
space:
mode:
authorDavid Blaikie <dblaikie@gmail.com>2013-02-21 07:55:39 +0000
committerDavid Blaikie <dblaikie@gmail.com>2013-02-21 07:55:39 +0000
commitea72255f5b0e8d92d5ae9feb5e92605d123888ca (patch)
treed3e6b3e98e4b61155896e6c1d2d12380f23abdaa /include/llvm/ADT/Optional.h
parent17cec5a68523fe346fb752b1661cc8e640dd520b (diff)
Add move ctor/assignment to Optional<T>
Code review feedback for r175580 by Jordan Rose. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@175729 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT/Optional.h')
-rw-r--r--include/llvm/ADT/Optional.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/include/llvm/ADT/Optional.h b/include/llvm/ADT/Optional.h
index c5dc29946c..fd1da74bb7 100644
--- a/include/llvm/ADT/Optional.h
+++ b/include/llvm/ADT/Optional.h
@@ -46,12 +46,41 @@ public:
Optional(T &&y) : hasVal(true) {
new (storage.buffer) T(std::forward<T>(y));
}
+ Optional(Optional<T> &&O) : hasVal(O) {
+ if (O) {
+ new (storage.buffer) T(std::move(*O));
+ O.reset();
+ }
+ }
+ Optional &operator=(T &&y) {
+ if (hasVal)
+ **this = std::move(y);
+ else {
+ new (storage.buffer) T(std::move(y));
+ hasVal = true;
+ }
+ return *this;
+ }
+ Optional &operator=(Optional &&O) {
+ if (!O)
+ reset();
+ else {
+ *this = std::move(*O);
+ O.reset();
+ }
+ return *this;
+ }
#endif
static inline Optional create(const T* y) {
return y ? Optional(*y) : Optional();
}
+ // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
+ // could be made more efficient by passing by value, possibly unifying them
+ // with the rvalue versions above - but this could place a different set of
+ // requirements (notably: the existence of a default ctor) when implemented
+ // in that way. Careful SFINAE to avoid such pitfalls would be required.
Optional &operator=(const T &y) {
if (hasVal)
**this = y;