Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85120 - trunk/boost/xpressive/detail/utility
From: eric_at_[hidden]
Date: 2013-07-23 00:03:37


Author: eric_niebler
Date: 2013-07-23 00:03:37 EDT (Tue, 23 Jul 2013)
New Revision: 85120
URL: http://svn.boost.org/trac/boost/changeset/85120

Log:
use RAII instead of try/catch in sequence_stack.hpp, refs #8882

Text files modified:
   trunk/boost/xpressive/detail/utility/sequence_stack.hpp | 33 ++++++++++++++++++++-------------
   1 files changed, 20 insertions(+), 13 deletions(-)

Modified: trunk/boost/xpressive/detail/utility/sequence_stack.hpp
==============================================================================
--- trunk/boost/xpressive/detail/utility/sequence_stack.hpp Mon Jul 22 23:26:30 2013 (r85119)
+++ trunk/boost/xpressive/detail/utility/sequence_stack.hpp 2013-07-23 00:03:37 EDT (Tue, 23 Jul 2013) (r85120)
@@ -31,22 +31,29 @@
 template<typename T>
 struct sequence_stack
 {
+ struct allocate_guard_t;
+ friend struct allocate_guard_t;
+ struct allocate_guard_t
+ {
+ std::size_t i;
+ T *p;
+ bool dismissed;
+ ~allocate_guard_t()
+ {
+ if(!this->dismissed)
+ sequence_stack::deallocate(this->p, this->i);
+ }
+ };
 private:
     static T *allocate(std::size_t size, T const &t)
     {
- std::size_t i = 0;
- T *p = (T *)::operator new(size * sizeof(T));
- try
- {
- for(; i < size; ++i)
- ::new((void *)(p+i)) T(t);
- }
- catch(...)
- {
- deallocate(p, i);
- throw;
- }
- return p;
+ allocate_guard_t guard = {0, (T *)::operator new(size * sizeof(T)), false};
+
+ for(; guard.i < size; ++guard.i)
+ ::new((void *)(guard.p + guard.i)) T(t);
+ guard.dismissed = true;
+
+ return guard.p;
     }
 
     static void deallocate(T *p, std::size_t i)


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk