4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.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.
24 \brief SocketHandle inline template implementation
27 #include "SocketHandle.ih"
30 #include "../Utils/senfassert.hh"
32 #include <boost/lexical_cast.hpp>
33 #include "../Utils/TypeInfo.hh"
35 #define prefix_ inline
36 ///////////////////////////////cti.p///////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::SocketHandle<SPolicy>
41 template <class SPolicy>
42 prefix_ senf::SocketHandle<SPolicy>::SocketHandle()
45 template <class SPolicy>
46 template <class OtherPolicy>
47 prefix_ senf::SocketHandle<SPolicy>::SocketHandle(SocketHandle<OtherPolicy> other,
48 typename IsCompatible<OtherPolicy>::type *)
52 template <class SPolicy>
53 template <class OtherPolicy>
54 prefix_ typename senf::SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type const &
55 senf::SocketHandle<SPolicy>::operator=(SocketHandle<OtherPolicy> other)
61 template <class SPolicy>
63 senf::SocketHandle<SPolicy>::SocketHandle(std::auto_ptr<SocketBody> body)
64 : FileHandle(std::auto_ptr<FileBody>(body.release()))
67 template <class SPolicy>
68 prefix_ senf::SocketHandle<SPolicy>::SocketHandle(FileHandle other, bool isChecked)
71 SENF_ASSERT( isChecked );
72 SENF_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
75 template <class SPolicy>
76 prefix_ senf::SocketBody & senf::SocketHandle<SPolicy>::body()
78 SENF_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
79 return static_cast<SocketBody &>(FileHandle::body());
82 template <class SPolicy>
83 prefix_ senf::SocketBody const & senf::SocketHandle<SPolicy>::body()
86 SENF_ASSERT( dynamic_cast<SocketBody const *>(&FileHandle::body()) );
87 return static_cast<SocketBody const &>(FileHandle::body());
90 template <class SPolicy>
91 prefix_ senf::SocketProtocol const & senf::SocketHandle<SPolicy>::protocol()
94 return body().protocol();
97 template <class SPolicy>
98 prefix_ void senf::SocketHandle<SPolicy>::assign(FileHandle other)
100 FileHandle::operator=(other);
103 template <class SPolicy>
104 prefix_ senf::SocketHandle<SPolicy>
105 senf::SocketHandle<SPolicy>::cast_static(FileHandle handle)
107 return SocketHandle(handle,true);
110 template <class SPolicy>
111 prefix_ senf::SocketHandle<SPolicy>
112 senf::SocketHandle<SPolicy>::cast_dynamic(FileHandle handle)
114 // throws bad_cast if the body is not a SocketBody
115 SocketBody & body (dynamic_cast<SocketBody&>(FileHandle::body(handle)));
116 // throws bad_cast if the policy is not compatible (already wrapped ...)
117 SPolicy::checkBaseOf(body.protocol().policy());
118 return cast_static(handle);
121 template <class Target, class Source>
122 prefix_ Target senf::static_socket_cast(Source handle)
124 BOOST_STATIC_ASSERT((
125 boost::is_convertible<Source*,FileHandle*>::value &&
126 boost::is_convertible<Target*,FileHandle*>::value &&
127 ( boost::is_convertible<Source,Target>::value ||
128 boost::is_convertible<Target,Source>::value ) ));
129 SENF_ASSERT( check_socket_cast<Target>(handle) );
130 return Target::cast_static(handle);
133 template <class Target, class Source>
134 prefix_ Target senf::dynamic_socket_cast(Source handle)
136 // BOOST_STATIC_ASSERT((
137 // boost::is_convertible<Source*,FileHandle*>::value &&
138 // boost::is_convertible<Target*,FileHandle*>::value &&
139 // ( boost::is_convertible<Source,Target>::value ||
140 // boost::is_convertible<Target,Source>::value ) ));
142 return Target::cast_dynamic(handle);
144 SENF_WRAP_EXC(std::bad_cast)
147 template <class Target, class Source>
148 prefix_ bool senf::check_socket_cast(Source handle)
150 // BOOST_STATIC_ASSERT((
151 // boost::is_convertible<Source*,FileHandle*>::value &&
152 // boost::is_convertible<Target*,FileHandle*>::value &&
153 // ( boost::is_convertible<Source,Target>::value ||
154 // boost::is_convertible<Target,Source>::value ) ));
155 // we don't have a non-throwing variant of cast_dynamic
157 // a) since the handle is passed back by value, we cannot return
158 // something like a null handle
159 // b) it is simpler to implement cast_dynamic throwig bad_cast on
160 // failure than implementing cast_check
162 Target::cast_dynamic(handle);
164 catch (std::bad_cast const &) {
170 template <class SPolicy>
171 prefix_ void senf::SocketHandle<SPolicy>::state(SocketStateMap & map, unsigned lod)
173 // We use typeid here even though the type of *this is static
174 // (SocketHandle is not polymorphic and has no vtable). This will
175 // automatically include the SocketPolicy template parameter in
176 // the type name and therefore show the \e static policy of the
178 map["handle"] << prettyName(typeid(*this));
179 body().state(map,lod);
182 template <class SPolicy>
183 prefix_ std::string senf::SocketHandle<SPolicy>::dumpState(unsigned lod)
187 return detail::dumpState(map);
190 template <class SPolicy>
191 template <class Facet>
192 prefix_ Facet const & senf::SocketHandle<SPolicy>::facet()
196 return dynamic_cast<Facet const &>(protocol());
198 SENF_WRAP_EXC(std::bad_cast)
201 ///////////////////////////////////////////////////////////////////////////
202 // senf::ProtocolSocketBody<SProtocol>
204 template <class SProtocol>
205 prefix_ senf::ProtocolSocketBody<SProtocol>::ProtocolSocketBody(bool isServer)
206 : SocketBody(isServer)
209 template <class SProtocol>
210 prefix_ senf::ProtocolSocketBody<SProtocol>::ProtocolSocketBody(bool isServer, int fd)
211 : SocketBody(isServer, fd)
214 ///////////////////////////////////////////////////////////////////////////
216 template <class SPolicy>
217 prefix_ std::ostream & senf::operator<<(std::ostream & os, SocketHandle<SPolicy> handle)
219 os << handle.dumpState();
223 ///////////////////////////////cti.e///////////////////////////////////////
230 // c-file-style: "senf"
231 // indent-tabs-mode: nil
232 // ispell-local-dictionary: "american"
233 // compile-command: "scons -u test"
234 // comment-column: 40