Fix bug in SocketPolicy::checkBaseOf
[senf.git] / Socket / SocketHandle.hh
1 // $Id:SocketHandle.hh 218 2007-03-20 14:39:32Z tho $
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 SocketHandle public header
25  */
26
27 #ifndef HH_SocketHandle_
28 #define HH_SocketHandle_ 1
29
30 // Custom includes
31 #include <memory> // std::auto_ptr
32 #include "FileHandle.hh"
33 #include "SocketPolicy.hh"
34
35 //#include "SocketHandle.mpp"
36 #include "SocketHandle.ih"
37 ///////////////////////////////hh.p////////////////////////////////////////
38 #include "SocketHandle.ih"
39
40 namespace senf {
41
42     /// \addtogroup handle_group
43     /// @{
44
45     /** \brief basic SocketHandle supporting protocol and policy abstraction
46
47         The senf::SocketHandle class introduces the two abstraction layers of the socket
48         library. senf::SocketHandle does \e not provide socket functions it only provides the
49         infrastructure necessary to support both, the protocol and the policy interface.
50
51         senf::SocketHandle takes the socket policy as a template argument. senf::SocketHandle also
52         introduces the protocol class. However, the class has no public constructors (see the
53         derived classes senf::ProtocolClientSocketHandle and senf::ProtocolServerSocketHandle).
54
55         The most important functionality provided by senf::SocketHandle is the conversion
56         constructor. This allows to implicitly convert between compatible socket handle types as
57         specified by the socket policy. The conversion constructor is defined in such a way, that
58         only valid conversions are possible (see the implementation source for a more complete
59         discussion).
60
61         \note This class is \e not meant to be used as a base-class outside the library
62         implementation; The protected interface is for internal use only.
63
64         \todo Create a SocketHandleBase class and move some non-Policy dependent code there
65      */
66     template <class SocketPolicy>
67     class SocketHandle
68         : public FileHandle
69     {
70     public:
71         ///////////////////////////////////////////////////////////////////////////
72         // Types
73
74         typedef SocketPolicy Policy;
75
76         /** \brief Check policy compatibility
77
78             IsCompatible is a template meta-function which will check some other socket policy for
79             conversion compatibility. This check is used in the senf::SocketPolicy implementation to
80             restrict the conversion operator.
81          */
82         template <class OtherPolicy>
83         struct IsCompatible
84             : public boost::enable_if< SocketPolicyIsBaseOf<SocketPolicy,OtherPolicy>,
85                                        SocketHandle >
86         {};
87
88         ///////////////////////////////////////////////////////////////////////////
89         ///\name Structors and default members
90         ///@{
91
92         // default copy constructor
93         // default copy assignment
94         // default destructor
95
96         // conversion constructors
97
98         template <class OtherPolicy>
99         SocketHandle(SocketHandle<OtherPolicy> other,
100                      typename IsCompatible<OtherPolicy>::type * = 0);
101                                         ///< Convert from other socket handle checking policy
102                                         ///< compatibility
103
104         ///@}
105         ///////////////////////////////////////////////////////////////////////////
106
107         template <class OtherPolicy>
108         typename IsCompatible<OtherPolicy>::type const & operator=(SocketHandle<OtherPolicy> other);
109                                         ///< Assign from other socket handle checking policy
110                                         ///< compatibility
111
112         void state(SocketStateMap & map, unsigned lod=0);
113                                         ///< Inquire state information of socket handle
114                                         /**< The map argument (a string to string mapping) will be
115                                              filled with information covering the current state of
116                                              the socket. The information provided depends on the
117                                              socket protocol. The amount of information returned can
118                                              be controlled using the \p lod value.
119
120                                              See senf::SocketProtocol::state() for more information,
121                                              how the Information is generated.
122
123                                              \param map string to string mapping to be filled with
124                                                  state information
125                                              \param lod level of detail requested. The interpretation
126                                                  of this value is protocol specific
127
128                                              \implementation This member will be re-implemented in
129                                                  every derived class. This is very important since
130                                                  state() is \e not a virtual function (which we
131                                                  don't want since we don't want to add a vtable
132                                                  pointer to every handle instance). */
133         std::string dumpState(unsigned lod=0);
134                                         ///< Format complete state information as string
135                                         /**< Formats the complete state map value and returns it as
136                                              a single multi-line string.
137
138                                              param lod  level of detail requested. The interpretation
139                                                 of this value is protocol specific
140
141                                              \implementation This member will be re-implemented in
142                                                  every derived class. See the state()
143                                                  documentation. */
144
145     protected:
146         explicit SocketHandle(std::auto_ptr<SocketProtocol> protocol, bool isServer);
147                                         ///< Initialize SocketHandle providing the protocol
148                                         /**< \param protocol Protocol class of the protocol
149                                                  implemented by this socket handle
150                                              \param isServer \c true, if this SobcketHandle instance
151                                                  implements a server handle, \c false otherwise */
152         SocketHandle(FileHandle other, bool isChecked);
153                                         ///< Initialize SocketHandle from arbitrary checked
154                                         ///< FileHandle
155                                         /**< This constructor is used to support up- and downcasting
156                                              of SocketHandle instances.
157
158                                              \warning It is absolutely necessary to ensure, that the
159                                                  FileHandle passed in is \e really a SocketHandle
160                                                  holding a SocketBody (and not a simple FileBody)
161                                                  instance. Additionally. the SocketPolicy absolutely
162                                                  must be compatible.
163
164                                              \param other FileHandle to assign
165                                              \param isChecked has to be \c true
166
167                                              \todo Answer, why the heck I need the \c isChecked
168                                                  parameter ??
169                                         */
170
171         SocketBody & body();            ///< Access socket body
172                                         /**< This member replaces the corresponding FileHandle
173                                              member and returns an appropriately cast body
174                                              reference */
175         SocketBody const & body() const; ///< Access socket body in const context
176                                         /**< This member replaces the corresponding FileHandle
177                                              member and returns an appropriately cast body
178                                              reference */
179         SocketProtocol const & protocol() const;
180                                         ///< Access protocol class
181
182         void assign(FileHandle other);  /**< \internal */
183
184     public:
185         static SocketHandle cast_static(FileHandle handle);
186                                         /**< \internal */
187         static SocketHandle cast_dynamic(FileHandle handle);
188                                         /**< \internal */
189
190     private:
191
192     };
193
194     /** \brief Write stream status dump to output stream
195
196         Write senf::SocketHandle::dumpState() to \c os
197
198         \related senf::SocketHandle
199      */
200     template <class Policy>
201     std::ostream & operator<<(std::ostream & os, SocketHandle<Policy> handle);
202
203     /** \brief static socket (down-)cast
204
205         This function is like \c static_cast but for socket handles. It allows to downcast any
206         FileHandle to any SocketHandle (and its derived types). static_socket_cast will \e not check
207         the validity of the cast, it will assume, that the cast is valid.
208
209         The function will however check, that the cast is possible. Casting between (at compile
210         time) known incompatible types (like casting a SocketHandle with a communication policy of
211         ConnectedCommunicationPolicy to a SocketHandle with UnconnectedCommunicationPolicy will fail
212         at compile time).
213
214         \warning
215         If the type you cast to is not really a compatible socket handle type you will get undefined
216         behavior, probably your program will crash (You will get an assertion in debug builds).
217
218         \related senf::SocketHandle
219      */
220     template <class Target, class Source>
221     Target static_socket_cast(Source handle);
222
223     /** \brief dynamic socket (down-)cast
224
225         This function is like \c dynamic_cast but for socket handles. It is a runtime typechecked
226         version of static_socket_cast.
227
228         \throws std::bad_cast You have tried to perform an invalid down- or crosscast.
229
230         \related senf::SocketHandle
231      */
232     template <class Target, class Source>
233     Target dynamic_socket_cast(Source handle);
234
235     /** \brief dynamically check cast validity
236
237         This function will check, whether the given cast is valid. This is the same as checking, that
238         dynamic_socket_cast does not throw.
239
240         This member is needed, since there is no 'null' SocketHandle (comparable to a null pointer)
241         which could be returned by a non-throwing variant of dynamic_socket_cast.
242
243         \related senf::SocketHandle
244      */
245     template <class Target, class Source>
246     bool check_socket_cast(Source handle);
247
248     /// @}
249 }
250
251 ///////////////////////////////hh.e////////////////////////////////////////
252 #include "SocketHandle.cci"
253 #include "SocketHandle.ct"
254 #include "SocketHandle.cti"
255 #endif
256
257 \f
258 // Local Variables:
259 // mode: c++
260 // fill-column: 100
261 // c-file-style: "senf"
262 // indent-tabs-mode: nil
263 // ispell-local-dictionary: "american"
264 // End: