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