Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:50

0001 /*
0002  * Distributed under the Boost Software License, Version 1.0.(See accompanying 
0003  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
0004  * 
0005  * See http://www.boost.org/libs/iostreams for documentation.
0006 
0007  * File:        boost/iostreams/detail/execute.hpp
0008  * Date:        Thu Dec 06 13:21:54 MST 2007
0009  * Copyright:   2007-2008 CodeRage, LLC
0010  * Author:      Jonathan Turkanis
0011  * Contact:     turkanis at coderage dot com
0012 
0013  * Defines the overloaded function template 
0014  * boost::iostreams::detail::execute_all() and the function template 
0015  * boost::iostreams::detail::execute_foreach().
0016  *
0017  * execute_all() invokes a primary operation and performs a sequence of cleanup 
0018  * operations, returning the result of the primary operation if no exceptions
0019  * are thrown. If one of the operations throws an exception, performs the
0020  * remaining operations and rethrows the initial exception.
0021  *
0022  * execute_foreach() is a variant of std::foreach which invokes a function 
0023  * object for each item in a sequence, catching all execptions and rethrowing
0024  * the first caught exception after the function object has been invoked on each
0025  * item.
0026  */
0027 
0028 #ifndef BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
0029 #define BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
0030 
0031 #if defined(_MSC_VER)
0032 # pragma once
0033 #endif
0034 
0035 #include <boost/config.hpp>
0036 #include <boost/detail/workaround.hpp>
0037 #include <boost/iostreams/detail/config/limits.hpp>   // MAX_EXECUTE_ARITY
0038 #include <boost/preprocessor/arithmetic/dec.hpp>
0039 #include <boost/preprocessor/cat.hpp>
0040 #include <boost/preprocessor/iteration/local.hpp>
0041 #include <boost/preprocessor/repetition/enum_params.hpp>
0042 #include <boost/preprocessor/repetition/enum_binary_params.hpp>
0043 #include <boost/preprocessor/punctuation/comma_if.hpp>
0044 #include <boost/utility/result_of.hpp>
0045 
0046 namespace boost { namespace iostreams { namespace detail {
0047 
0048 // Helper for class template execute_traits.
0049 template<typename Result>
0050 struct execute_traits_impl {
0051     typedef Result result_type;
0052     template<typename Op>
0053     static Result execute(Op op) { return op(); }
0054 };
0055 
0056 // Specialization for void return. For simplicity, execute() returns int 
0057 // for operations returning void. This could be avoided with additional work.
0058 template<>
0059 struct execute_traits_impl<void> {
0060     typedef int result_type;
0061     template<typename Op>
0062     static int execute(Op op) { op(); return 0; }
0063 };
0064 
0065 // Deduces the result type of Op and allows uniform treatment of operations 
0066 // returning void and non-void.
0067 template< typename Op, 
0068           typename Result = // VC6.5 workaround.
0069               #if !defined(BOOST_NO_RESULT_OF) && \
0070                   !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x592))
0071                   typename boost::result_of<Op()>::type
0072               #else
0073                   BOOST_DEDUCED_TYPENAME Op::result_type
0074               #endif
0075           >
0076 struct execute_traits 
0077     : execute_traits_impl<Result>
0078     { };
0079 
0080 // Implementation with no cleanup operations.
0081 template<typename Op>
0082 typename execute_traits<Op>::result_type 
0083 execute_all(Op op) 
0084 { 
0085     return execute_traits<Op>::execute(op);
0086 }
0087 
0088 // Implementation with one or more cleanup operations
0089 #define BOOST_PP_LOCAL_MACRO(n) \
0090    template<typename Op, BOOST_PP_ENUM_PARAMS(n, typename C)> \
0091    typename execute_traits<Op>::result_type \
0092    execute_all(Op op, BOOST_PP_ENUM_BINARY_PARAMS(n, C, c)) \
0093    { \
0094        typename execute_traits<Op>::result_type r; \
0095        try { \
0096            r = boost::iostreams::detail::execute_all( \
0097                    op BOOST_PP_COMMA_IF(BOOST_PP_DEC(n)) \
0098                    BOOST_PP_ENUM_PARAMS(BOOST_PP_DEC(n), c) \
0099                ); \
0100        } catch (...) { \
0101            try { \
0102                BOOST_PP_CAT(c, BOOST_PP_DEC(n))(); \
0103            } catch (...) { } \
0104            throw; \
0105        } \
0106        BOOST_PP_CAT(c, BOOST_PP_DEC(n))(); \
0107        return r; \
0108    } \
0109    /**/
0110 
0111 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_EXECUTE_ARITY)
0112 #include BOOST_PP_LOCAL_ITERATE()
0113 #undef BOOST_PP_LOCAL_MACRO
0114 
0115 template<class InIt, class Op>
0116 Op execute_foreach(InIt first, InIt last, Op op)
0117 {
0118     if (first == last)
0119         return op;
0120     try {
0121         op(*first);
0122     } catch (...) {
0123         try {
0124             ++first;
0125             boost::iostreams::detail::execute_foreach(first, last, op);
0126         } catch (...) { }
0127         throw;
0128     }
0129     ++first;
0130     return boost::iostreams::detail::execute_foreach(first, last, op);
0131 }
0132 
0133 } } } // End namespaces detail, iostreams, boost.
0134 
0135 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED