diff --git a/mfbt/Scoped.h b/mfbt/Scoped.h index a7f9e6e8e61f..ff078f4282d0 100644 --- a/mfbt/Scoped.h +++ b/mfbt/Scoped.h @@ -52,8 +52,10 @@ * the scope, graphics contexts, etc. */ +#include "mozilla/Assertions.h" #include "mozilla/Attributes.h" #include "mozilla/GuardObjects.h" +#include "mozilla/Move.h" #include "mozilla/NullPtr.h" namespace mozilla { @@ -83,12 +85,23 @@ class Scoped { MOZ_GUARD_OBJECT_NOTIFIER_INIT; } + explicit Scoped(const Resource& v MOZ_GUARD_OBJECT_NOTIFIER_PARAM) : value(v) { MOZ_GUARD_OBJECT_NOTIFIER_INIT; } + + /* Move constructor. */ + explicit Scoped(Scoped&& v + MOZ_GUARD_OBJECT_NOTIFIER_PARAM) + : value(Move(v.value)) + { + MOZ_GUARD_OBJECT_NOTIFIER_INIT; + v.value = Traits::empty(); + } + ~Scoped() { Traits::release(value); } @@ -146,6 +159,14 @@ class Scoped return *this; } + /* Move assignment operator. */ + Scoped& operator=(Scoped&& rhs) { + MOZ_ASSERT(&rhs != this, "self-move-assignment not allowed"); + this->~Scoped(); + new(this) Scoped(Move(rhs)); + return *this; + } + private: explicit Scoped(const Scoped& value) MOZ_DELETE; Scoped& operator=(const Scoped& value) MOZ_DELETE; @@ -169,20 +190,30 @@ struct name : public mozilla::Scoped > \ { \ typedef mozilla::Scoped > Super; \ typedef typename Super::Resource Resource; \ - name& operator=(Resource ptr) { \ - Super::operator=(ptr); \ + name& operator=(Resource rhs) { \ + Super::operator=(rhs); \ + return *this; \ + } \ + name& operator=(name&& rhs) { \ + Super::operator=(Move(rhs)); \ return *this; \ } \ explicit name(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM) \ : Super(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT) \ {} \ - explicit name(Resource ptr \ + explicit name(Resource rhs \ MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \ - : Super(ptr MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) \ + : Super(rhs \ + MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) \ + {} \ + explicit name(name&& rhs \ + MOZ_GUARD_OBJECT_NOTIFIER_PARAM) \ + : Super(Move(rhs) \ + MOZ_GUARD_OBJECT_NOTIFIER_PARAM_TO_PARENT) \ {} \ private: \ - explicit name(name& source) MOZ_DELETE; \ - name& operator=(name& source) MOZ_DELETE; \ + explicit name(name&) MOZ_DELETE; \ + name& operator=(name&) MOZ_DELETE; \ }; /*