From 1cd7d08d7cae1779924e240202d4cce354d7e12b Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Tue, 14 Feb 2006 11:54:46 +0000 Subject: [PATCH] rename SmartPtr-ByRef and ScopeGuard-ByRefHolder into RefToValue and move it to loki/RefToValue.h git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@530 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/RefToValue.h | 70 +++++++++++++++++++++++++++++++++++++++ include/loki/ScopeGuard.h | 27 ++------------- include/loki/SmartPtr.h | 31 +++++------------ 3 files changed, 81 insertions(+), 47 deletions(-) create mode 100755 include/loki/RefToValue.h diff --git a/include/loki/RefToValue.h b/include/loki/RefToValue.h new file mode 100755 index 0000000..9fc6109 --- /dev/null +++ b/include/loki/RefToValue.h @@ -0,0 +1,70 @@ +//////////////////////////////////////////////////////////////////////////////// +// The Loki Library +// Copyright (c) 2006 Richard Sposato +// Copyright (c) 2006 Peter Kümmel +// Permission to use, copy, modify, distribute and sell this software for any +// purpose is hereby granted without fee, provided that the above copyright +// notice appear in all copies and that both that copyright notice and this +// permission notice appear in supporting documentation. +// The authors make no representations about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. +//////////////////////////////////////////////////////////////////////////////// +#ifndef LOKI_REFTOVALUE_H +#define LOKI_REFTOVALUE_H + +namespace Loki +{ + + //////////////////////////////////////////////////////////////////////////////// + /// \class RefToValue + /// + /// \ingroup SmartPointerGroup + /// Transports a reference as a value + /// Serves to implement the Colvin/Gibbons trick for SmartPtr/ScopeGuard + //////////////////////////////////////////////////////////////////////////////// + + template + class RefToValue + { + public: + + RefToValue(T& ref) : ref_(ref) + {} + + RefToValue(const RefToValue& rhs) : ref_(rhs.ref_) + {} + + operator T& () const + { + return ref_; + } + + private: + // Disable - not implemented + RefToValue(); + RefToValue& operator=(const RefToValue&); + + T& ref_; + }; + + + //////////////////////////////////////////////////////////////////////////////// + /// \function ByRef + /// + /// \ingroup SmartPointerGroup + /// RefToValue creator. + //////////////////////////////////////////////////////////////////////////////// + + template + inline RefToValue ByRef(T& t) + { + return RefToValue(t); + } + +} + + + +#endif //LOKI_REFTOVALUE_H + diff --git a/include/loki/ScopeGuard.h b/include/loki/ScopeGuard.h index e45f7c4..b5ef1c2 100755 --- a/include/loki/ScopeGuard.h +++ b/include/loki/ScopeGuard.h @@ -22,30 +22,6 @@ namespace Loki { - template - class RefHolder - { - T& ref_; - public: - RefHolder(T& ref) : ref_(ref) - {} - - operator T& () const - { - return ref_; - } - - private: - // Disable assignment - not implemented - RefHolder& operator=(const RefHolder&); - }; - - template - inline RefHolder ByRef(T& t) - { - return RefHolder(t); - } - class ScopeGuardImplBase { ScopeGuardImplBase& operator =(const ScopeGuardImplBase&); @@ -385,6 +361,9 @@ namespace Loki #endif //LOKI_SCOPEGUARD_H_ // $Log$ +// Revision 1.5 2006/02/14 11:54:46 syntheticpp +// rename SmartPtr-ByRef and ScopeGuard-ByRefHolder into RefToValue and move it to loki/RefToValue.h +// // Revision 1.4 2006/01/16 19:05:09 rich_sposato // Added cvs keywords. // diff --git a/include/loki/SmartPtr.h b/include/loki/SmartPtr.h index f5cc38e..3fb2aa9 100644 --- a/include/loki/SmartPtr.h +++ b/include/loki/SmartPtr.h @@ -34,6 +34,8 @@ #include "SmallObj.h" #include "TypeManip.h" #include "static_check.h" +#include "RefToValue.h" + #include #include #include @@ -820,26 +822,6 @@ namespace Loki {} }; -//////////////////////////////////////////////////////////////////////////////// -/// \class ByRef -/// -/// \ingroup SmartPointerGroup -/// Transports a reference as a value -/// Serves to implement the Colvin/Gibbons trick for SmartPtr -//////////////////////////////////////////////////////////////////////////////// - - template - class ByRef - { - public: - ByRef(T& v) : value_(v) {} - operator T&() { return value_; } - // gcc doesn't like this: - // operator const T&() const { return value_; } - private: - ByRef& operator=(const ByRef &); - T& value_; - }; //////////////////////////////////////////////////////////////////////////////// /// \class DontPropagateConst @@ -1017,12 +999,12 @@ namespace Loki : SP(rhs), OP(rhs), KP(rhs), CP(rhs) { GetImplRef(*this) = OP::Clone(GetImplRef(rhs)); } - SmartPtr(ByRef rhs) + SmartPtr(RefToValue rhs) : SP(rhs), OP(rhs), KP(rhs), CP(rhs) {} - operator ByRef() - { return ByRef(*this); } + operator RefToValue() + { return RefToValue(*this); } SmartPtr& operator=(CopyArg& rhs) { @@ -1449,6 +1431,9 @@ namespace std #endif // SMARTPTR_INC_ // $Log$ +// Revision 1.16 2006/02/14 11:54:46 syntheticpp +// rename SmartPtr-ByRef and ScopeGuard-ByRefHolder into RefToValue and move it to loki/RefToValue.h +// // Revision 1.15 2006/02/08 18:12:29 rich_sposato // Fixed bug 1425890. Last SmartPtr in linked chain NULLs its prev & next // pointers to prevent infinite recursion. Added asserts.