use wxScopeGuard instead of an explicit try/catch(...)/throw: this suppresses wxUSE_EXCEPTIONS tests and also avoids MSVC unreachable code warnings

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56195 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-10-09 11:27:59 +00:00
parent bcffb4d1a9
commit fc31181d2c

View File

@@ -22,6 +22,7 @@
#else // !wxUSE_STL #else // !wxUSE_STL
#include "wx/utils.h" #include "wx/utils.h"
#include "wx/scopeguard.h"
#include "wx/meta/movable.h" #include "wx/meta/movable.h"
#include "wx/meta/if.h" #include "wx/meta/if.h"
@@ -291,39 +292,28 @@ public:
reserve(size() + 1); reserve(size() + 1);
// the place where the new element is going to be inserted
value_type * const place = m_values + idx;
// unless we're inserting at the end, move following elements out of // unless we're inserting at the end, move following elements out of
// the way: // the way:
if ( after > 0 ) if ( after > 0 )
{ Ops::MemmoveForward(place + 1, place, after);
Ops::MemmoveForward(m_values + idx + 1, m_values + idx, after);
}
#if wxUSE_EXCEPTIONS // if the ctor called below throws an exception, we need to move all
try // the elements back to their original positions in m_values
{ wxScopeGuard moveBack = wxMakeGuard(
#endif Ops::MemmoveBackward, place, place + 1, after);
// use placement new to initialize new object in preallocated place if ( !after )
// in m_values and store 'v' in it: moveBack.Dismiss();
void* const place = m_values + idx;
// use placement new to initialize new object in preallocated place in
// m_values and store 'v' in it:
new(place) value_type(v); new(place) value_type(v);
#if wxUSE_EXCEPTIONS
}
catch ( ... )
{
// if the ctor threw an exception, we need to move all the elements
// back to their original positions in m_values
if ( after > 0 )
{
Ops::MemmoveBackward(m_values + idx, m_values + idx + 1, after);
}
throw; // rethrow the exception // now that we did successfully add the new element, increment the size
} // and disable moving the items back
#endif // wxUSE_EXCEPTIONS moveBack.Dismiss();
// increment m_size only if ctor didn't throw -- if it did, we'll be
// left with m_values larger than necessary, but number of elements will
// be the same
m_size++; m_size++;
return begin() + idx; return begin() + idx;