dynamic_io_new.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_DYNAMIC_IO_NEW_HPP
  9. #define BOOST_GIL_IO_DYNAMIC_IO_NEW_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 {
  15. namespace detail {
  16. template <long N>
  17. struct construct_matched_t
  18. {
  19. template <typename ...Images,typename Pred>
  20. static bool apply(any_image<Images...>& img, Pred pred)
  21. {
  22. if (pred.template apply<mp11::mp_at_c<any_image<Images...>, N-1>>())
  23. {
  24. using image_t = mp11::mp_at_c<any_image<Images...>, N-1>;
  25. image_t x;
  26. img = std::move(x);
  27. return true;
  28. }
  29. else
  30. return construct_matched_t<N-1>::apply(img, pred);
  31. }
  32. };
  33. template <>
  34. struct construct_matched_t<0>
  35. {
  36. template <typename ...Images,typename Pred>
  37. static bool apply(any_image<Images...>&,Pred) { return false; }
  38. };
  39. // A function object that can be passed to apply_operation.
  40. // Given a predicate IsSupported taking a view type and returning an boolean integral coonstant,
  41. // calls the apply method of OpClass with the view if the given view IsSupported, or throws an exception otherwise
  42. template <typename IsSupported, typename OpClass>
  43. class dynamic_io_fnobj
  44. {
  45. private:
  46. OpClass* _op;
  47. template <typename View>
  48. void apply(View const& view, std::true_type) { _op->apply(view); }
  49. template <typename View, typename Info>
  50. void apply(View const& view, Info const & info, const std::true_type) { _op->apply(view, info); }
  51. template <typename View>
  52. void apply(View const& /* view */, std::false_type)
  53. {
  54. io_error("dynamic_io: unsupported view type for the given file format");
  55. }
  56. template <typename View, typename Info >
  57. void apply(View const& /* view */, Info const& /* info */, const std::false_type)
  58. {
  59. io_error("dynamic_io: unsupported view type for the given file format");
  60. }
  61. public:
  62. dynamic_io_fnobj(OpClass* op) : _op(op) {}
  63. using result_type = void;
  64. template <typename View>
  65. void operator()(View const& view)
  66. {
  67. apply(view, typename IsSupported::template apply<View>::type());
  68. }
  69. template< typename View, typename Info >
  70. void operator()(View const& view, Info const& info)
  71. {
  72. apply(view, info, typename IsSupported::template apply<View>::type());
  73. }
  74. };
  75. } // namespace detail
  76. /// \brief Within the any_image, constructs an image with the given dimensions
  77. /// and a type that satisfies the given predicate
  78. template <typename ...Images,typename Pred>
  79. inline bool construct_matched(any_image<Images...>& img, Pred pred)
  80. {
  81. constexpr auto size = mp11::mp_size<any_image<Images...>>::value;
  82. return detail::construct_matched_t<size>::apply(img, pred);
  83. }
  84. } } // namespace boost::gil
  85. #endif