|
From: <ag...@us...> - 2011-01-12 12:01:32
|
Revision: 1875
http://zoolib.svn.sourceforge.net/zoolib/?rev=1875&view=rev
Author: agreen
Date: 2011-01-12 12:01:25 +0000 (Wed, 12 Jan 2011)
Log Message:
-----------
Remove ZRefDynamicCast.
Change Adopt() to be a singleton that has an operator(), so we
can also provide an operator&, which lets one write Adopt& XXX
rather than Adopt(XXX), which means one less parenthesis to balance.
Do similar thing for MakeRef.
Add TempRef, which is useful with ObjC -- don't have to choose
between autoreleasing something and putting it in a local var
to be manually released.
Modified Paths:
--------------
trunk/zoolib/source/cxx/zoolib/ZRef.h
trunk/zoolib/source/cxx/zoolib/ZTypes.h
Modified: trunk/zoolib/source/cxx/zoolib/ZRef.h
===================================================================
--- trunk/zoolib/source/cxx/zoolib/ZRef.h 2011-01-10 19:32:08 UTC (rev 1874)
+++ trunk/zoolib/source/cxx/zoolib/ZRef.h 2011-01-12 12:01:25 UTC (rev 1875)
@@ -28,10 +28,6 @@
#include "zoolib/ZDebug.h"
#include "zoolib/ZTypes.h" // For Adopt_T
-#ifdef __OBJC__
- struct objc_object;
-#endif
-
namespace ZooLib {
// =================================================================================================
@@ -55,9 +51,7 @@
}
static void spCheck(T* iP)
- {
- ZAssert(iP);
- }
+ { ZAssertStop(1, iP); }
public:
#ifdef __OBJC__
@@ -92,10 +86,10 @@
ZRef& operator=(const ZRef& iOther)
{
- T* theP = iOther.Get();
- std::swap(theP, fP);
+ T* otherP = iOther.Get();
+ std::swap(otherP, fP);
spRetain(fP);
- spRelease(theP);
+ spRelease(otherP);
return *this;
}
@@ -123,41 +117,33 @@
template <class O, bool OtherSense>
ZRef& operator=(const ZRef<O, OtherSense>& iOther)
{
- T* theP = iOther.Get();
- std::swap(theP, fP);
+ T* otherP = iOther.Get();
+ std::swap(otherP, fP);
spRetain(fP);
- spRelease(theP);
+ spRelease(otherP);
return *this;
}
- ZRef(const Adopt_T<T>& iAdopt)
+ template <class O>
+ ZRef(const Adopt_T<O>& iAdopt)
: fP(iAdopt.Get())
{}
- ZRef& operator=(const Adopt_T<T>& iAdopt)
+ template <class O>
+ ZRef& operator=(const Adopt_T<O>& iAdopt)
{
- T* theP = iAdopt.Get();
- std::swap(theP, fP);
- spRelease(theP);
+ T* otherP = iAdopt.Get();
+ std::swap(otherP, fP);
+ spRelease(otherP);
return *this;
}
- ZRef(const Adopt_T<T*>& iAdopt)
- : fP(iAdopt.Get())
- {}
-
- ZRef& operator=(const Adopt_T<T*>& iAdopt)
- {
- T* theP = iAdopt.Get();
- std::swap(theP, fP);
- spRelease(theP);
- return *this;
- }
-
- bool operator==(const T* iP) const
+ template <class O>
+ bool operator==(O* iP) const
{ return fP == iP; }
- bool operator!=(const T* iP) const
+ template <class O>
+ bool operator!=(O* iP) const
{ return fP != iP; }
template <class O, bool OtherSense>
@@ -178,33 +164,35 @@
return fP;
}
+ T* Get() const
+ { return fP; }
+
T* Copy() const
{
spRetain(fP);
return fP;
}
- T* Get() const
- { return fP; }
+ T* Orphan()
+ {
+ T* otherP = nullptr;
+ std::swap(otherP, fP);
+ return otherP;
+ }
void Clear()
{
- T* theP = nullptr;
- std::swap(theP, fP);
- spRelease(theP);
+ T* otherP = nullptr;
+ std::swap(otherP, fP);
+ spRelease(otherP);
}
- T* Orphan()
+ T*& OParam()
{
- T* theP = nullptr;
- std::swap(theP, fP);
- return theP;
+ this->Clear();
+ return fP;
}
- // Used with COM output parameters. See sCOMPtr and sCOMVoidPtr in ZWinCOM.h
- T*& GetPtrRef()
- { return fP; }
-
template <class O>
O* DynamicCast() const
{ return dynamic_cast<O*>(fP); }
@@ -241,8 +229,8 @@
#pragma mark -
#pragma mark * ZRef partially specialized for pointer types
-template <class T> void sRetain_T(T& ioPtr);
-template <class T> void sRelease_T(T iPtr);
+template <class T> void sRetain_T(T*& ioPtr);
+template <class T> void sRelease_T(T* iPtr);
template <class T>
class ZRef<T*, true>
@@ -277,10 +265,10 @@
ZRef& operator=(const ZRef& iOther)
{
- T* theP = iOther.Get();
- std::swap(theP, fP);
+ T* otherP = iOther.Get();
+ std::swap(otherP, fP);
spRetain(fP);
- spRelease(theP);
+ spRelease(otherP);
return *this;
}
@@ -301,48 +289,52 @@
}
template <class O>
- ZRef(const ZRef<O>& iOther)
+ ZRef(const ZRef<O*>& iOther)
: fP(iOther.Get())
{ spRetain(fP); }
template <class O>
- ZRef& operator=(const ZRef<O>& iOther)
+ ZRef& operator=(const ZRef<O*>& iOther)
{
- T* theP = iOther.Get();
- std::swap(theP, fP);
+ T* otherP = iOther.Get();
+ std::swap(otherP, fP);
spRetain(fP);
- spRelease(theP);
+ spRelease(otherP);
return *this;
}
- ZRef(const Adopt_T<T*>& iAdopt)
+ template <class O>
+ ZRef(const Adopt_T<O*>& iAdopt)
: fP(iAdopt.Get())
{}
- ZRef& operator=(const Adopt_T<T*>& iAdopt)
+ template <class O>
+ ZRef& operator=(const Adopt_T<O*>& iAdopt)
{
- T* theP = iAdopt.Get();
- std::swap(theP, fP);
- spRelease(theP);
+ T* otherP = iAdopt.Get();
+ std::swap(otherP, fP);
+ spRelease(otherP);
return *this;
}
- bool operator==(const T* iP) const
+ template <class O>
+ bool operator==(O* iP) const
{ return fP == iP; }
- bool operator!=(const T* iP) const
+ template <class O>
+ bool operator!=(O* iP) const
{ return fP != iP; }
template <class O>
- bool operator==(const ZRef<O>& iOther) const
+ bool operator==(const ZRef<O*>& iOther) const
{ return fP == iOther.Get(); }
template <class O>
- bool operator!=(const ZRef<O>& iOther) const
+ bool operator!=(const ZRef<O*>& iOther) const
{ return fP != iOther.Get(); }
template <class O>
- bool operator<(const ZRef<O>& iOther) const
+ bool operator<(const ZRef<O*>& iOther) const
{ return fP < iOther.Get(); }
operator T*()
@@ -351,30 +343,33 @@
operator T*() const
{ return fP; }
- T* Get() const { return fP; }
+ T* Get() const
+ { return fP; }
- void Clear()
+ T* Copy() const
{
- T* theP = nullptr;
- std::swap(theP, fP);
- spRelease(theP);
+ spRetain(fP);
+ return fP;
}
T* Orphan()
{
- T* theP = nullptr;
- std::swap(theP, fP);
- return theP;
+ T* otherP = nullptr;
+ std::swap(otherP, fP);
+ return otherP;
}
- // Used with output parameters.
- T*& GetPtrRef()
- { return fP; }
+ void Clear()
+ {
+ T* otherP = nullptr;
+ std::swap(otherP, fP);
+ spRelease(otherP);
+ }
T*& OParam()
{
this->Clear();
- return this->GetPtrRef();
+ return fP;
}
template <class O>
@@ -389,26 +384,25 @@
#pragma mark -
#pragma mark * MakeRef
-template <class T>
-ZRef<T> MakeRef(T* iP)
- { return ZRef<T>(iP); }
+const struct MakeRef_t
+ {
+ template <class T>
+ ZRef<T> operator()(T* iT) const { return ZRef<T>(iT); }
+ } MakeRef = {};
+const struct TempRef_t
+ {
+ template <class T>
+ ZRef<T> operator&(T* iT) const { return ZRef<T>(Adopt_T<T>(iT)); }
+
+ template <class T>
+ ZRef<T> operator()(T* iT) const { return ZRef<T>(Adopt_T<T>(iT)); }
+ } TempRef = {};
+
// =================================================================================================
#pragma mark -
-#pragma mark * ZRef casts
+#pragma mark *
-template <class O, class T> O* ZRefDynamicCast(const ZRef<T>& iVal)
- { return dynamic_cast<O*>(iVal.Get()); }
-
-template <class O, class T> O* ZRefStaticCast(const ZRef<T>& iVal)
- { return static_cast<O*>(iVal.Get()); }
-
-template <class O, class T> O ZRefStaticCast(const ZRef<T*>& iVal)
- { return static_cast<O>(iVal.Get()); }
-
-template <class O, class T> O ZRefStaticCast(const ZRef<const T*>& iVal)
- { return static_cast<O>(iVal.Get()); }
-
template <class T>
void swap(ZRef<T>& a, ZRef<T>& b)
{ a.swap(b); }
Modified: trunk/zoolib/source/cxx/zoolib/ZTypes.h
===================================================================
--- trunk/zoolib/source/cxx/zoolib/ZTypes.h 2011-01-10 19:32:08 UTC (rev 1874)
+++ trunk/zoolib/source/cxx/zoolib/ZTypes.h 2011-01-12 12:01:25 UTC (rev 1875)
@@ -126,10 +126,17 @@
T* fP;
};
-template <class P>
-Adopt_T<P> Adopt(P iP)
- { return Adopt_T<P>(iP); }
+// Adopt functor.
+// operator& lets you use Adopt& as a pseudo prefix operator.
+const struct Adopt_t
+ {
+ template <class T>
+ Adopt_T<T> operator()(T iT) const { return Adopt_T<T>(iT); }
+ template <class T>
+ Adopt_T<T> operator&(T iT) const { return Adopt_T<T>(iT); }
+ } Adopt = {};
+
// =================================================================================================
typedef void* VoidStar_t;
@@ -144,7 +151,7 @@
// =================================================================================================
-const bool I_KNOW_WHAT_IM_DOING = true;
+const class IKnowWhatIAmDoing_t {} IKnowWhatIAmDoing = {};
} // namespace ZooLib
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|