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.)
5 * See http://www.boost.org/libs/iostreams for documentation.
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
13 * Defines the overloaded function template
14 * boost::iostreams::detail::execute_all() and the function template
15 * boost::iostreams::detail::execute_foreach().
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.
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
28 #ifndef BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
29 #define BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
31 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
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>
46 namespace boost
{ namespace iostreams
{ namespace detail
{
48 // Helper for class template execute_traits.
49 template<typename Result
>
50 struct execute_traits_impl
{
51 typedef Result result_type
;
53 static Result
execute(Op op
) { return op(); }
56 // Specialization for void return. For simplicity, execute() returns int
57 // for operations returning void. This could be avoided with additional work.
59 struct execute_traits_impl
<void> {
60 typedef int result_type
;
62 static int execute(Op op
) { op(); return 0; }
65 // Deduces the result type of Op and allows uniform treatment of operations
66 // returning void and non-void.
67 template< 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
73 BOOST_DEDUCED_TYPENAME
Op::result_type
77 : execute_traits_impl
<Result
>
80 // Implementation with no cleanup operations.
82 typename execute_traits
<Op
>::result_type
85 return execute_traits
<Op
>::execute(op
);
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)) \
94 typename execute_traits<Op>::result_type r; \
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) \
102 BOOST_PP_CAT(c, BOOST_PP_DEC(n))(); \
106 BOOST_PP_CAT(c, BOOST_PP_DEC(n))(); \
111 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_IOSTREAMS_MAX_EXECUTE_ARITY)
112 #include BOOST_PP_LOCAL_ITERATE()
113 #undef BOOST_PP_LOCAL_MACRO
115 template<class InIt
, class Op
>
116 Op
execute_foreach(InIt first
, InIt last
, Op op
)
125 boost::iostreams::detail::execute_foreach(first
, last
, op
);
130 return boost::iostreams::detail::execute_foreach(first
, last
, op
);
133 } } } // End namespaces detail, iostreams, boost.
135 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED