blob: 73240c88ce740cd37072f26b5cc91cf6f53beb65 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001/*
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: ndnboost/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 * ndnboost::iostreams::detail::execute_all() and the function template
15 * ndnboost::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 NDNBOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
29#define NDNBOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED
30
31#if defined(_MSC_VER) && (_MSC_VER >= 1020)
32# pragma once
33#endif
34
35#include <ndnboost/config.hpp>
36#include <ndnboost/detail/workaround.hpp>
37#include <ndnboost/iostreams/detail/config/limits.hpp> // MAX_EXECUTE_ARITY
38#include <ndnboost/preprocessor/arithmetic/dec.hpp>
39#include <ndnboost/preprocessor/cat.hpp>
40#include <ndnboost/preprocessor/iteration/local.hpp>
41#include <ndnboost/preprocessor/repetition/enum_params.hpp>
42#include <ndnboost/preprocessor/repetition/enum_binary_params.hpp>
43#include <ndnboost/preprocessor/punctuation/comma_if.hpp>
44#include <ndnboost/utility/result_of.hpp>
45
46namespace ndnboost { 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(NDNBOOST_NO_RESULT_OF) && \
70 !NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x592))
71 typename ndnboost::result_of<Op()>::type
72 #else
73 NDNBOOST_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 NDNBOOST_PP_LOCAL_MACRO(n) \
90 template<typename Op, NDNBOOST_PP_ENUM_PARAMS(n, typename C)> \
91 typename execute_traits<Op>::result_type \
92 execute_all(Op op, NDNBOOST_PP_ENUM_BINARY_PARAMS(n, C, c)) \
93 { \
94 typename execute_traits<Op>::result_type r; \
95 try { \
96 r = ndnboost::iostreams::detail::execute_all( \
97 op NDNBOOST_PP_COMMA_IF(NDNBOOST_PP_DEC(n)) \
98 NDNBOOST_PP_ENUM_PARAMS(NDNBOOST_PP_DEC(n), c) \
99 ); \
100 } catch (...) { \
101 try { \
102 NDNBOOST_PP_CAT(c, NDNBOOST_PP_DEC(n))(); \
103 } catch (...) { } \
104 throw; \
105 } \
106 NDNBOOST_PP_CAT(c, NDNBOOST_PP_DEC(n))(); \
107 return r; \
108 } \
109 /**/
110
111#define NDNBOOST_PP_LOCAL_LIMITS (1, NDNBOOST_IOSTREAMS_MAX_EXECUTE_ARITY)
112#include NDNBOOST_PP_LOCAL_ITERATE()
113#undef NDNBOOST_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 ndnboost::iostreams::detail::execute_foreach(first, last, op);
126 } catch (...) { }
127 throw;
128 }
129 ++first;
130 return ndnboost::iostreams::detail::execute_foreach(first, last, op);
131}
132
133} } } // End namespaces detail, iostreams, boost.
134
135#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_EXECUTE_HPP_INCLUDED