dynamic.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_IO_DETAIL_DYNAMIC_HPP
  9. #define BOOST_GIL_IO_DETAIL_DYNAMIC_HPP
  10. #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
  11. #include <boost/gil/detail/mp11.hpp>
  12. #include <boost/gil/io/error.hpp>
  13. #include <type_traits>
  14. namespace boost { namespace gil { namespace detail {
  15. template <long N>
  16. struct construct_matched_t
  17. {
  18. template <typename ...Images, typename Pred>
  19. static bool apply(any_image<Images...>& img, Pred pred)
  20. {
  21. if (pred.template apply<mp11::mp_at_c<any_image<Images...>, N-1>>())
  22. {
  23. using image_t = mp11::mp_at_c<any_image<Images...>, N-1>;
  24. image_t x;
  25. img = std::move(x);
  26. return true;
  27. }
  28. else
  29. return construct_matched_t<N-1>::apply(img, pred);
  30. }
  31. };
  32. template <>
  33. struct construct_matched_t<0>
  34. {
  35. template <typename ...Images, typename Pred>
  36. static bool apply(any_image<Images...>&, Pred) { return false; }
  37. };
  38. // A function object that can be passed to variant2::visit.
  39. // Given a predicate IsSupported taking a view type and returning an boolean integral coonstant,
  40. // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
  41. template <typename IsSupported, typename OpClass>
  42. class dynamic_io_fnobj
  43. {
  44. private:
  45. OpClass* _op;
  46. template <typename View>
  47. void apply(View const& view, std::true_type) { _op->apply(view); }
  48. template <typename View, typename Info>
  49. void apply(View const& view, Info const & info, const std::true_type) { _op->apply(view, info); }
  50. template <typename View>
  51. void apply(View const& /* view */, std::false_type)
  52. {
  53. io_error("dynamic_io: unsupported view type for the given file format");
  54. }
  55. template <typename View, typename Info >
  56. void apply(View const& /* view */, Info const& /* info */, const std::false_type)
  57. {
  58. io_error("dynamic_io: unsupported view type for the given file format");
  59. }
  60. public:
  61. dynamic_io_fnobj(OpClass* op) : _op(op) {}
  62. using result_type = void;
  63. template <typename View>
  64. void operator()(View const& view)
  65. {
  66. apply(view, typename IsSupported::template apply<View>::type());
  67. }
  68. template <typename View, typename Info>
  69. void operator()(View const& view, Info const& info)
  70. {
  71. apply(view, info, typename IsSupported::template apply<View>::type());
  72. }
  73. };
  74. /// \brief Within the any_image, constructs an image with the given dimensions
  75. /// and a type that satisfies the given predicate
  76. template <typename ...Images, typename Pred>
  77. inline bool construct_matched(any_image<Images...>& img, Pred pred)
  78. {
  79. constexpr auto size = mp11::mp_size<any_image<Images...>>::value;
  80. return construct_matched_t<size>::apply(img, pred);
  81. }
  82. } } } // namespace boost::gil::detail
  83. #endif