Boost GIL


make_reader.hpp
1 //
2 // Copyright 2012 Christian Henning
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_MAKE_READER_HPP
9 #define BOOST_GIL_IO_MAKE_READER_HPP
10 
11 #include <boost/gil/detail/mp11.hpp>
12 #include <boost/gil/io/get_reader.hpp>
13 
14 #include <type_traits>
15 
16 namespace boost { namespace gil {
17 
18 template <typename String, typename FormatTag, typename ConversionPolicy>
19 inline
20 auto make_reader(
21  String const&file_name,
22  image_read_settings<FormatTag> const& settings,
23  ConversionPolicy const&,
24  typename std::enable_if
25  <
26  mp11::mp_and
27  <
28  detail::is_supported_path_spec<String>,
29  is_format_tag<FormatTag>
30  >::value
31  >::type* /*dummy*/ = nullptr)
32  -> typename get_reader<String, FormatTag, ConversionPolicy>::type
33 {
34  typename get_read_device<String, FormatTag>::type device(
35  detail::convert_to_native_string(file_name),
36  typename detail::file_stream_device<FormatTag>::read_tag());
37 
38  return
39  typename get_reader<String, FormatTag, ConversionPolicy>::type(device, settings);
40 }
41 
42 template< typename FormatTag
43  , typename ConversionPolicy
44  >
45 inline
46 typename get_reader< std::wstring
47  , FormatTag
48  , ConversionPolicy
49  >::type
50 make_reader( const std::wstring& file_name
51  , const image_read_settings< FormatTag >& settings
52  , const ConversionPolicy&
53  )
54 {
55  const char* str = detail::convert_to_native_string( file_name );
56 
57  typename get_read_device< std::wstring
58  , FormatTag
59  >::type device( str
60  , typename detail::file_stream_device< FormatTag >::read_tag()
61  );
62 
63  delete[] str; // TODO: RAII
64 
65  return typename get_reader< std::wstring
66  , FormatTag
67  , ConversionPolicy
68  >::type( device
69  , settings
70  );
71 }
72 
73 template< typename FormatTag
74  , typename ConversionPolicy
75  >
76 inline
77 typename get_reader< std::wstring
78  , FormatTag
79  , ConversionPolicy
80  >::type
81 make_reader( detail::filesystem::path const& path
82  , const image_read_settings< FormatTag >& settings
83  , const ConversionPolicy& cc
84  )
85 {
86  return make_reader( path.wstring()
87  , settings
88  , cc
89  );
90 }
91 
92 template <typename Device, typename FormatTag, typename ConversionPolicy>
93 inline
94 auto make_reader(
95  Device& file,
96  image_read_settings<FormatTag> const& settings,
97  ConversionPolicy const&,
98  typename std::enable_if
99  <
100  mp11::mp_and
101  <
102  detail::is_adaptable_input_device<FormatTag, Device>,
103  is_format_tag<FormatTag>
104  >::value
105  >::type* /*dummy*/ = nullptr)
106  -> typename get_reader<Device, FormatTag, ConversionPolicy>::type
107 {
108  typename get_read_device<Device, FormatTag>::type device(file);
109 
110  return
111  typename get_reader<Device, FormatTag, ConversionPolicy>::type(device, settings);
112 }
113 
114 // no image_read_settings
115 
116 template <typename String, typename FormatTag, typename ConversionPolicy>
117 inline
118 auto make_reader(
119  String const&file_name,
120  FormatTag const&,
121  ConversionPolicy const& cc,
122  typename std::enable_if
123  <
124  mp11::mp_and
125  <
126  detail::is_supported_path_spec<String>,
127  is_format_tag<FormatTag>
128  >::value
129  >::type* /*dummy*/ = nullptr)
130  -> typename get_reader<String, FormatTag, ConversionPolicy>::type
131 {
132  return make_reader(file_name, image_read_settings<FormatTag>(), cc);
133 }
134 
135 template< typename FormatTag
136  , typename ConversionPolicy
137  >
138 inline
139 typename get_reader< std::wstring
140  , FormatTag
141  , ConversionPolicy
142  >::type
143 make_reader( const std::wstring& file_name
144  , const FormatTag&
145  , const ConversionPolicy& cc
146  )
147 {
148  return make_reader( file_name
149  , image_read_settings< FormatTag >()
150  , cc
151  );
152 }
153 
154 template< typename FormatTag
155  , typename ConversionPolicy
156  >
157 inline
158 typename get_reader< std::wstring
159  , FormatTag
160  , ConversionPolicy
161  >::type
162 make_reader( detail::filesystem::path const& path
163  , const FormatTag&
164  , const ConversionPolicy& cc
165  )
166 {
167  return make_reader( path.wstring()
168  , image_read_settings< FormatTag >()
169  , cc
170  );
171 }
172 
173 template <typename Device, typename FormatTag, typename ConversionPolicy>
174 inline
175 auto make_reader(
176  Device& file,
177  FormatTag const&,
178  ConversionPolicy const& cc,
179  typename std::enable_if
180  <
181  mp11::mp_and
182  <
183  detail::is_adaptable_input_device<FormatTag, Device>,
184  is_format_tag<FormatTag>
185  >::value
186  >::type* /*dummy*/ = nullptr)
187  -> typename get_reader<Device, FormatTag, ConversionPolicy>::type
188 {
189  return make_reader(file, image_read_settings<FormatTag>(), cc);
190 }
191 
192 }} // namespace boost::gil
193 
194 #endif