4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #ifndef HH_FileHandle_
24 #define HH_FileHandle_ 1
27 #include <memory> // std::auto_ptr
28 #include "Utils/SafeBool.hh"
30 //#include "FileHandle.mpp"
31 ///////////////////////////////hh.p////////////////////////////////////////
32 #include "FileHandle.ih"
37 /** \brief Basic file handle wrapper
39 senf::FileHandle provides a simple wrapper for arbitrary file handles. It exposes only a
40 minimal interface which does \e not include reading or writing (since some filehandles are
41 not readable or writable or only using special function calls like sendto).
43 The FileHandle class provides handle/body handling and uses automatic reference
44 counting. The senf::FileHandle istance is very lightweight and should be used like a
47 \attention You should mostly pass around senf::FileHandle objects by \e value und not by
50 The FileHandle abstraction is only applicable to real filehandles. It is \e not possible to
51 wrap any provider or consumer into a filehandle like interface using this wrapper. The
52 wrapper will forward some calls directly to the underlying API without relying on virtual
53 methods. This allows important members to be inlined.
55 It is not possible to use the senf::FileHandle class directly since it does not have any
56 public constructor. The FileHandle class is however the baseclass of all handle classes of
59 \section filehandle_new Writing senf::FileHandle derived classes
61 To build a new FileHandle type you need to derive from senf::FileHandle. The derived class
62 will have to call the protocted FileHandle constructor passing a new senf::FileBody
63 instance. This instance may either be a simple senf::FileBody or a class derived from
66 \todo Add public default constructor to allow declaration of (empty) senf::FileHandle
70 : public SafeBool<FileHandle>
73 ///////////////////////////////////////////////////////////////////////////
76 ///////////////////////////////////////////////////////////////////////////
77 ///\name Structors and default members
80 // protected default constructor
81 // default copy constructor
82 // default copy assignment
85 // no conversion constructors
88 ///////////////////////////////////////////////////////////////////////////
90 void close(); ///< Close filehandle
91 /**< \throws senf::SystemException */
92 void terminate(); ///< Close filehandle ignoring error conditions
94 bool readable() const; ///< Check, wether a read on the handle would not block
95 ///< (ignoring blocking state)
96 void waitReadable() const; ///< Wait, until read on the handle would not block (ignoring
98 bool writeable() const; ///< Check, wether a write on the handle would not block
99 ///< (ignoring blocking state)
100 void waitWriteable() const; ///< Wait, until a write on the handle would not block
101 ///< (ignoring blocking state)
103 bool blocking() const; ///< Return current blocking state
104 void blocking(bool status); ///< Set blocking state
106 bool eof() const; ///< Check EOF condition
107 /**< Depending on the socket type, this might never return \p
109 bool valid() const; ///< Check filehandle validity
110 /**< Any operation besides valid() will fail on an invalid
113 bool boolean_test() const; ///< Short for valid() && ! eof()
114 /**< This is called when using a FileHandle instance in a boolen
117 int fd() const; ///< Return the raw FileHandle
119 static FileHandle cast_static(FileHandle handle); ///< \internal
120 static FileHandle cast_dynamic(FileHandle handle); ///< \internal
123 explicit FileHandle(std::auto_ptr<FileBody> body);
124 ///< create new FileHandle instance
125 /**< The FileHandle instance will take over ownership over the
126 given FileBody instance which must have been allocated using
127 \c new. To configure the FileHandle behavior, A derived class
128 may provide any class derived from FileBody here. */
130 FileBody & body(); ///< Access body
131 FileBody const & body() const; ///< Access body in const context
132 static FileBody & body(FileHandle & handle); ///< Access body of another FileHandle instance
133 static FileBody const & body(FileHandle const & handle); ///< Access body of another
134 ///< FileHandle instance in const context
136 void fd(int fd); ///< Set raw filehandle
142 int retrieve_filehandle(FileHandle handle);
146 ///////////////////////////////hh.e////////////////////////////////////////
147 #include "FileHandle.cci"
148 //#include "FileHandle.ct"
149 //#include "FileHandle.cti"
155 // c-file-style: "senf"