fix undefined behaviour

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@482 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2006-01-16 18:21:01 +00:00
parent 658a2e5c79
commit a6e8b74fd7
2 changed files with 49 additions and 8 deletions

View file

@ -66,7 +66,6 @@ namespace Loki
///////////////////// /////////////////////
namespace Private namespace Private
{ {
template template
< <
@ -76,8 +75,8 @@ namespace Loki
> >
struct AutoPtrHolder struct AutoPtrHolder
{ {
AutoPtrHolder() //: ptr(Ptr()) this owerwrites the pointer to PtrImpl AutoPtrHolder() : ptr(Ptr())
{} // when using DeclaredRimpl!! {}
~AutoPtrHolder() ~AutoPtrHolder()
{ {
@ -85,9 +84,42 @@ namespace Loki
Del<Ptr>::Destroy( ptr ); Del<Ptr>::Destroy( ptr );
} }
Ptr Create()
{
ptr = Ptr( new Impl );
return ptr;
}
Ptr ptr;
};
template
<
class Impl,
class Ptr,
template<class> class Del
>
struct AutoPtrHolderChecked //: AutoPtrHolder<Impl,Ptr,Del>
{
static bool init_;
AutoPtrHolderChecked() : ptr(Ptr())
{
init_ = true;
}
~AutoPtrHolderChecked()
{
// delete automatically by the delete policy
Del<Ptr>::Destroy( ptr );
}
template<class T> template<class T>
operator T&() operator T&()
{ {
if(!init_)
// if this throws change the declaration order
throw 1;
Create(); Create();
return *ptr; return *ptr;
} }
@ -101,6 +133,15 @@ namespace Loki
Ptr ptr; Ptr ptr;
}; };
template
<
class Impl,
class Ptr,
template<class> class Del
>
bool AutoPtrHolderChecked<Impl,Ptr,Del>::init_ = false;
template<class T> template<class T>
struct HavePtrHolder struct HavePtrHolder
{ {
@ -266,7 +307,7 @@ namespace Loki
Type; Type;
// init declared rimpl // init declared rimpl
typedef Private::AutoPtrHolder typedef Private::AutoPtrHolderChecked
< <
Type, Type,
Type*, Type*,

View file

@ -251,7 +251,7 @@ Pimpl8;
template<class T> template<class T>
struct R struct R
{ {
typedef Loki::Private::AutoPtrHolder typedef Loki::Private::AutoPtrHolderChecked
< <
T, T,
T*, T*,
@ -271,8 +271,8 @@ struct P6 {Pimpl6 d; P6();};
struct R1 : private Rimpl1 {R1();}; struct R1 : private Rimpl1 {R1();};
struct R2 : private Rimpl2 {R2();}; struct R2 : private Rimpl2 {R2();};
struct R3 : private Rimpl3 {R3();}; struct R3 : private Rimpl3 {R3();};
struct R4 {Rimpl4& d; R<Rimpl4>::Init rinit; R4();}; struct R4 {R<Rimpl4>::Init rinit; Rimpl4& d; R4();};
struct R5 {Rimpl5& d; R<Rimpl5>::Init rinit; R5();}; struct R5 {R<Rimpl5>::Init rinit; Rimpl5& d; R5();};
struct R6 {Rimpl6& d; R<Rimpl6>::Init rinit; R6();}; struct R6 {R<Rimpl6>::Init rinit; Rimpl6& d; R6();};