|
From: <ag...@us...> - 2012-02-01 00:07:10
|
Revision: 2703
http://zoolib.svn.sourceforge.net/zoolib/?rev=2703&view=rev
Author: agreen
Date: 2012-02-01 00:07:03 +0000 (Wed, 01 Feb 2012)
Log Message:
-----------
Bit of a rework to make comments and explanation more intelligible.
Modified Paths:
--------------
trunk/zoolib/source/cxx/zoolib/ZAny.cpp
trunk/zoolib/source/cxx/zoolib/ZAny.h
Modified: trunk/zoolib/source/cxx/zoolib/ZAny.cpp
===================================================================
--- trunk/zoolib/source/cxx/zoolib/ZAny.cpp 2012-01-30 12:17:11 UTC (rev 2702)
+++ trunk/zoolib/source/cxx/zoolib/ZAny.cpp 2012-02-01 00:07:03 UTC (rev 2703)
@@ -28,29 +28,29 @@
inline static const std::type_info* spPODTypeInfo(const void* iPtr)
{ return (const std::type_info*)(((intptr_t)iPtr) ^ 1); }
-inline ZAny::Holder_InPlace& ZAny::fHolder_InPlace()
- { return *sFetch_T<Holder_InPlace>(fBytes_InPlace); }
+inline ZAny::Holder_InPlace& ZAny::pAsInPlace()
+ { return *sFetch_T<Holder_InPlace>(&fDistinguisher); }
-inline const ZAny::Holder_InPlace& ZAny::fHolder_InPlace() const
- { return *sFetch_T<Holder_InPlace>(fBytes_InPlace); }
+inline const ZAny::Holder_InPlace& ZAny::pAsInPlace() const
+ { return *sFetch_T<Holder_InPlace>(&fDistinguisher); }
-inline ZRef<ZAny::Holder_Counted>& ZAny::fHolder_Ref()
- { return *sFetch_T<ZRef<Holder_Counted> >(fBytes_Payload); }
+inline ZRef<ZAny::Holder_Counted>& ZAny::pAsCounted()
+ { return *sFetch_T<ZRef<Holder_Counted> >(&fPayload); }
-inline const ZRef<ZAny::Holder_Counted>& ZAny::fHolder_Ref() const
- { return *sFetch_T<ZRef<Holder_Counted> >(fBytes_Payload); }
+inline const ZRef<ZAny::Holder_Counted>& ZAny::pAsCounted() const
+ { return *sFetch_T<ZRef<Holder_Counted> >(&fPayload); }
const std::type_info& ZAny::Type() const
{
- if (spIsPOD(fPtr_InPlace))
+ if (spIsPOD(fDistinguisher))
{
- return *spPODTypeInfo(fPtr_InPlace);
+ return *spPODTypeInfo(fDistinguisher);
}
- else if (fPtr_InPlace)
+ else if (fDistinguisher)
{
- return fHolder_InPlace().Type();
+ return pAsInPlace().Type();
}
- else if (const ZRef<Holder_Counted>& theHolderRef = fHolder_Ref())
+ else if (const ZRef<Holder_Counted>& theHolderRef = pAsCounted())
{
return theHolderRef->Type();
}
@@ -62,15 +62,15 @@
void* ZAny::VoidStar()
{
- if (spIsPOD(fPtr_InPlace))
+ if (spIsPOD(fDistinguisher))
{
- return fBytes_Payload;
+ return &fPayload;
}
- else if (fPtr_InPlace)
+ else if (fDistinguisher)
{
- return fHolder_InPlace().VoidStar();
+ return pAsInPlace().VoidStar();
}
- else if (ZRef<Holder_Counted>& theHolderRef = fHolder_Ref())
+ else if (ZRef<Holder_Counted>& theHolderRef = pAsCounted())
{
if (theHolderRef->IsShared())
theHolderRef = theHolderRef->Clone();
@@ -84,15 +84,15 @@
const void* ZAny::ConstVoidStar() const
{
- if (spIsPOD(fPtr_InPlace))
+ if (spIsPOD(fDistinguisher))
{
- return fBytes_Payload;
+ return &fPayload;
}
- else if (fPtr_InPlace)
+ else if (fDistinguisher)
{
- return fHolder_InPlace().ConstVoidStar();
+ return pAsInPlace().ConstVoidStar();
}
- else if (const ZRef<Holder_Counted>& theHolderRef = fHolder_Ref())
+ else if (const ZRef<Holder_Counted>& theHolderRef = pAsCounted())
{
return theHolderRef->VoidStar();
}
@@ -104,7 +104,7 @@
void ZAny::swap(ZAny& ioOther)
{
- if (fPtr_InPlace || ioOther.fPtr_InPlace)
+ if (fDistinguisher || ioOther.fDistinguisher)
{
// Trivial implementation for now
const ZAny temp = *this;
@@ -113,43 +113,43 @@
}
else
{
- fHolder_Ref().swap(ioOther.fHolder_Ref());
+ pAsCounted().swap(ioOther.pAsCounted());
}
}
bool ZAny::IsNull() const
- { return not fPtr_InPlace && not fHolder_Ref(); }
+ { return not fDistinguisher && not fPayload.fAsPtr; }
void ZAny::Clear()
{
- if (spIsPOD(fPtr_InPlace))
+ if (spIsPOD(fDistinguisher))
{
- fPtr_InPlace = 0;
+ fDistinguisher = 0;
}
- else if (fPtr_InPlace)
+ else if (fDistinguisher)
{
- sDtor_T<Holder_InPlace>(fBytes_InPlace);
- fPtr_InPlace = 0;
+ sDtor_T<Holder_InPlace>(&fDistinguisher);
+ fDistinguisher = 0;
}
else
{
- sDtor_T<ZRef<Holder_Counted> >(fBytes_Payload);
+ sDtor_T<ZRef<Holder_Counted> >(&fPayload);
}
- fPtr_Counted = 0;
+ fPayload.fAsPtr = 0;
}
void* ZAny::pGetMutable(const std::type_info& iTypeInfo)
{
- if (spIsPOD(fPtr_InPlace))
+ if (spIsPOD(fDistinguisher))
{
- if (iTypeInfo == *spPODTypeInfo(fPtr_InPlace))
- return fBytes_Payload;
+ if (iTypeInfo == *spPODTypeInfo(fDistinguisher))
+ return &fPayload;
}
- else if (fPtr_InPlace)
+ else if (fDistinguisher)
{
- return fHolder_InPlace().VoidStarIf(iTypeInfo);
+ return pAsInPlace().VoidStarIf(iTypeInfo);
}
- else if (ZRef<Holder_Counted>& theHolderRef = fHolder_Ref())
+ else if (ZRef<Holder_Counted>& theHolderRef = pAsCounted())
{
if (theHolderRef->Type() == iTypeInfo)
{
@@ -164,16 +164,16 @@
const void* ZAny::pGet(const std::type_info& iTypeInfo) const
{
- if (spIsPOD(fPtr_InPlace))
+ if (spIsPOD(fDistinguisher))
{
- if (iTypeInfo == *spPODTypeInfo(fPtr_InPlace))
- return fBytes_Payload;
+ if (iTypeInfo == *spPODTypeInfo(fDistinguisher))
+ return &fPayload;
}
- else if (fPtr_InPlace)
+ else if (fDistinguisher)
{
- return fHolder_InPlace().ConstVoidStarIf(iTypeInfo);
+ return pAsInPlace().ConstVoidStarIf(iTypeInfo);
}
- else if (const ZRef<Holder_Counted>& theHolderRef = fHolder_Ref())
+ else if (const ZRef<Holder_Counted>& theHolderRef = pAsCounted())
{
return theHolderRef->VoidStarIf(iTypeInfo);
}
@@ -183,23 +183,23 @@
void ZAny::pCtor_Complex(const ZAny& iOther)
{
- if (iOther.fPtr_InPlace)
+ if (iOther.fDistinguisher)
{
- iOther.fHolder_InPlace().CtorInto(fBytes_InPlace);
+ iOther.pAsInPlace().CtorInto(&fDistinguisher);
}
else
{
- fPtr_InPlace = 0;
- sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, iOther.fHolder_Ref());
+ fDistinguisher = 0;
+ sCtor_T<ZRef<Holder_Counted> >(&fPayload, iOther.pAsCounted());
}
}
void ZAny::pDtor_Complex()
{
- if (fPtr_InPlace)
- sDtor_T<Holder_InPlace>(fBytes_InPlace);
+ if (fDistinguisher)
+ sDtor_T<Holder_InPlace>(&fDistinguisher);
else
- sDtor_T<ZRef<Holder_Counted> >(fBytes_Payload);
+ sDtor_T<ZRef<Holder_Counted> >(&fPayload);
}
} // namespace ZooLib
Modified: trunk/zoolib/source/cxx/zoolib/ZAny.h
===================================================================
--- trunk/zoolib/source/cxx/zoolib/ZAny.h 2012-01-30 12:17:11 UTC (rev 2702)
+++ trunk/zoolib/source/cxx/zoolib/ZAny.h 2012-02-01 00:07:03 UTC (rev 2703)
@@ -25,7 +25,6 @@
#include "zoolib/ZCountedWithoutFinalize.h"
#include "zoolib/ZQ.h"
#include "zoolib/ZRef.h"
-#include "zoolib/ZTypes.h"
#include <typeinfo> // for std::type_info
@@ -249,12 +248,11 @@
// -----------------
- // Pseudo field accesors, hence the 'f' prefix
- Holder_InPlace& fHolder_InPlace();
- const Holder_InPlace& fHolder_InPlace() const;
+ Holder_InPlace& pAsInPlace();
+ const Holder_InPlace& pAsInPlace() const;
- ZRef<Holder_Counted>& fHolder_Ref();
- const ZRef<Holder_Counted>& fHolder_Ref() const;
+ ZRef<Holder_Counted>& pAsCounted();
+ const ZRef<Holder_Counted>& pAsCounted() const;
// -----------------
@@ -276,20 +274,20 @@
{
if (ZAnyTraits<S>::eAllowInPlace && sizeof(S) <= sizeof(fPayload))
{
- sCtor_T<Holder_InPlace_T<S> >(fBytes_InPlace, iP0, iP1);
+ sCtor_T<Holder_InPlace_T<S> >(&fDistinguisher, iP0, iP1);
}
else
{
- fPtr_InPlace = 0;
- sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, new Holder_Counted_T<S>(iP0, iP1));
+ fDistinguisher = 0;
+ sCtor_T<ZRef<Holder_Counted> >(&fPayload, new Holder_Counted_T<S>(iP0, iP1));
}
}
template <class S, class P0, class P1>
void pCtor_Counted_T(const P0& iP0, const P1& iP1)
{
- fPtr_InPlace = 0;
- sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, new Holder_Counted_T<S>(iP0, iP1));
+ fDistinguisher = 0;
+ sCtor_T<ZRef<Holder_Counted> >(&fPayload, new Holder_Counted_T<S>(iP0, iP1));
}
template <class S, class P0>
@@ -302,27 +300,27 @@
#if ZCONFIG(Compiler,GCC)
else if (std::tr1::is_pod<S>::value)
{
- fPtr_InPlace = (void*)(((intptr_t)&typeid(S)) | 1);
- sCtor_T<S>(fBytes_Payload, iP0);
+ fDistinguisher = (void*)(((intptr_t)&typeid(S)) | 1);
+ sCtor_T<S>(&fPayload, iP0);
}
#endif
else
{
- sCtor_T<Holder_InPlace_T<S> >(fBytes_InPlace, iP0);
+ sCtor_T<Holder_InPlace_T<S> >(&fDistinguisher, iP0);
}
}
else
{
- fPtr_InPlace = 0;
- sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, new Holder_Counted_T<S>(iP0));
+ fDistinguisher = 0;
+ sCtor_T<ZRef<Holder_Counted> >(&fPayload, new Holder_Counted_T<S>(iP0));
}
}
template <class S, class P0>
void pCtor_Counted_T(const P0& iP0)
{
- fPtr_InPlace = 0;
- sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, new Holder_Counted_T<S>(iP0));
+ fDistinguisher = 0;
+ sCtor_T<ZRef<Holder_Counted> >(&fPayload, new Holder_Counted_T<S>(iP0));
}
// -----------------
@@ -337,20 +335,20 @@
#if ZCONFIG(Compiler,GCC)
else if (std::tr1::is_pod<S>::value)
{
- fPtr_InPlace = (void*)(((intptr_t)&typeid(S)) | 1);
- return *sCtor_T<S>(fBytes_Payload, iP0);
+ fDistinguisher = (void*)(((intptr_t)&typeid(S)) | 1);
+ return *sCtor_T<S>(&fPayload, iP0);
}
#endif
else
{
- return sCtor_T<Holder_InPlace_T<S> >(fBytes_InPlace, iP0)->fValue;
+ return sCtor_T<Holder_InPlace_T<S> >(&fDistinguisher, iP0)->fValue;
}
}
else
{
- fPtr_InPlace = 0;
+ fDistinguisher = 0;
Holder_Counted_T<S>* theHolder = new Holder_Counted_T<S>(iP0);
- sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, theHolder);
+ sCtor_T<ZRef<Holder_Counted> >(&fPayload, theHolder);
return theHolder->fValue;
}
}
@@ -365,51 +363,49 @@
#if ZCONFIG(Compiler,GCC)
else if (std::tr1::is_pod<S>::value)
{
- fPtr_InPlace = (void*)(((intptr_t)&typeid(S)) | 1);
- return *sCtor_T<S>(fBytes_Payload);
+ fDistinguisher = (void*)(((intptr_t)&typeid(S)) | 1);
+ return *sCtor_T<S>(&fPayload);
}
#endif
else
{
- return sCtor_T<Holder_InPlace_T<S> >(fBytes_InPlace)->fValue;
+ return sCtor_T<Holder_InPlace_T<S> >(&fDistinguisher)->fValue;
}
}
else
{
- fPtr_InPlace = 0;
+ fDistinguisher = 0;
Holder_Counted_T<S>* theHolder = new Holder_Counted_T<S>;
- sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, theHolder);
+ sCtor_T<ZRef<Holder_Counted> >(&fPayload, theHolder);
return theHolder->fValue;
}
}
// -----------------
+ // There are three situations, distinguished by the value in fDistinguisher.
+ // 1. It's null. fPayload.fAsPtr points to an instance of a Holder_Counted subclass. If
+ // fPayload.fAsPtr is also null then this is itself a null object.
+ // 2. LSB is set. With an unset LSB it points to a typeid, and fPayload holds a POD value.
+ // 3. LSB is unset. It's the vptr of a Holder_InPlace, the fields of the object itself
+ // spilling over into fPayload.
+
+ void* fDistinguisher;
union
{
- char fBytes_InPlace[1];
- void* fPtr_InPlace;
- };
+ // This union provides space for a refcounted pointer to a Holder_Counted, space
+ // for the most common in-place values, and makes some values legible in a debugger.
+ void* fAsPtr;
- union
- {
- char fBytes_Payload[1];
- void* fPtr_Counted;
- union
- {
- // This union serves three purposes. It reserves space for in-place
- // values, ensures ZAny has appropriate alignment, and finally
- // makes some types legible when debugging.
- bool fAsBool;
- char fAsChar;
- short fAsShort;
- int fAsInt;
- long fAsLong;
- int64 fAsLongLong;
- float fAsFloat;
- double fAsDouble;
- } fPayload;
- };
+ bool fAsBool;
+ char fAsChar;
+ short fAsShort;
+ int fAsInt;
+ long fAsLong;
+ int64 fAsLongLong;
+ float fAsFloat;
+ double fAsDouble;
+ } fPayload;
};
// =================================================================================================
@@ -420,9 +416,9 @@
inline void ZAny::pCtor(const ZAny& iOther)
{
- if (spIsPOD(iOther.fPtr_InPlace))
+ if (spIsPOD(iOther.fDistinguisher))
{
- fPtr_InPlace = iOther.fPtr_InPlace;
+ fDistinguisher = iOther.fDistinguisher;
fPayload = iOther.fPayload;
}
else
@@ -433,7 +429,7 @@
inline void ZAny::pDtor()
{
- if (not spIsPOD(fPtr_InPlace))
+ if (not spIsPOD(fDistinguisher))
pDtor_Complex();
}
@@ -442,8 +438,8 @@
inline ZAny::ZAny()
{
- fPtr_InPlace = 0;
- fPtr_Counted = 0;
+ fDistinguisher = 0;
+ fPayload.fAsPtr = 0;
}
inline ZAny::ZAny(const ZAny& iOther)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|