fixup Fix to build with libxml 2.12.x (fixes #505)
[platform/upstream/libzypp.git] / zypp / base / Errno.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/Errno.h
10  *
11 */
12 #ifndef ZYPP_BASE_ERRNO_H
13 #define ZYPP_BASE_ERRNO_H
14
15 #include <cerrno>
16 #include <iosfwd>
17
18 #include "zypp/base/String.h"
19
20 ///////////////////////////////////////////////////////////////////
21 namespace zypp
22 { /////////////////////////////////////////////////////////////////
23
24   /** Convenience \c errno wrapper. */
25   class Errno
26   {
27     public:
28       /** Default ctor: \c errno */
29       Errno() : _errno( errno ) {}
30
31       /** Ctor set to \c errno if error condition, else \c 0.
32        * \code
33        *  int ret = ::write( fd, buffer, size );
34        *  DBG << "write returns: " << Errno( ret != size ) << end;
35        *  // on success:    "write returns: [0-Success]"
36        *  // on error e.g.: "write returns: [11-Resource temporarily unavailable]"
37        * \endcode
38        */
39       Errno( bool error_r ) : _errno( error_r ? errno : 0 ) {}
40
41       /** Ctor taking an explicit errno value. */
42       Errno( int errno_r ) : _errno( errno_r ) {}
43
44     public:
45       /** Return the stored errno. */
46       int get() const { return _errno; }
47
48       /** Allow implicit conversion to \c int. */
49       operator int() const { return get(); }
50
51       /** Return human readable error string. */
52       std::string asString() const { return str::form( "[%d-%s]", _errno, ::strerror(_errno) ); }
53
54     private:
55       int _errno;
56   };
57
58   /** \relates Errno Stream output */
59   inline std::ostream & operator<<( std::ostream & str, const Errno & obj )
60   { return str << obj.asString(); }
61
62   /////////////////////////////////////////////////////////////////
63 } // namespace zypp
64 ///////////////////////////////////////////////////////////////////
65 #endif // ZYPP_BASE_ERRNO_H