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