f5786355ec72cd15c2ea17fa2e206ab5e09009f5
[senf.git] / Socket / FileHandle.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief FileHandle public header
25  */
26
27 /** \defgroup handle_group The Handle Hierarchy
28
29     <div class="diamap" name="FhHierarchy">
30     <span coords="233,47,438,89">\ref SocketHandle</span>
31     <span coords="32,126,281,168">\ref ClientSocketHandle</span>
32     <span coords="0,187,326,229">\ref ProtocolClientSocketHandle</span>
33     <span coords="350,187,684,229">\ref ProtocolServerSocketHandle</span>
34     <span coords="243,0,343,28">\ref FileHandle</span>
35     <span coords="382,126,638,168">\ref ServerSocketHandle</span>
36     </div>
37     \htmlonly <img src="FhHierarchy.png" border="0" alt="FhHierarchy" usemap="#FhHierarchy"> \endhtmlonly
38
39     The senf::FileHandle class is the base of a hierarchy of socket handle classes (realized as
40     templates). These classes provide an interface to the complete socket API. While going down the
41     inheritance hierarchy, the interface will be more and more complete.
42
43     The most complete interface is provided by senf::ProtocolClientSocketHandle and
44     senf::ProtocolServerSocketHandle. The template Arguments specifies the Protocol class of the
45     underlying socket type. These are the \e only classes having public constructors and are
46     therefore the only classes, which may be created by the library user. You will normally use
47     these classes by naming a specific socket typedef (e.g. senf::TCPv4ClientSocketHandle).
48
49     However, to aid writing flexible and generic code, the socket library provides the
50     senf::ClientSocketHandle and senf::ServerSocketHandle class templates. These templates implement
51     a family of closely related classes based on the specification of the socket policy. This policy
52     specification may be \e incomplete (see below). Instances of
53     senf::ClientSocketHandle/senf::ServerSocketHandle can be assigned and converted to different
54     ClientSocketHandle/ServerSocketHandle types as long as the policy specifications are compatible.
55
56     \attention It is very important, to (almost) always pass the socket handle <em>by
57     value</em>. The socket handle is a very lightweight class and designed to be used like an
58     ordinary built-in type. This is very important in combination with the policy interface.
59
60     \note The FileHandle hierarchy below the SocketHandle template is \e not meant to be user
61     extensible. To add new socket types, you should introduce new protocol and/or policy classes,
62     the SocketHandle classes should not be changed.
63  */
64
65 #ifndef HH_FileHandle_
66 #define HH_FileHandle_ 1
67
68 // Custom includes
69 #include <memory> // std::auto_ptr
70 #include "../Utils/safe_bool.hh"
71
72 //#include "FileHandle.mpp"
73 ///////////////////////////////hh.p////////////////////////////////////////
74 #include "FileHandle.ih"
75
76 namespace senf {
77
78     /// \addtogroup handle_group
79     /// @{
80
81     /** \brief Basic file handle wrapper
82
83         senf::FileHandle provides a simple wrapper for arbitrary file handles. It exposes only a
84         minimal interface which does \e not include reading or writing (since some filehandles are
85         not readable or writable or only using special function calls like sendto).
86
87         The FileHandle class provides handle/body handling and uses automatic reference
88         counting. The senf::FileHandle instance is very lightweight and should be used like a
89         built-in type.
90
91         \attention You should mostly pass around senf::FileHandle objects by \e value and not by
92         reference.
93
94         The FileHandle abstraction is only applicable to real filehandles. It is \e not possible to
95         wrap any provider or consumer into a filehandle like interface using this wrapper. The
96         wrapper will forward some calls directly to the underlying API without relying on virtual
97         methods. This allows important members to be inlined.
98
99         It is not possible to use the senf::FileHandle class directly since it does not have any
100         public constructor. The FileHandle class is however the baseclass of all handle classes of
101         the socket library.
102
103         \section filehandle_new Writing senf::FileHandle derived classes
104
105         To build a new FileHandle type you need to derive from senf::FileHandle. The derived class
106         will have to call the protected FileHandle constructor passing a new senf::FileBody
107         instance. This instance may either be a simple senf::FileBody or a class derived from
108         senf::FileBody.
109      */
110     class FileHandle
111         : public safe_bool<FileHandle>
112     {
113     public:
114         ///////////////////////////////////////////////////////////////////////////
115         // Types
116
117         ///////////////////////////////////////////////////////////////////////////
118         ///\name Structors and default members
119         ///@{
120
121         FileHandle();
122         ~FileHandle();
123
124         // my default constructor
125         // default copy constructor
126         // default copy assignment
127         // default destructor
128
129         // no conversion constructors
130
131         ///@}
132         ///////////////////////////////////////////////////////////////////////////
133
134         void close();                ///< Close filehandle
135                                      /**< \throws senf::SystemException */
136         void terminate();            ///< Close filehandle ignoring error conditions
137
138         bool readable() const;       ///< Check, whether a read on the handle would not block
139                                      ///< (ignoring blocking state)
140         void waitReadable() const;   ///< Wait, until read on the handle would not block (ignoring
141                                      ///< blocking state)
142         bool writeable() const;      ///< Check, whether a write on the handle would not block
143                                      ///< (ignoring blocking state)
144         void waitWriteable() const;  ///< Wait, until a write on the handle would not block
145                                      ///< (ignoring blocking state)
146
147         bool blocking() const;       ///< Return current blocking state
148         void blocking(bool status);  ///< Set blocking state
149
150         bool eof() const;            ///< Check EOF condition
151                                      /**< Depending on the socket type, this might never return \p
152                                         true.
153
154                                         This member is somewhat problematic performance wise if
155                                         called frequently since it relies on virtual
156                                         functions. However, since the eof() handling is extremely
157                                         protocol dependent, a policy based implementation does not
158                                         seam feasible. */
159         bool valid() const;          ///< Check filehandle validity
160                                      /**< Any operation besides valid() will fail on an invalid
161                                         FileHandle */
162
163         bool boolean_test() const;  ///< Short for valid() && ! eof()
164                                     /**< This is called when using a FileHandle instance in a boolean
165                                        context
166
167                                        See the performance comments for the eof() member */
168
169         int fd() const;             ///< Return the raw FileHandle
170
171         static FileHandle cast_static(FileHandle handle);  /**< \internal */
172         static FileHandle cast_dynamic(FileHandle handle); /**< \internal */
173
174     protected:
175         explicit FileHandle(std::auto_ptr<FileBody> body);
176                                     ///< create new FileHandle instance
177                                     /**< The FileHandle instance will take over ownership over the
178                                        given FileBody instance which must have been allocated using
179                                        \c new. To configure the FileHandle behavior, A derived class
180                                        may provide any class derived from FileBody here. */
181
182         explicit FileHandle(FileBody::ptr body);
183
184         FileBody & body();          ///< Access body
185         FileBody const & body() const; ///< Access body in const context
186         static FileBody & body(FileHandle & handle); ///< Access body of another FileHandle instance
187         static FileBody const & body(FileHandle const & handle); ///< Access body of another
188                                     ///< FileHandle instance in const context
189
190         void fd(int fd);            ///< Set raw filehandle
191
192     private:
193         FileBody::ptr body_;
194
195         friend class FileBody;
196     };
197
198     /** \brief Adapt FileHandle to senf::Scheduler
199         \related senf::FileHandle
200
201         \internal
202
203         This function will be called by the Scheduler to retrieve the file descriptor of the
204         FileHandle.
205      */
206     int retrieve_filehandle(FileHandle handle);
207
208     /// @}
209
210 }
211
212 ///////////////////////////////hh.e////////////////////////////////////////
213 #include "FileHandle.cci"
214 //#include "FileHandle.ct"
215 //#include "FileHandle.cti"
216 #endif
217
218 \f
219 // Local Variables:
220 // mode: c++
221 // fill-column: 100
222 // c-file-style: "senf"
223 // indent-tabs-mode: nil
224 // ispell-local-dictionary: "american"
225 // compile-command: "scons -u test"
226 // comment-column: 40
227 // End: