PageRenderTime 35ms CodeModel.GetById 24ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/iostreams/detail/execute.hpp

http://hadesmem.googlecode.com/
C++ Header | 135 lines | 88 code | 13 blank | 34 comment | 4 complexity | 25a5531bacbc6fe78caf129ca0f8b8dc MD5 | raw file
  1/*
  2 * Distributed under the Boost Software License, Version 1.0.(See accompanying 
  3 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  4 * 
  5 * See http://www.boost.org/libs/iostreams for documentation.
  6
  7 * File:        boost/iostreams/detail/execute.hpp
  8 * Date:        Thu Dec 06 13:21:54 MST 2007
  9 * Copyright:   2007-2008 CodeRage, LLC
 10 * Author:      Jonathan Turkanis
 11 * Contact:     turkanis at coderage dot com
 12
 13 * Defines the overloaded function template 
 14 * boost::iostreams::detail::execute_all() and the function template 
 15 * boost::iostreams::detail::execute_foreach().
 16 *
 17 * execute_all() invokes a primary operation and performs a sequence of cleanup 
 18 * operations, returning the result of the primary operation if no exceptions
 19 * are thrown. If one of the operations throws an exception, performs the
 20 * remaining operations and rethrows the initial exception.
 21 *
 22 * execute_foreach() is a variant of std::foreach which invokes a function 
 23 * object for each item in a sequence, catching all execptions and rethrowing
 24 * the first caught exception after the function object has been invoked on each
 25 * item.
 26 */
 27
 28#ifndef BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
 29#define BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
 30
 31#if defined(_MSC_VER) && (_MSC_VER >= 1020)
 32# pragma once
 33#endif
 34
 35#include <boost/config.hpp>
 36#include <boost/detail/workaround.hpp>
 37#include <boost/iostreams/detail/config/limits.hpp>   // MAX_EXECUTE_ARITY
 38#include <boost/preprocessor/arithmetic/dec.hpp>
 39#include <boost/preprocessor/cat.hpp>
 40#include <boost/preprocessor/iteration/local.hpp>
 41#include <boost/preprocessor/repetition/enum_params.hpp>
 42#include <boost/preprocessor/repetition/enum_binary_params.hpp>
 43#include <boost/preprocessor/punctuation/comma_if.hpp>
 44#include <boost/utility/result_of.hpp>
 45
 46namespace boost { namespace iostreams { namespace detail {
 47
 48// Helper for class template execute_traits.
 49template<typename Result>
 50struct execute_traits_impl {
 51    typedef Result result_type;
 52    template<typename Op>
 53    static Result execute(Op op) { return op(); }
 54};
 55
 56// Specialization for void return. For simplicity, execute() returns int 
 57// for operations returning void. This could be avoided with additional work.
 58template<>
 59struct execute_traits_impl<void> {
 60    typedef int result_type;
 61    template<typename Op>
 62    static int execute(Op op) { op(); return 0; }
 63};
 64
 65// Deduces the result type of Op and allows uniform treatment of operations 
 66// returning void and non-void.
 67template< typename Op, 
 68          typename Result = // VC6.5 workaround.
 69              #if !defined(BOOST_NO_RESULT_OF) && \
 70                  !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
 71                  typename boost::result_of<Op()>::type
 72              #else
 73                  BOOST_DEDUCED_TYPENAME Op::result_type
 74              #endif
 75          >
 76struct execute_traits 
 77    : execute_traits_impl<Result>
 78    { };
 79
 80// Implementation with no cleanup operations.
 81template<typename Op>
 82typename execute_traits<Op>::result_type 
 83execute_all(Op op) 
 84{ 
 85    return execute_traits<Op>::execute(op);
 86}
 87
 88// Implementation with one or more cleanup operations
 89#define BOOST_PP_LOCAL_MACRO(n) \
 90   template<typename Op, BOOST_PP_ENUM_PARAMS(n, typename C)> \
 91   typename execute_traits<Op>::result_type \
 92   execute_all(Op op, BOOST_PP_ENUM_BINARY_PARAMS(n, C, c)) \
 93   { \
 94       typename execute_traits<Op>::result_type r; \
 95       try { \
 96           r = boost::iostreams::detail::execute_all( \
 97                   op BOOST_PP_COMMA_IF(BOOST_PP_DEC(n)) \
 98                   BOOST_PP_ENUM_PARAMS(BOOST_PP_DEC(n), c) \
 99               ); \
100       } catch (...) { \
101           try { \
102               BOOST_PP_CAT(c, BOOST_PP_DEC(n))(); \
103           } catch (...) { } \
104           throw; \
105       } \
106       BOOST_PP_CAT(c, BOOST_PP_DEC(n))(); \
107       return r; \
108   } \
109   /**/
110
111#define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_EXECUTE_ARITY)
112#include BOOST_PP_LOCAL_ITERATE()
113#undef BOOST_PP_LOCAL_MACRO
114
115template<class InIt, class Op>
116Op execute_foreach(InIt first, InIt last, Op op)
117{
118    if (first == last)
119        return op;
120    try {
121        op(*first);
122    } catch (...) {
123        try {
124            ++first;
125            boost::iostreams::detail::execute_foreach(first, last, op);
126        } catch (...) { }
127        throw;
128    }
129    ++first;
130    return boost::iostreams::detail::execute_foreach(first, last, op);
131}
132
133} } } // End namespaces detail, iostreams, boost.
134
135#endif // #ifndef BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED