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:
parent
658a2e5c79
commit
a6e8b74fd7
2 changed files with 49 additions and 8 deletions
|
@ -66,7 +66,6 @@ namespace Loki
|
|||
/////////////////////
|
||||
|
||||
namespace Private
|
||||
|
||||
{
|
||||
template
|
||||
<
|
||||
|
@ -76,8 +75,8 @@ namespace Loki
|
|||
>
|
||||
struct AutoPtrHolder
|
||||
{
|
||||
AutoPtrHolder() //: ptr(Ptr()) this owerwrites the pointer to PtrImpl
|
||||
{} // when using DeclaredRimpl!!
|
||||
AutoPtrHolder() : ptr(Ptr())
|
||||
{}
|
||||
|
||||
~AutoPtrHolder()
|
||||
{
|
||||
|
@ -85,9 +84,42 @@ namespace Loki
|
|||
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>
|
||||
operator T&()
|
||||
{
|
||||
if(!init_)
|
||||
// if this throws change the declaration order
|
||||
throw 1;
|
||||
Create();
|
||||
return *ptr;
|
||||
}
|
||||
|
@ -101,6 +133,15 @@ namespace Loki
|
|||
Ptr ptr;
|
||||
};
|
||||
|
||||
template
|
||||
<
|
||||
class Impl,
|
||||
class Ptr,
|
||||
template<class> class Del
|
||||
>
|
||||
bool AutoPtrHolderChecked<Impl,Ptr,Del>::init_ = false;
|
||||
|
||||
|
||||
template<class T>
|
||||
struct HavePtrHolder
|
||||
{
|
||||
|
@ -266,7 +307,7 @@ namespace Loki
|
|||
Type;
|
||||
|
||||
// init declared rimpl
|
||||
typedef Private::AutoPtrHolder
|
||||
typedef Private::AutoPtrHolderChecked
|
||||
<
|
||||
Type,
|
||||
Type*,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue