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 <boost/lexical_cast.hpp>
32 #include "../Utils/TypeInfo.hh"
34 #define prefix_ inline
35 ///////////////////////////////cti.p///////////////////////////////////////
37 template <class SocketPolicy>
38 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle()
41 template <class SocketPolicy>
42 template <class OtherPolicy>
43 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle(SocketHandle<OtherPolicy> other,
44 typename IsCompatible<OtherPolicy>::type *)
48 template <class SocketPolicy>
49 template <class OtherPolicy>
50 prefix_ typename senf::SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const &
51 senf::SocketHandle<SocketPolicy>::operator=(SocketHandle<OtherPolicy> other)
57 template <class SocketPolicy>
59 senf::SocketHandle<SocketPolicy>::SocketHandle(std::auto_ptr<SocketProtocol> protocol,
61 : FileHandle(std::auto_ptr<FileBody>(new SocketBody(protocol,isServer)))
64 template <class SocketPolicy>
65 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle(FileHandle other, bool isChecked)
68 BOOST_ASSERT( isChecked );
69 BOOST_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
72 template <class SocketPolicy>
73 prefix_ senf::SocketBody & senf::SocketHandle<SocketPolicy>::body()
75 BOOST_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
76 return static_cast<SocketBody &>(FileHandle::body());
79 template <class SocketPolicy>
80 prefix_ senf::SocketBody const & senf::SocketHandle<SocketPolicy>::body()
83 BOOST_ASSERT( dynamic_cast<SocketBody const *>(&FileHandle::body()) );
84 return static_cast<SocketBody const &>(FileHandle::body());
87 template <class SocketPolicy>
88 prefix_ senf::SocketProtocol const & senf::SocketHandle<SocketPolicy>::protocol()
91 return body().protocol();
94 template <class SocketPolicy>
95 prefix_ void senf::SocketHandle<SocketPolicy>::assign(FileHandle other)
97 FileHandle::operator=(other);
100 template <class SocketPolicy>
101 prefix_ senf::SocketHandle<SocketPolicy>
102 senf::SocketHandle<SocketPolicy>::cast_static(FileHandle handle)
104 return SocketHandle(handle,true);
107 template <class SocketPolicy>
108 prefix_ senf::SocketHandle<SocketPolicy>
109 senf::SocketHandle<SocketPolicy>::cast_dynamic(FileHandle handle)
111 // throws bad_cast if the body is not a SocketBody
112 SocketBody & body (dynamic_cast<SocketBody&>(FileHandle::body(handle)));
113 // throws bad_cast if the policy is not compatible
114 SocketPolicy::checkBaseOf(body.protocol().policy());
115 return cast_static(handle);
118 template <class Target, class Source>
119 prefix_ Target senf::static_socket_cast(Source handle)
121 BOOST_STATIC_ASSERT((
122 boost::is_convertible<Source*,FileHandle*>::value &&
123 boost::is_convertible<Target*,FileHandle*>::value &&
124 ( boost::is_convertible<Source,Target>::value ||
125 boost::is_convertible<Target,Source>::value ) ));
126 BOOST_ASSERT( check_socket_cast<Target>(handle) );
127 return Target::cast_static(handle);
130 template <class Target, class Source>
131 prefix_ Target senf::dynamic_socket_cast(Source handle)
133 BOOST_STATIC_ASSERT((
134 boost::is_convertible<Source*,FileHandle*>::value &&
135 boost::is_convertible<Target*,FileHandle*>::value &&
136 ( boost::is_convertible<Source,Target>::value ||
137 boost::is_convertible<Target,Source>::value ) ));
138 return Target::cast_dynamic(handle);
141 template <class Target, class Source>
142 prefix_ bool senf::check_socket_cast(Source handle)
144 BOOST_STATIC_ASSERT((
145 boost::is_convertible<Source*,FileHandle*>::value &&
146 boost::is_convertible<Target*,FileHandle*>::value &&
147 ( boost::is_convertible<Source,Target>::value ||
148 boost::is_convertible<Target,Source>::value ) ));
149 // we don't have a non-throwing variant of cast_dynamic
151 // a) since the handle is passed back by value, we cannot return
152 // something like a null handle
153 // b) it is simpler to implement cast_dynamic throwig bad_cast on
154 // failure than implementing cast_check
156 Target::cast_dynamic(handle);
158 catch (std::bad_cast const &) {
164 template <class SocketPolicy>
165 prefix_ void senf::SocketHandle<SocketPolicy>::state(SocketStateMap & map, unsigned lod)
167 // We use typeid here even though the type of *this is static
168 // (SocketHandle is not polymorphic and has no vtable). This will
169 // automatically include the SocketPolicy template parameter in
170 // the type name and therefore show the \e static policy of the
172 map["handle"] << prettyName(typeid(*this));
173 body().state(map,lod);
176 template <class SocketPolicy>
177 prefix_ std::string senf::SocketHandle<SocketPolicy>::dumpState(unsigned lod)
181 return detail::dumpState(map);
184 ///////////////////////////////cti.e///////////////////////////////////////
191 // c-file-style: "senf"
192 // indent-tabs-mode: nil
193 // ispell-local-dictionary: "american"
194 // compile-command: "scons -u test"
195 // comment-column: 40