execute.hpp 4.4 KB

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