|
From: <ag...@us...> - 2012-01-30 12:16:35
|
Revision: 2700
http://zoolib.svn.sourceforge.net/zoolib/?rev=2700&view=rev
Author: agreen
Date: 2012-01-30 12:16:24 +0000 (Mon, 30 Jan 2012)
Log Message:
-----------
Add DMutable and pCtorRet_T.
Modified Paths:
--------------
trunk/zoolib/source/cxx/zoolib/ZAny.h
Modified: trunk/zoolib/source/cxx/zoolib/ZAny.h
===================================================================
--- trunk/zoolib/source/cxx/zoolib/ZAny.h 2012-01-30 12:15:22 UTC (rev 2699)
+++ trunk/zoolib/source/cxx/zoolib/ZAny.h 2012-01-30 12:16:24 UTC (rev 2700)
@@ -124,10 +124,18 @@
if (S* theP = this->PGetMutable<S>())
return *theP;
pDtor();
- pCtor_T<S>(S());
- return *this->PGetMutable<S>();
+ return pCtorRet_T<S>();
}
+ template <class S>
+ S& DMutable(const S& iDefault)
+ {
+ if (S* theP = this->PGetMutable<S>())
+ return *theP;
+ pDtor();
+ return pCtorRet_T<S>(iDefault);
+ }
+
// Special purpose constructors, called by sAny and sAnyCounted
template <class S, class P0>
ZAny(const S* dummy, const P0& iP0)
@@ -170,6 +178,8 @@
class Holder_InPlace_T : public Holder_InPlace
{
public:
+ Holder_InPlace_T() {}
+
template <class P0>
Holder_InPlace_T(const P0& iP0) : fValue(iP0) {}
@@ -216,6 +226,8 @@
class Holder_Counted_T : public Holder_Counted
{
public:
+ Holder_Counted_T() {}
+
template <class P0>
Holder_Counted_T(const P0& iP0) : fValue(iP0) {}
@@ -291,7 +303,7 @@
else if (std::tr1::is_pod<S>::value)
{
fPtr_InPlace = (void*)(((intptr_t)&typeid(S)) | 1);
- *((S*)fBytes_Payload) = iP0;
+ sCtor_T<S>(fBytes_Payload, iP0);
}
#endif
else
@@ -315,6 +327,64 @@
// -----------------
+ template <class S, class P0>
+ S& pCtorRet_T(const P0& iP0)
+ {
+ if (ZAnyTraits<S>::eAllowInPlace && sizeof(S) <= sizeof(fPayload))
+ {
+ if (false)
+ {}
+ #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);
+ }
+ #endif
+ else
+ {
+ return sCtor_T<Holder_InPlace_T<S> >(fBytes_InPlace, iP0)->fValue;
+ }
+ }
+ else
+ {
+ fPtr_InPlace = 0;
+ Holder_Counted_T<S>* theHolder = new Holder_Counted_T<S>(iP0);
+ sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, theHolder);
+ return theHolder->fValue;
+ }
+ }
+
+ template <class S>
+ S& pCtorRet_T()
+ {
+ if (ZAnyTraits<S>::eAllowInPlace && sizeof(S) <= sizeof(fPayload))
+ {
+ if (false)
+ {}
+ #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);
+ }
+ #endif
+ else
+ {
+ return sCtor_T<Holder_InPlace_T<S> >(fBytes_InPlace)->fValue;
+ }
+ }
+ else
+ {
+ fPtr_InPlace = 0;
+ Holder_Counted_T<S>* theHolder = new Holder_Counted_T<S>;
+ sCtor_T<ZRef<Holder_Counted> >(fBytes_Payload, theHolder);
+ return theHolder->fValue;
+ }
+ }
+
+// -----------------
+
union
{
char fBytes_InPlace[1];
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|