Changeset 56195

Show
Ignore:
Timestamp:
10/09/08 04:27:59 (6 weeks ago)
Author:
VZ
Message:

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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • wxWidgets/trunk/include/wx/vector.h

    r55654 r56195  
    2323 
    2424#include "wx/utils.h" 
     25#include "wx/scopeguard.h" 
    2526#include "wx/meta/movable.h" 
    2627#include "wx/meta/if.h" 
     
    292293        reserve(size() + 1); 
    293294 
     295        // the place where the new element is going to be inserted 
     296        value_type * const place = m_values + idx; 
     297 
    294298        // unless we're inserting at the end, move following elements out of 
    295299        // the way: 
    296300        if ( after > 0 ) 
    297         { 
    298             Ops::MemmoveForward(m_values + idx + 1, m_values + idx, after); 
    299         } 
    300  
    301 #if wxUSE_EXCEPTIONS 
    302         try 
    303         { 
    304 #endif 
    305             // use placement new to initialize new object in preallocated place 
    306             // in m_values and store 'v' in it: 
    307             void* const place = m_values + idx; 
    308             new(place) value_type(v); 
    309 #if wxUSE_EXCEPTIONS 
    310         } 
    311         catch ( ... ) 
    312         { 
    313             // if the ctor threw an exception, we need to move all the elements 
    314             // back to their original positions in m_values 
    315             if ( after > 0 ) 
    316             { 
    317                 Ops::MemmoveBackward(m_values + idx, m_values + idx + 1, after); 
    318             } 
    319  
    320             throw; // rethrow the exception 
    321         } 
    322 #endif // wxUSE_EXCEPTIONS 
    323  
    324         // increment m_size only if ctor didn't throw -- if it did, we'll be 
    325         // left with m_values larger than necessary, but number of elements will 
    326         // be the same 
     301            Ops::MemmoveForward(place + 1, place, after); 
     302 
     303        // if the ctor called below throws an exception, we need to move all 
     304        // the elements back to their original positions in m_values 
     305        wxScopeGuard moveBack = wxMakeGuard( 
     306                Ops::MemmoveBackward, place, place + 1, after); 
     307        if ( !after ) 
     308            moveBack.Dismiss(); 
     309 
     310        // use placement new to initialize new object in preallocated place in 
     311        // m_values and store 'v' in it: 
     312        new(place) value_type(v); 
     313 
     314        // now that we did successfully add the new element, increment the size 
     315        // and disable moving the items back 
     316        moveBack.Dismiss(); 
    327317        m_size++; 
    328318