4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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.
23 // Definition of inline template functions
25 #include "SocketHandle.ih"
29 #include <boost/lexical_cast.hpp>
30 #include "Utils/TypeInfo.hh"
32 #define prefix_ inline
33 ///////////////////////////////cti.p///////////////////////////////////////
35 template <class SocketPolicy>
36 template <class OtherPolicy>
37 prefix_ satcom::lib::SocketHandle<SocketPolicy>::SocketHandle(SocketHandle<OtherPolicy> other,
38 typename IsCompatible<OtherPolicy>::type *)
42 template <class SocketPolicy>
43 template <class OtherPolicy>
44 prefix_ typename satcom::lib::SocketHandle<SocketPolicy>::template IsCompatible<OtherPolicy>::type const &
45 satcom::lib::SocketHandle<SocketPolicy>::operator=(SocketHandle<OtherPolicy> other)
51 template <class SocketPolicy>
53 satcom::lib::SocketHandle<SocketPolicy>::SocketHandle(std::auto_ptr<SocketProtocol> protocol,
55 : FileHandle(std::auto_ptr<FileBody>(new SocketBody(protocol,isServer)))
58 template <class SocketPolicy>
59 prefix_ satcom::lib::SocketHandle<SocketPolicy>::SocketHandle(FileHandle other, bool isChecked)
62 BOOST_ASSERT( isChecked );
63 BOOST_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
66 template <class SocketPolicy>
67 prefix_ satcom::lib::SocketBody & satcom::lib::SocketHandle<SocketPolicy>::body()
69 BOOST_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
70 return static_cast<SocketBody &>(FileHandle::body());
73 template <class SocketPolicy>
74 prefix_ satcom::lib::SocketBody const & satcom::lib::SocketHandle<SocketPolicy>::body()
77 BOOST_ASSERT( dynamic_cast<SocketBody const *>(&FileHandle::body()) );
78 return static_cast<SocketBody const &>(FileHandle::body());
81 template <class SocketPolicy>
82 prefix_ satcom::lib::SocketProtocol const & satcom::lib::SocketHandle<SocketPolicy>::protocol()
85 return body().protocol();
88 template <class SocketPolicy>
89 prefix_ void satcom::lib::SocketHandle<SocketPolicy>::assign(FileHandle other)
91 FileHandle::operator=(other);
94 template <class SocketPolicy>
95 prefix_ satcom::lib::SocketHandle<SocketPolicy>
96 satcom::lib::SocketHandle<SocketPolicy>::cast_static(FileHandle handle)
98 return SocketHandle(handle,true);
101 template <class SocketPolicy>
102 prefix_ satcom::lib::SocketHandle<SocketPolicy>
103 satcom::lib::SocketHandle<SocketPolicy>::cast_dynamic(FileHandle handle)
105 // throws bad_cast if the body is not a SocketBody
106 SocketBody & body (dynamic_cast<SocketBody&>(FileHandle::body(handle)));
107 // throws bad_cast if the poplicy is not compatible
108 SocketPolicy::checkBaseOf(body.protocol().policy());
109 return cast_static(handle);
112 template <class Target, class Source>
113 prefix_ Target satcom::lib::static_socket_cast(Source handle)
115 BOOST_STATIC_ASSERT((
116 boost::is_convertible<Source*,FileHandle*>::value &&
117 boost::is_convertible<Target*,FileHandle*>::value &&
118 ( boost::is_convertible<Source,Target>::value ||
119 boost::is_convertible<Target,Source>::value ) ));
120 BOOST_ASSERT( check_socket_cast<Target>(handle) );
121 return Target::cast_static(handle);
124 template <class Target, class Source>
125 prefix_ Target satcom::lib::dynamic_socket_cast(Source handle)
127 BOOST_STATIC_ASSERT((
128 boost::is_convertible<Source*,FileHandle*>::value &&
129 boost::is_convertible<Target*,FileHandle*>::value &&
130 ( boost::is_convertible<Source,Target>::value ||
131 boost::is_convertible<Target,Source>::value ) ));
132 return Target::cast_dynamic(handle);
135 template <class Target, class Source>
136 prefix_ bool satcom::lib::check_socket_cast(Source handle)
138 BOOST_STATIC_ASSERT((
139 boost::is_convertible<Source*,FileHandle*>::value &&
140 boost::is_convertible<Target*,FileHandle*>::value &&
141 ( boost::is_convertible<Source,Target>::value ||
142 boost::is_convertible<Target,Source>::value ) ));
143 // we don't have a non-throwing variant of cast_dynamic
145 // a) since the handle is passed back by value, we cannot return
146 // something like a null handle
147 // b) it is simpler to implement cast_dynamic throwig bad_cast on
148 // failure than implementing cast_check
150 Target::cast_dynamic(handle);
152 catch (std::bad_cast const &) {
158 template <class SocketPolicy>
159 prefix_ void satcom::lib::SocketHandle<SocketPolicy>::state(SocketStateMap & map, unsigned lod)
161 map["handle"] = prettyName(typeid(*this));
162 body().state(map,lod);
165 template <class SocketPolicy>
166 prefix_ std::string satcom::lib::SocketHandle<SocketPolicy>::dumpState(unsigned lod)
170 return detail::dumpState(map);
173 ///////////////////////////////////////////////////////////////////////////
174 // satcom::lib::detail::ConvertibleString
177 prefix_ satcom::lib::detail::ConvertibleString::ConvertibleString(T const & other)
178 : std::string(boost::lexical_cast<std::string>(other))
181 ///////////////////////////////cti.e///////////////////////////////////////
187 // c-file-style: "satcom"