#ifndef BOOST_GIL_IO_DEVICE_HPP
#define BOOST_GIL_IO_DEVICE_HPP
+#include <boost/gil/detail/mp11.hpp>
#include <boost/gil/io/base.hpp>
-#include <boost/assert.hpp>
-#include <boost/core/ignore_unused.hpp>
-
#include <cstdio>
#include <memory>
#include <type_traits>
/// Constructor
///
file_stream_device( const std::string& file_name
- , read_tag = read_tag()
+ , read_tag tag = read_tag()
)
- {
- FILE* file = nullptr;
-
- io_error_if( ( file = fopen( file_name.c_str(), "rb" )) == nullptr
- , "file_stream_device: failed to open file"
- );
-
- _file = file_ptr_t( file
- , file_deleter
- );
- }
+ : file_stream_device(file_name.c_str(), tag)
+ {}
///
/// Constructor
FILE* file = nullptr;
io_error_if( ( file = fopen( file_name, "rb" )) == nullptr
- , "file_stream_device: failed to open file"
+ , "file_stream_device: failed to open file for reading"
);
_file = file_ptr_t( file
/// Constructor
///
file_stream_device( const std::string& file_name
- , write_tag
+ , write_tag tag
)
- {
- FILE* file = nullptr;
-
- io_error_if( ( file = fopen( file_name.c_str(), "wb" )) == nullptr
- , "file_stream_device: failed to open file"
- );
-
- _file = file_ptr_t( file
- , file_deleter
- );
- }
+ : file_stream_device(file_name.c_str(), tag)
+ {}
///
/// Constructor
FILE* file = nullptr;
io_error_if( ( file = fopen( file_name, "wb" )) == nullptr
- , "file_stream_device: failed to open file"
+ , "file_stream_device: failed to open file for writing"
);
_file = file_ptr_t( file
{
int ch;
- if(( ch = std::getc( get() )) == EOF )
- io_error( "file_stream_device: unexpected EOF" );
+ io_error_if( ( ch = std::getc( get() )) == EOF
+ , "file_stream_device: unexpected EOF"
+ );
return ( char ) ch;
}
);
///@todo: add compiler symbol to turn error checking on and off.
- if(ferror( get() ))
- {
- BOOST_ASSERT(false);
- }
+ io_error_if( ferror( get() )
+ , "file_stream_device: file read error"
+ );
//libjpeg sometimes reads blocks in 4096 bytes even when the file is smaller than that.
- //assert( num_elements == count );
- BOOST_ASSERT(num_elements > 0 );
-
+ //return value indicates how much was actually read
+ //returning less than "count" is not an error
return num_elements;
}
template< typename T
, int N
>
- std::size_t read( T (&buf)[N] )
+ void read( T (&buf)[N] )
{
- return read( buf, N );
+ io_error_if( read( buf, N ) < N
+ , "file_stream_device: file read error"
+ );
}
/// Reads byte
- uint8_t read_uint8() throw()
+ uint8_t read_uint8()
{
byte_t m[1];
}
/// Reads 16 bit little endian integer
- uint16_t read_uint16() throw()
+ uint16_t read_uint16()
{
byte_t m[2];
}
/// Reads 32 bit little endian integer
- uint32_t read_uint32() throw()
+ uint32_t read_uint32()
{
byte_t m[4];
std::size_t write( const T* buf
, std::size_t count
)
- throw()
{
std::size_t num_elements = fwrite( buf
, buff_item<T>::size
, get()
);
- BOOST_ASSERT(num_elements == count);
+ //return value indicates how much was actually written
+ //returning less than "count" is not an error
return num_elements;
}
template < typename T
, std::size_t N
>
- std::size_t write( const T (&buf)[N] ) throw()
+ void write( const T (&buf)[N] )
{
- return write( buf, N );
+ io_error_if( write( buf, N ) < N
+ , "file_stream_device: file write error"
+ );
+ return ;
}
/// Writes byte
- void write_uint8( uint8_t x ) throw()
+ void write_uint8( uint8_t x )
{
byte_t m[1] = { x };
write(m);
}
/// Writes 16 bit little endian integer
- void write_uint16( uint16_t x ) throw()
+ void write_uint16( uint16_t x )
{
byte_t m[2];
}
/// Writes 32 bit little endian integer
- void write_uint32( uint32_t x ) throw()
+ void write_uint32( uint32_t x )
{
byte_t m[4];
, count
, whence
) != 0
- , "file read error"
+ , "file_stream_device: file seek error"
);
}
long int pos = ftell( get() );
io_error_if( pos == -1L
- , "file read error"
+ , "file_stream_device: file position error"
);
return pos;
, get()
);
- BOOST_ASSERT(num_elements == line.size());
- boost::ignore_unused(num_elements);
+ io_error_if( num_elements < line.size()
+ , "file_stream_device: line print error"
+ );
}
int error()
istream_device( std::istream& in )
: _in( in )
{
- if (!in)
- {
- // does the file exists?
- io_error("Stream is not valid.");
- }
+ // does the file exists?
+ io_error_if( !in
+ , "istream_device: Stream is not valid."
+ );
}
int getc_unchecked()
{
int ch;
- if(( ch = _in.get() ) == EOF )
- io_error( "file_stream_device: unexpected EOF" );
+ io_error_if( ( ch = _in.get() ) == EOF
+ , "istream_device: unexpected EOF"
+ );
return ( char ) ch;
}
}
/// Reads array
- template< typename T
- , int N
- >
- size_t read( T (&buf)[N] )
+ template<typename T, int N>
+ void read(T (&buf)[N])
{
- return read( buf, N );
+ read(buf, N);
}
/// Reads byte
- uint8_t read_uint8() throw()
+ uint8_t read_uint8()
{
byte_t m[1];
}
/// Reads 16 bit little endian integer
- uint16_t read_uint16() throw()
+ uint16_t read_uint16()
{
byte_t m[2];
}
/// Reads 32 bit little endian integer
- uint32_t read_uint32() throw()
+ uint32_t read_uint32()
{
byte_t m[4];
void write(const byte_t*, std::size_t)
{
- io_error( "Bad io error." );
+ io_error( "istream_device: Bad io error." );
}
void flush() {}
std::size_t read(byte_t *, std::size_t)
{
- io_error( "Bad io error." );
+ io_error( "ostream_device: Bad io error." );
return 0;
}
template < typename T
, std::size_t N
>
- void write( const T (&buf)[N] ) throw()
+ void write( const T (&buf)[N] )
{
write( buf, N );
}
/// Writes byte
- void write_uint8( uint8_t x ) throw()
+ void write_uint8( uint8_t x )
{
byte_t m[1] = { x };
write(m);
}
/// Writes 16 bit little endian integer
- void write_uint16( uint16_t x ) throw()
+ void write_uint16( uint16_t x )
{
byte_t m[2];
}
/// Writes 32 bit little endian integer
- void write_uint32( uint32_t x ) throw()
+ void write_uint32( uint32_t x )
{
byte_t m[4];
* Metafunction to detect input devices.
* Should be replaced by an external facility in the future.
*/
-template< typename IODevice > struct is_input_device : mpl::false_{};
-template< typename FormatTag > struct is_input_device< file_stream_device< FormatTag > > : mpl::true_{};
-template< typename FormatTag > struct is_input_device< istream_device< FormatTag > > : mpl::true_{};
+template< typename IODevice > struct is_input_device : std::false_type{};
+template< typename FormatTag > struct is_input_device< file_stream_device< FormatTag > > : std::true_type{};
+template< typename FormatTag > struct is_input_device< istream_device< FormatTag > > : std::true_type{};
template< typename FormatTag
, typename T
, typename D = void
>
-struct is_adaptable_input_device : mpl::false_{};
+struct is_adaptable_input_device : std::false_type{};
template <typename FormatTag, typename T>
struct is_adaptable_input_device
T,
typename std::enable_if
<
- mpl::or_
+ mp11::mp_or
<
- is_base_and_derived<std::istream, T>,
- is_same<std::istream, T>
+ std::is_base_of<std::istream, T>,
+ std::is_same<std::istream, T>
>::value
>::type
-> : mpl::true_
+> : std::true_type
{
using device_type = istream_device<FormatTag>;
};
, FILE*
, void
>
- : mpl::true_
+ : std::true_type
{
using device_type = file_stream_device<FormatTag>;
};
, typename T
, typename D = void
>
-struct is_read_device : mpl::false_
+struct is_read_device : std::false_type
{};
template <typename FormatTag, typename T>
T,
typename std::enable_if
<
- mpl::or_
+ mp11::mp_or
<
is_input_device<FormatTag>,
is_adaptable_input_device<FormatTag, T>
>::value
>::type
-> : mpl::true_
+> : std::true_type
{
};
* Metafunction to detect output devices.
* Should be replaced by an external facility in the future.
*/
-template<typename IODevice> struct is_output_device : mpl::false_{};
+template<typename IODevice> struct is_output_device : std::false_type{};
-template< typename FormatTag > struct is_output_device< file_stream_device< FormatTag > > : mpl::true_{};
-template< typename FormatTag > struct is_output_device< ostream_device < FormatTag > > : mpl::true_{};
+template< typename FormatTag > struct is_output_device< file_stream_device< FormatTag > > : std::true_type{};
+template< typename FormatTag > struct is_output_device< ostream_device < FormatTag > > : std::true_type{};
template< typename FormatTag
, typename IODevice
, typename D = void
>
-struct is_adaptable_output_device : mpl::false_ {};
+struct is_adaptable_output_device : std::false_type {};
template <typename FormatTag, typename T>
struct is_adaptable_output_device
T,
typename std::enable_if
<
- mpl::or_
+ mp11::mp_or
<
- is_base_and_derived<std::ostream, T>,
- is_same<std::ostream, T>
+ std::is_base_of<std::ostream, T>,
+ std::is_same<std::ostream, T>
>::value
>::type
-> : mpl::true_
+> : std::true_type
{
using device_type = ostream_device<FormatTag>;
};
template<typename FormatTag> struct is_adaptable_output_device<FormatTag,FILE*,void>
- : mpl::true_
+ : std::true_type
{
using device_type = file_stream_device<FormatTag>;
};
, typename T
, typename D = void
>
-struct is_write_device : mpl::false_
+struct is_write_device : std::false_type
{};
template <typename FormatTag, typename T>
T,
typename std::enable_if
<
- mpl::or_
+ mp11::mp_or
<
is_output_device<FormatTag>,
is_adaptable_output_device<FormatTag, T>
>::value
>::type
-> : mpl::true_
+> : std::true_type
{
};
namespace detail {
template< typename T >
-struct is_reader : mpl::false_
+struct is_reader : std::false_type
{};
template< typename Device
, FormatTag
, ConversionPolicy
>
- > : mpl::true_
+ > : std::true_type
{};
template< typename T >
-struct is_dynamic_image_reader : mpl::false_
+struct is_dynamic_image_reader : std::false_type
{};
template< typename Device
struct is_dynamic_image_reader< dynamic_image_reader< Device
, FormatTag
>
- > : mpl::true_
+ > : std::true_type
{};
template< typename T >
-struct is_writer : mpl::false_
+struct is_writer : std::false_type
{};
template< typename Device
struct is_writer< writer< Device
, FormatTag
>
- > : mpl::true_
+ > : std::true_type
{};
template< typename T >
-struct is_dynamic_image_writer : mpl::false_
+struct is_dynamic_image_writer : std::false_type
{};
template< typename Device
struct is_dynamic_image_writer< dynamic_image_writer< Device
, FormatTag
>
- > : mpl::true_
+ > : std::true_type
{};
} // namespace detail