Warning, file /include/boost/leaf/context.hpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 #ifndef BOOST_LEAF_CONTEXT_HPP_INCLUDED
0002 #define BOOST_LEAF_CONTEXT_HPP_INCLUDED
0003
0004
0005
0006
0007
0008
0009 #include <boost/leaf/config.hpp>
0010 #include <boost/leaf/error.hpp>
0011
0012 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
0013 # include <thread>
0014 #endif
0015
0016 namespace boost { namespace leaf {
0017
0018 class error_info;
0019 class diagnostic_info;
0020 class verbose_diagnostic_info;
0021
0022 template <class>
0023 struct is_predicate: std::false_type
0024 {
0025 };
0026
0027 namespace leaf_detail
0028 {
0029 template <class T>
0030 struct is_exception: std::is_base_of<std::exception, typename std::decay<T>::type>
0031 {
0032 };
0033
0034 template <class E>
0035 struct handler_argument_traits;
0036
0037 template <class E, bool IsPredicate = is_predicate<E>::value>
0038 struct handler_argument_traits_defaults;
0039
0040 template <class E>
0041 struct handler_argument_traits_defaults<E, false>
0042 {
0043 using error_type = typename std::decay<E>::type;
0044 constexpr static bool always_available = false;
0045
0046 template <class Tup>
0047 BOOST_LEAF_CONSTEXPR static error_type const * check( Tup const &, error_info const & ) noexcept;
0048
0049 template <class Tup>
0050 BOOST_LEAF_CONSTEXPR static error_type * check( Tup &, error_info const & ) noexcept;
0051
0052 template <class Tup>
0053 BOOST_LEAF_CONSTEXPR static E get( Tup & tup, error_info const & ei ) noexcept
0054 {
0055 return *check(tup, ei);
0056 }
0057
0058 static_assert(!is_predicate<error_type>::value, "Handlers must take predicate arguments by value");
0059 static_assert(!std::is_same<E, error_info>::value, "Handlers must take leaf::error_info arguments by const &");
0060 static_assert(!std::is_same<E, diagnostic_info>::value, "Handlers must take leaf::diagnostic_info arguments by const &");
0061 static_assert(!std::is_same<E, verbose_diagnostic_info>::value, "Handlers must take leaf::verbose_diagnostic_info arguments by const &");
0062 };
0063
0064 template <class Pred>
0065 struct handler_argument_traits_defaults<Pred, true>: handler_argument_traits<typename Pred::error_type>
0066 {
0067 using base = handler_argument_traits<typename Pred::error_type>;
0068 static_assert(!base::always_available, "Predicates can't use types that are always_available");
0069
0070 template <class Tup>
0071 BOOST_LEAF_CONSTEXPR static bool check( Tup const & tup, error_info const & ei ) noexcept
0072 {
0073 auto e = base::check(tup, ei);
0074 return e && Pred::evaluate(*e);
0075 }
0076
0077 template <class Tup>
0078 BOOST_LEAF_CONSTEXPR static Pred get( Tup const & tup, error_info const & ei ) noexcept
0079 {
0080 return Pred{*base::check(tup, ei)};
0081 }
0082 };
0083
0084 template <class E>
0085 struct handler_argument_always_available
0086 {
0087 using error_type = E;
0088 constexpr static bool always_available = true;
0089
0090 template <class Tup>
0091 BOOST_LEAF_CONSTEXPR static bool check( Tup &, error_info const & ) noexcept
0092 {
0093 return true;
0094 }
0095 };
0096
0097 template <class E>
0098 struct handler_argument_traits: handler_argument_traits_defaults<E>
0099 {
0100 };
0101
0102 template <>
0103 struct handler_argument_traits<void>
0104 {
0105 using error_type = void;
0106 constexpr static bool always_available = false;
0107
0108 template <class Tup>
0109 BOOST_LEAF_CONSTEXPR static std::exception const * check( Tup const &, error_info const & ) noexcept;
0110 };
0111
0112 template <class E>
0113 struct handler_argument_traits<E &&>
0114 {
0115 static_assert(sizeof(E) == 0, "Error handlers may not take rvalue ref arguments");
0116 };
0117
0118 template <class E>
0119 struct handler_argument_traits<E *>: handler_argument_always_available<typename std::remove_const<E>::type>
0120 {
0121 template <class Tup>
0122 BOOST_LEAF_CONSTEXPR static E * get( Tup & tup, error_info const & ei) noexcept
0123 {
0124 return handler_argument_traits_defaults<E>::check(tup, ei);
0125 }
0126 };
0127
0128 template <class E>
0129 struct handler_argument_traits_require_by_value
0130 {
0131 static_assert(sizeof(E) == 0, "Error handlers must take this type by value");
0132 };
0133 }
0134
0135
0136
0137 namespace leaf_detail
0138 {
0139 template <int I, class Tuple>
0140 struct tuple_for_each
0141 {
0142 BOOST_LEAF_CONSTEXPR static void activate( Tuple & tup ) noexcept
0143 {
0144 static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
0145 tuple_for_each<I-1,Tuple>::activate(tup);
0146 std::get<I-1>(tup).activate();
0147 }
0148
0149 BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & tup ) noexcept
0150 {
0151 static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
0152 std::get<I-1>(tup).deactivate();
0153 tuple_for_each<I-1,Tuple>::deactivate(tup);
0154 }
0155
0156 BOOST_LEAF_CONSTEXPR static void unload( Tuple & tup, int err_id ) noexcept
0157 {
0158 static_assert(!std::is_same<error_info, typename std::decay<decltype(std::get<I-1>(tup))>::type>::value, "Bug in LEAF: context type deduction");
0159 BOOST_LEAF_ASSERT(err_id != 0);
0160 auto & sl = std::get<I-1>(tup);
0161 sl.unload(err_id);
0162 tuple_for_each<I-1,Tuple>::unload(tup, err_id);
0163 }
0164
0165 template <class CharT, class Traits>
0166 static void print( std::basic_ostream<CharT, Traits> & os, void const * tup, int err_id_to_print )
0167 {
0168 BOOST_LEAF_ASSERT(tup != nullptr);
0169 tuple_for_each<I-1,Tuple>::print(os, tup, err_id_to_print);
0170 std::get<I-1>(*static_cast<Tuple const *>(tup)).print(os, err_id_to_print);
0171 }
0172 };
0173
0174 template <class Tuple>
0175 struct tuple_for_each<0, Tuple>
0176 {
0177 BOOST_LEAF_CONSTEXPR static void activate( Tuple & ) noexcept { }
0178 BOOST_LEAF_CONSTEXPR static void deactivate( Tuple & ) noexcept { }
0179 BOOST_LEAF_CONSTEXPR static void unload( Tuple &, int ) noexcept { }
0180 template <class CharT, class Traits>
0181 BOOST_LEAF_CONSTEXPR static void print( std::basic_ostream<CharT, Traits> &, void const *, int ) { }
0182 };
0183 }
0184
0185
0186
0187 namespace leaf_detail
0188 {
0189 template <class T> struct does_not_participate_in_context_deduction: std::is_abstract<T> { };
0190 template <> struct does_not_participate_in_context_deduction<void>: std::true_type { };
0191 template <> struct does_not_participate_in_context_deduction<error_id>: std::true_type { };
0192
0193 template <class L>
0194 struct deduce_e_type_list;
0195
0196 template <template<class...> class L, class... T>
0197 struct deduce_e_type_list<L<T...>>
0198 {
0199 using type =
0200 leaf_detail_mp11::mp_remove_if<
0201 leaf_detail_mp11::mp_unique<
0202 leaf_detail_mp11::mp_list<typename handler_argument_traits<T>::error_type...>
0203 >,
0204 does_not_participate_in_context_deduction
0205 >;
0206 };
0207
0208 template <class L>
0209 struct deduce_e_tuple_impl;
0210
0211 template <template <class...> class L, class... E>
0212 struct deduce_e_tuple_impl<L<E...>>
0213 {
0214 using type = std::tuple<slot<E>...>;
0215 };
0216
0217 template <class... E>
0218 using deduce_e_tuple = typename deduce_e_tuple_impl<typename deduce_e_type_list<leaf_detail_mp11::mp_list<E...>>::type>::type;
0219 }
0220
0221
0222
0223 template <class... E>
0224 class context
0225 {
0226 context( context const & ) = delete;
0227 context & operator=( context const & ) = delete;
0228
0229 using Tup = leaf_detail::deduce_e_tuple<E...>;
0230 Tup tup_;
0231 bool is_active_;
0232
0233 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
0234 std::thread::id thread_id_;
0235 #endif
0236
0237 public:
0238
0239 BOOST_LEAF_CONSTEXPR context( context && x ) noexcept:
0240 tup_(std::move(x.tup_)),
0241 is_active_(false)
0242 {
0243 BOOST_LEAF_ASSERT(!x.is_active());
0244 }
0245
0246 BOOST_LEAF_CONSTEXPR context() noexcept:
0247 is_active_(false)
0248 {
0249 }
0250
0251 ~context() noexcept
0252 {
0253 BOOST_LEAF_ASSERT(!is_active());
0254 }
0255
0256 BOOST_LEAF_CONSTEXPR Tup const & tup() const noexcept
0257 {
0258 return tup_;
0259 }
0260
0261 BOOST_LEAF_CONSTEXPR Tup & tup() noexcept
0262 {
0263 return tup_;
0264 }
0265
0266 BOOST_LEAF_CONSTEXPR void activate() noexcept
0267 {
0268 using namespace leaf_detail;
0269 BOOST_LEAF_ASSERT(!is_active());
0270 tuple_for_each<std::tuple_size<Tup>::value,Tup>::activate(tup_);
0271 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
0272 thread_id_ = std::this_thread::get_id();
0273 #endif
0274 is_active_ = true;
0275 }
0276
0277 BOOST_LEAF_CONSTEXPR void deactivate() noexcept
0278 {
0279 using namespace leaf_detail;
0280 BOOST_LEAF_ASSERT(is_active());
0281 is_active_ = false;
0282 #if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
0283 BOOST_LEAF_ASSERT(std::this_thread::get_id() == thread_id_);
0284 thread_id_ = std::thread::id();
0285 #endif
0286 tuple_for_each<std::tuple_size<Tup>::value,Tup>::deactivate(tup_);
0287 }
0288
0289 BOOST_LEAF_CONSTEXPR void unload(error_id id) noexcept
0290 {
0291 BOOST_LEAF_ASSERT(!is_active());
0292 leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::unload(tup_, id.value());
0293 }
0294
0295 BOOST_LEAF_CONSTEXPR bool is_active() const noexcept
0296 {
0297 return is_active_;
0298 }
0299
0300 template <class CharT, class Traits>
0301 void print( std::basic_ostream<CharT, Traits> & os ) const
0302 {
0303 leaf_detail::tuple_for_each<std::tuple_size<Tup>::value,Tup>::print(os, &tup_, 0);
0304 }
0305
0306 template <class CharT, class Traits>
0307 friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, context const & ctx )
0308 {
0309 ctx.print(os);
0310 return os;
0311 }
0312
0313 template <class R, class... H>
0314 BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... ) const;
0315
0316 template <class R, class... H>
0317 BOOST_LEAF_CONSTEXPR R handle_error( error_id, H && ... );
0318 };
0319
0320 template <class Ctx>
0321 BOOST_LEAF_CONSTEXPR BOOST_LEAF_ALWAYS_INLINE context_activator<Ctx> activate_context(Ctx & ctx) noexcept
0322 {
0323 return context_activator<Ctx>(ctx);
0324 }
0325
0326
0327
0328 namespace leaf_detail
0329 {
0330 template <class TypeList>
0331 struct deduce_context_impl;
0332
0333 template <template <class...> class L, class... E>
0334 struct deduce_context_impl<L<E...>>
0335 {
0336 using type = context<E...>;
0337 };
0338
0339 template <class TypeList>
0340 using deduce_context = typename deduce_context_impl<TypeList>::type;
0341
0342 template <class H>
0343 struct fn_mp_args_fwd
0344 {
0345 using type = fn_mp_args<H>;
0346 };
0347
0348 template <class... H>
0349 struct fn_mp_args_fwd<std::tuple<H...> &>: fn_mp_args_fwd<std::tuple<H...>> { };
0350
0351 template <class... H>
0352 struct fn_mp_args_fwd<std::tuple<H...> const &>: fn_mp_args_fwd<std::tuple<H...>> { };
0353
0354 template <class... H>
0355 struct fn_mp_args_fwd<std::tuple<H...>>
0356 {
0357 using type = leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>;
0358 };
0359
0360 template <class... H>
0361 struct context_type_from_handlers_impl
0362 {
0363 using type = deduce_context<leaf_detail_mp11::mp_append<typename fn_mp_args_fwd<H>::type...>>;
0364 };
0365 }
0366
0367 template <class... H>
0368 using context_type_from_handlers = typename leaf_detail::context_type_from_handlers_impl<H...>::type;
0369
0370
0371
0372 template <class... H>
0373 BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context() noexcept
0374 {
0375 return { };
0376 }
0377
0378 template <class... H>
0379 BOOST_LEAF_CONSTEXPR inline context_type_from_handlers<H...> make_context( H && ... ) noexcept
0380 {
0381 return { };
0382 }
0383
0384
0385
0386 #if BOOST_LEAF_CFG_CAPTURE
0387
0388 template <class...>
0389 BOOST_LEAF_DEPRECATED("Please use try_capture_all instead of make_shared_context/capture.")
0390 inline context_ptr make_shared_context() noexcept
0391 {
0392 return std::make_shared<polymorphic_context>();
0393 }
0394
0395 template <class... H>
0396 BOOST_LEAF_DEPRECATED("Please use try_capture_all instead of make_shared_context/capture.")
0397 inline context_ptr make_shared_context( H && ... ) noexcept
0398 {
0399 return std::make_shared<polymorphic_context>();
0400 }
0401
0402 #endif
0403
0404 } }
0405
0406 #endif