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 template <class SocketPolicy>
39 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle()
42 template <class SocketPolicy>
43 template <class OtherPolicy>
44 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle(SocketHandle<OtherPolicy> other,
45 typename IsCompatible<OtherPolicy>::type *)
49 template <class SocketPolicy>
50 template <class OtherPolicy>
51 prefix_ typename senf::SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const &
52 senf::SocketHandle<SocketPolicy>::operator=(SocketHandle<OtherPolicy> other)
58 template <class SocketPolicy>
60 senf::SocketHandle<SocketPolicy>::SocketHandle(std::auto_ptr<SocketProtocol> protocol,
62 : FileHandle(std::auto_ptr<FileBody>(new SocketBody(protocol,isServer)))
65 template <class SocketPolicy>
66 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle(FileHandle other, bool isChecked)
69 SENF_ASSERT( isChecked );
70 SENF_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
73 template <class SocketPolicy>
74 prefix_ senf::SocketBody & senf::SocketHandle<SocketPolicy>::body()
76 SENF_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
77 return static_cast<SocketBody &>(FileHandle::body());
80 template <class SocketPolicy>
81 prefix_ senf::SocketBody const & senf::SocketHandle<SocketPolicy>::body()
84 SENF_ASSERT( dynamic_cast<SocketBody const *>(&FileHandle::body()) );
85 return static_cast<SocketBody const &>(FileHandle::body());
88 template <class SocketPolicy>
89 prefix_ senf::SocketProtocol const & senf::SocketHandle<SocketPolicy>::protocol()
92 return body().protocol();
95 template <class SocketPolicy>
96 prefix_ void senf::SocketHandle<SocketPolicy>::assign(FileHandle other)
98 FileHandle::operator=(other);
101 template <class SocketPolicy>
102 prefix_ senf::SocketHandle<SocketPolicy>
103 senf::SocketHandle<SocketPolicy>::cast_static(FileHandle handle)
105 return SocketHandle(handle,true);
108 template <class SocketPolicy>
109 prefix_ senf::SocketHandle<SocketPolicy>
110 senf::SocketHandle<SocketPolicy>::cast_dynamic(FileHandle handle)
112 // throws bad_cast if the body is not a SocketBody
113 SocketBody & body (dynamic_cast<SocketBody&>(FileHandle::body(handle)));
114 // throws bad_cast if the policy is not compatible
115 SocketPolicy::checkBaseOf(body.protocol().policy());
116 return cast_static(handle);
119 template <class Target, class Source>
120 prefix_ Target senf::static_socket_cast(Source handle)
122 BOOST_STATIC_ASSERT((
123 boost::is_convertible<Source*,FileHandle*>::value &&
124 boost::is_convertible<Target*,FileHandle*>::value &&
125 ( boost::is_convertible<Source,Target>::value ||
126 boost::is_convertible<Target,Source>::value ) ));
127 SENF_ASSERT( check_socket_cast<Target>(handle) );
128 return Target::cast_static(handle);
131 template <class Target, class Source>
132 prefix_ Target senf::dynamic_socket_cast(Source handle)
134 BOOST_STATIC_ASSERT((
135 boost::is_convertible<Source*,FileHandle*>::value &&
136 boost::is_convertible<Target*,FileHandle*>::value &&
137 ( boost::is_convertible<Source,Target>::value ||
138 boost::is_convertible<Target,Source>::value ) ));
139 return Target::cast_dynamic(handle);
142 template <class Target, class Source>
143 prefix_ bool senf::check_socket_cast(Source handle)
145 BOOST_STATIC_ASSERT((
146 boost::is_convertible<Source*,FileHandle*>::value &&
147 boost::is_convertible<Target*,FileHandle*>::value &&
148 ( boost::is_convertible<Source,Target>::value ||
149 boost::is_convertible<Target,Source>::value ) ));
150 // we don't have a non-throwing variant of cast_dynamic
152 // a) since the handle is passed back by value, we cannot return
153 // something like a null handle
154 // b) it is simpler to implement cast_dynamic throwig bad_cast on
155 // failure than implementing cast_check
157 Target::cast_dynamic(handle);
159 catch (std::bad_cast const &) {
165 template <class SocketPolicy>
166 prefix_ void senf::SocketHandle<SocketPolicy>::state(SocketStateMap & map, unsigned lod)
168 // We use typeid here even though the type of *this is static
169 // (SocketHandle is not polymorphic and has no vtable). This will
170 // automatically include the SocketPolicy template parameter in
171 // the type name and therefore show the \e static policy of the
173 map["handle"] << prettyName(typeid(*this));
174 body().state(map,lod);
177 template <class SocketPolicy>
178 prefix_ std::string senf::SocketHandle<SocketPolicy>::dumpState(unsigned lod)
182 return detail::dumpState(map);
185 ///////////////////////////////cti.e///////////////////////////////////////
192 // c-file-style: "senf"
193 // indent-tabs-mode: nil
194 // ispell-local-dictionary: "american"
195 // compile-command: "scons -u test"
196 // comment-column: 40