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"
31 #include <senf/Utils/senfassert.hh>
32 #include <senf/Utils/TypeInfo.hh>
34 #define prefix_ inline
35 ///////////////////////////////cti.p///////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::SocketHandle<SPolicy>
40 template <class SPolicy>
41 prefix_ senf::SocketHandle<SPolicy>::SocketHandle()
44 template <class SPolicy>
45 template <class OtherPolicy>
46 prefix_ senf::SocketHandle<SPolicy>::SocketHandle(SocketHandle<OtherPolicy> other,
47 typename IsCompatible<OtherPolicy>::type *)
51 template <class SPolicy>
52 template <class OtherPolicy>
53 prefix_ typename senf::SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type const &
54 senf::SocketHandle<SPolicy>::operator=(SocketHandle<OtherPolicy> other)
60 template <class SPolicy>
62 senf::SocketHandle<SPolicy>::SocketHandle(std::auto_ptr<SocketBody> body)
63 : FileHandle(std::auto_ptr<FileBody>(body.release()))
66 template <class SPolicy>
67 prefix_ senf::SocketHandle<SPolicy>::SocketHandle(FileHandle other, bool isChecked)
70 SENF_ASSERT( isChecked, "Inrernal failure: Wrong overload called !!" );
71 SENF_ASSERT( ! valid() || dynamic_cast<SocketBody *>(&FileHandle::body()),
72 "Internal failure: Replacing or re-assigning non-empty incompatible handle");
75 template <class SPolicy>
76 prefix_ senf::SocketBody & senf::SocketHandle<SPolicy>::body()
78 SENF_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()),
79 "Internal failure: Invalid body found it's way into SocketHandle");
80 return static_cast<SocketBody &>(FileHandle::body());
83 template <class SPolicy>
84 prefix_ senf::SocketBody const & senf::SocketHandle<SPolicy>::body()
87 SENF_ASSERT( dynamic_cast<SocketBody const *>(&FileHandle::body()),
88 "Internal failure: Invalid body found it's way into SocketHandle");
89 return static_cast<SocketBody const &>(FileHandle::body());
92 template <class SPolicy>
93 prefix_ senf::SocketProtocol & senf::SocketHandle<SPolicy>::protocol()
96 return body().protocol();
99 template <class SPolicy>
100 prefix_ void senf::SocketHandle<SPolicy>::assign(FileHandle other)
102 FileHandle::operator=(other);
105 template <class SPolicy>
106 prefix_ senf::SocketHandle<SPolicy>
107 senf::SocketHandle<SPolicy>::cast_static(FileHandle handle)
109 return SocketHandle(handle,true);
112 template <class SPolicy>
113 prefix_ senf::SocketHandle<SPolicy>
114 senf::SocketHandle<SPolicy>::cast_dynamic(FileHandle handle)
116 // throws bad_cast if the body is not a SocketBody
117 SocketBody & body (dynamic_cast<SocketBody&>(FileHandle::body(handle)));
118 // throws bad_cast if the policy is not compatible (already wrapped ...)
119 SPolicy::checkBaseOf(body.protocol().policy());
120 return cast_static(handle);
123 template <class Target, class Source>
124 prefix_ Target senf::static_socket_cast(Source handle)
126 BOOST_STATIC_ASSERT((
127 boost::is_convertible<Source*,FileHandle*>::value &&
128 boost::is_convertible<Target*,FileHandle*>::value &&
129 ( boost::is_convertible<Source,Target>::value ||
130 boost::is_convertible<Target,Source>::value ) ));
131 SENF_ASSERT( check_socket_cast<Target>(handle),
132 "Invalid static_socket_cast" );
133 return Target::cast_static(handle);
136 template <class Target, class Source>
137 prefix_ Target senf::dynamic_socket_cast(Source handle)
139 // BOOST_STATIC_ASSERT((
140 // boost::is_convertible<Source*,FileHandle*>::value &&
141 // boost::is_convertible<Target*,FileHandle*>::value &&
142 // ( boost::is_convertible<Source,Target>::value ||
143 // boost::is_convertible<Target,Source>::value ) ));
145 return Target::cast_dynamic(handle);
147 SENF_WRAP_EXC(std::bad_cast)
150 template <class Target, class Source>
151 prefix_ bool senf::check_socket_cast(Source handle)
153 // BOOST_STATIC_ASSERT((
154 // boost::is_convertible<Source*,FileHandle*>::value &&
155 // boost::is_convertible<Target*,FileHandle*>::value &&
156 // ( boost::is_convertible<Source,Target>::value ||
157 // boost::is_convertible<Target,Source>::value ) ));
158 // we don't have a non-throwing variant of cast_dynamic
160 // a) since the handle is passed back by value, we cannot return
161 // something like a null handle
162 // b) it is simpler to implement cast_dynamic throwig bad_cast on
163 // failure than implementing cast_check
165 Target::cast_dynamic(handle);
167 catch (std::bad_cast const &) {
173 template <class SPolicy>
174 prefix_ void senf::SocketHandle<SPolicy>::state(SocketStateMap & map, unsigned lod)
176 // We use typeid here even though the type of *this is static
177 // (SocketHandle is not polymorphic and has no vtable). This will
178 // automatically include the SocketPolicy template parameter in
179 // the type name and therefore show the \e static policy of the
181 map["handle"] << prettyName(typeid(*this));
183 map["valid"] << "true";
184 body().state(map,lod);
186 map["valid"] << "false";
189 template <class SPolicy>
190 prefix_ std::string senf::SocketHandle<SPolicy>::dumpState(unsigned lod)
194 return detail::dumpState(map);
197 template <class SPolicy>
198 template <class Facet>
199 prefix_ Facet & senf::SocketHandle<SPolicy>::facet()
203 return dynamic_cast<Facet &>(protocol());
205 SENF_WRAP_EXC(std::bad_cast)
208 ///////////////////////////////////////////////////////////////////////////
209 // senf::ProtocolSocketBody<SProtocol>
211 template <class SProtocol>
212 prefix_ senf::ProtocolSocketBody<SProtocol>::ProtocolSocketBody(bool isServer)
213 : SocketBody(isServer)
216 template <class SProtocol>
217 prefix_ senf::ProtocolSocketBody<SProtocol>::ProtocolSocketBody(bool isServer, int fd)
218 : SocketBody(isServer, fd)
221 ///////////////////////////////////////////////////////////////////////////
223 template <class SPolicy>
224 prefix_ std::ostream & senf::operator<<(std::ostream & os, SocketHandle<SPolicy> handle)
226 os << handle.dumpState();
230 ///////////////////////////////cti.e///////////////////////////////////////
237 // c-file-style: "senf"
238 // indent-tabs-mode: nil
239 // ispell-local-dictionary: "american"
240 // compile-command: "scons -u test"
241 // comment-column: 40