dynamic_io_new.hpp 2.9 KB

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