Imported Upstream version 17.0.0
[platform/upstream/libzypp.git] / zypp / base / Fd.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/Fd.h
10  *
11 */
12 #ifndef ZYPP_BASE_FD_H
13 #define ZYPP_BASE_FD_H
14
15 #include "zypp/Pathname.h"
16
17 ///////////////////////////////////////////////////////////////////
18 namespace zypp
19 { /////////////////////////////////////////////////////////////////
20   ///////////////////////////////////////////////////////////////////
21   namespace base
22   { /////////////////////////////////////////////////////////////////
23
24     ///////////////////////////////////////////////////////////////////
25     //
26     //  CLASS NAME : Fd
27     //
28     /** Assert \c close called on open filedescriptor.
29      * \code
30      * ...
31      * scoped_ptr<Fd> fd; // calls close when going out of scope
32      * try {
33      *   fd.reset( new Fd( "/some/file" ) );
34      * } catch ( ... ) {
35      *   // open failed.
36      * }
37      * read( fd->fd(), ... ),
38      * \endcode
39      *
40      * \ingroup g_RAII
41      * \todo It's dumb. Openflags and more related functions (read/write..)
42      * could be added.
43     */
44     class Fd
45     {
46       NON_COPYABLE( Fd );
47     public:
48       /** Ctor opens file.
49        * \throw EXCEPTION If open fails.
50       */
51       Fd( const Pathname & file_r, int open_flags, mode_t mode = 0 );
52
53       /** Move ctor */
54       Fd( Fd && rhs )
55       : m_fd( -1 )
56       { std::swap( m_fd, rhs.m_fd ); }
57
58       /** Move assign */
59       Fd & operator=( Fd && rhs )
60       { if ( this != &rhs ) std::swap( m_fd, rhs.m_fd ); return *this; }
61
62       /** Dtor closes file. */
63       ~Fd()
64       { close(); }
65
66       /** Explicitly close the file. */
67       void close();
68
69       /** Test for valid filedescriptor. */
70       bool isOpen() const
71       { return m_fd != -1; }
72
73       /** Return the filedescriptor. */
74       int fd() const
75       { return m_fd; }
76
77       /** Return the filedescriptor. */
78       int operator*() const
79       { return m_fd; }
80
81     private:
82       /** The filedescriptor. */
83       int m_fd;
84     };
85     ///////////////////////////////////////////////////////////////////
86
87     /////////////////////////////////////////////////////////////////
88   } // namespace base
89   ///////////////////////////////////////////////////////////////////
90   /////////////////////////////////////////////////////////////////
91 } // namespace zypp
92 ///////////////////////////////////////////////////////////////////
93 #endif // ZYPP_BASE_FD_H