From 49e8a3c652c6954b1066b6974479e3412915956e Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Tue, 27 Jan 2015 10:31:08 +0100 Subject: [PATCH] BReferencable: implement const references with specialization This is simpler and cleaner than my previous attempt. Thanks to Ingo for suggesting this. --- headers/os/support/Referenceable.h | 116 ++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 25 deletions(-) diff --git a/headers/os/support/Referenceable.h b/headers/os/support/Referenceable.h index a9af8c691c..f95ee81c72 100644 --- a/headers/os/support/Referenceable.h +++ b/headers/os/support/Referenceable.h @@ -37,7 +37,7 @@ protected: // #pragma mark - BReference -template +template class BReference { public: BReference() @@ -92,34 +92,34 @@ public: } } - ConstType* Get() const + Type* Get() const { return fObject; } - ConstType* Detach() + Type* Detach() { Type* object = fObject; fObject = NULL; return object; } - ConstType& operator*() const + Type& operator*() const { return *fObject; } - ConstType* operator->() const + Type* operator->() const { return fObject; } - operator ConstType*() const + operator Type*() const { return fObject; } - BReference& operator=(const BReference& other) + BReference& operator=(const BReference& other) { SetTo(other.fObject); return *this; @@ -131,14 +131,14 @@ public: return *this; } - template - BReference& operator=(const BReference& other) + template + BReference& operator=(const BReference& other) { SetTo(other.Get()); return *this; } - bool operator==(const BReference& other) const + bool operator==(const BReference& other) const { return fObject == other.fObject; } @@ -148,7 +148,7 @@ public: return fObject == other; } - bool operator!=(const BReference& other) const + bool operator!=(const BReference& other) const { return fObject != other.fObject; } @@ -163,38 +163,104 @@ private: }; -// #pragma mark - BReference +// #pragma mark - BReference -template -class BConstReference: public BReference { +template +class BReference { public: - BConstReference() + BReference(Type* object, bool alreadyHasReference = false) : - BReference() + fReference(object, alreadyHasReference) { } - BConstReference(Type* object, bool alreadyHasReference = false) + BReference(const BReference& other) : - BReference(object, alreadyHasReference) + fReference(other) { } - BConstReference(const BReference& other) + template + BReference(const BReference& other) : - BReference(other) + fReference(other.Get()) { } - // Allow assignment of a const reference from a mutable one (but not the - // reverse). - BConstReference& operator=(const BReference& other) + void SetTo(Type* object, bool alreadyHasReference = false) { - SetTo(other.Get()); - return *this; + fReference.SetTo(object, alreadyHasReference); } + void Unset() + { + fReference.Unset(); + } + + const Type* Get() const + { + return fReference.Get(); + } + + const Type* Detach() + { + return fReference.Detach(); + } + + const Type& operator*() const + { + return *fReference; + } + + const Type* operator->() const + { + return fReference.Get(); + } + + operator const Type*() const + { + return fReference.Get(); + } + + BReference& operator=(const BReference& other) + { + fReference = other.fReference; + } + + BReference& operator=(Type* other) + { + fReference = other; + } + + template + BReference& operator=(const BReference& other) + { + fReference = other.Get(); + } + + bool operator==(const BReference& other) const + { + return fReference == other.Get(); + } + + bool operator==(const Type* other) const + { + return fReference == other; + } + + bool operator!=(const BReference& other) const + { + return fReference != other.Get(); + } + + bool operator!=(const Type* other) const + { + return fReference != other; + } + +private: + BReference fReference; };