Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / gil / io / device.hpp
index c357699..88dade0 100644 (file)
@@ -8,11 +8,9 @@
 #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>
@@ -63,19 +61,10 @@ public:
     /// 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
@@ -87,7 +76,7 @@ public:
         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
@@ -99,19 +88,10 @@ public:
     /// 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
@@ -123,7 +103,7 @@ public:
         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
@@ -152,8 +132,9 @@ public:
     {
         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;
     }
@@ -170,15 +151,13 @@ public:
                                         );
 
         ///@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;
     }
 
@@ -186,13 +165,15 @@ public:
     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];
 
@@ -201,7 +182,7 @@ public:
     }
 
     /// Reads 16 bit little endian integer
-    uint16_t read_uint16() throw()
+    uint16_t read_uint16()
     {
         byte_t m[2];
 
@@ -210,7 +191,7 @@ public:
     }
 
     /// Reads 32 bit little endian integer
-    uint32_t read_uint32() throw()
+    uint32_t read_uint32()
     {
         byte_t m[4];
 
@@ -223,7 +204,6 @@ public:
     std::size_t write( const T*    buf
                      , std::size_t count
                      )
-    throw()
     {
         std::size_t num_elements = fwrite( buf
                                          , buff_item<T>::size
@@ -231,7 +211,8 @@ public:
                                          , 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;
     }
 
@@ -239,20 +220,23 @@ public:
     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];
 
@@ -263,7 +247,7 @@ public:
     }
 
     /// Writes 32 bit little endian integer
-    void write_uint32( uint32_t x ) throw()
+    void write_uint32( uint32_t x )
     {
         byte_t m[4];
 
@@ -281,7 +265,7 @@ public:
                           , count
                           , whence
                           ) != 0
-                   , "file read error"
+                   , "file_stream_device: file seek error"
                    );
     }
 
@@ -290,7 +274,7 @@ public:
         long int pos = ftell( get() );
 
         io_error_if( pos == -1L
-                   , "file read error"
+                   , "file_stream_device: file position error"
                    );
 
         return pos;
@@ -310,8 +294,9 @@ public:
                                          , 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()
@@ -345,11 +330,10 @@ public:
    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()
@@ -361,8 +345,9 @@ public:
     {
         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;
     }
@@ -388,16 +373,14 @@ public:
     }
 
     /// 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];
 
@@ -406,7 +389,7 @@ public:
     }
 
     /// Reads 16 bit little endian integer
-    uint16_t read_uint16() throw()
+    uint16_t read_uint16()
     {
         byte_t m[2];
 
@@ -415,7 +398,7 @@ public:
     }
 
     /// Reads 32 bit little endian integer
-    uint32_t read_uint32() throw()
+    uint32_t read_uint32()
     {
         byte_t m[4];
 
@@ -434,7 +417,7 @@ public:
 
     void write(const byte_t*, std::size_t)
     {
-        io_error( "Bad io error." );
+        io_error( "istream_device: Bad io error." );
     }
 
     void flush() {}
@@ -458,7 +441,7 @@ public:
 
     std::size_t read(byte_t *, std::size_t)
     {
-        io_error( "Bad io error." );
+        io_error( "ostream_device: Bad io error." );
         return 0;
     }
 
@@ -485,20 +468,20 @@ public:
     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];
 
@@ -509,7 +492,7 @@ public:
     }
 
     /// Writes 32 bit little endian integer
-    void write_uint32( uint32_t x ) throw()
+    void write_uint32( uint32_t x )
     {
         byte_t m[4];
 
@@ -544,15 +527,15 @@ private:
  * 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
@@ -561,13 +544,13 @@ 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>;
 };
@@ -577,7 +560,7 @@ struct is_adaptable_input_device< FormatTag
                                 , FILE*
                                 , void
                                 >
-    : mpl::true_
+    : std::true_type
 {
     using device_type = file_stream_device<FormatTag>;
 };
@@ -589,7 +572,7 @@ template< typename FormatTag
         , typename T
         , typename D = void
         >
-struct is_read_device : mpl::false_
+struct is_read_device : std::false_type
 {};
 
 template <typename FormatTag, typename T>
@@ -599,13 +582,13 @@ struct is_read_device
     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
 {
 };
 
@@ -614,16 +597,16 @@ struct is_read_device
  * 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
@@ -632,19 +615,19 @@ 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>;
 };
@@ -657,7 +640,7 @@ template< typename FormatTag
         , typename T
         , typename D = void
         >
-struct is_write_device : mpl::false_
+struct is_write_device : std::false_type
 {};
 
 template <typename FormatTag, typename T>
@@ -667,13 +650,13 @@ struct is_write_device
     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
 {
 };
 
@@ -691,7 +674,7 @@ template< typename Device, typename FormatTag, typename Log = no_log > class dyn
 namespace detail {
 
 template< typename T >
-struct is_reader : mpl::false_
+struct is_reader : std::false_type
 {};
 
 template< typename Device
@@ -702,11 +685,11 @@ struct is_reader< reader< 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
@@ -715,11 +698,11 @@ 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
@@ -728,11 +711,11 @@ 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
@@ -741,7 +724,7 @@ template< typename Device
 struct is_dynamic_image_writer< dynamic_image_writer< Device
                                                     , FormatTag
                                                     >
-                > : mpl::true_
+                > : std::true_type
 {};
 
 } // namespace detail