| 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(); |