Move include files in debian packge into 'senf' subdirectory
[senf.git] / Socket / SocketHandle.cti
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief SocketHandle inline template implementation
25  */
26
27 #include "SocketHandle.ih"
28
29 // Custom includes
30 #include <typeinfo>
31 #include <boost/lexical_cast.hpp>
32 #include "../Utils/TypeInfo.hh"
33
34 #define prefix_ inline
35 ///////////////////////////////cti.p///////////////////////////////////////
36
37 template <class SocketPolicy>
38 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle()
39 {}
40
41 template <class SocketPolicy>
42 template <class OtherPolicy>
43 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle(SocketHandle<OtherPolicy> other,
44                                                               typename IsCompatible<OtherPolicy>::type *)
45     : FileHandle(other)
46 {}
47
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)
52 {
53     assign(other);
54     return *this;
55 }
56
57 template <class SocketPolicy>
58 prefix_
59 senf::SocketHandle<SocketPolicy>::SocketHandle(std::auto_ptr<SocketProtocol> protocol,
60                                                       bool isServer)
61     : FileHandle(std::auto_ptr<FileBody>(new SocketBody(protocol,isServer)))
62 {}
63
64 template <class SocketPolicy>
65 prefix_ senf::SocketHandle<SocketPolicy>::SocketHandle(FileHandle other, bool isChecked)
66     : FileHandle(other)
67 {
68     BOOST_ASSERT( isChecked );
69     BOOST_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
70 }
71
72 template <class SocketPolicy>
73 prefix_ senf::SocketBody & senf::SocketHandle<SocketPolicy>::body()
74 {
75     BOOST_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()) );
76     return static_cast<SocketBody &>(FileHandle::body());
77 }
78
79 template <class SocketPolicy>
80 prefix_ senf::SocketBody const & senf::SocketHandle<SocketPolicy>::body()
81     const
82 {
83     BOOST_ASSERT( dynamic_cast<SocketBody const *>(&FileHandle::body()) );
84     return static_cast<SocketBody const &>(FileHandle::body());
85 }
86
87 template <class SocketPolicy>
88 prefix_ senf::SocketProtocol const & senf::SocketHandle<SocketPolicy>::protocol()
89     const
90 {
91     return body().protocol();
92 }
93
94 template <class SocketPolicy>
95 prefix_ void senf::SocketHandle<SocketPolicy>::assign(FileHandle other)
96 {
97     FileHandle::operator=(other);
98 }
99
100 template <class SocketPolicy>
101 prefix_ senf::SocketHandle<SocketPolicy>
102 senf::SocketHandle<SocketPolicy>::cast_static(FileHandle handle)
103 {
104     return SocketHandle(handle,true);
105 }
106
107 template <class SocketPolicy>
108 prefix_ senf::SocketHandle<SocketPolicy>
109 senf::SocketHandle<SocketPolicy>::cast_dynamic(FileHandle handle)
110 {
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);
116 }
117
118 template <class Target, class Source>
119 prefix_ Target senf::static_socket_cast(Source handle)
120 {
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);
128 }
129
130 template <class Target, class Source>
131 prefix_ Target senf::dynamic_socket_cast(Source handle)
132 {
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);
139 }
140
141 template <class Target, class Source>
142 prefix_ bool senf::check_socket_cast(Source handle)
143 {
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
150     // for two reasons:
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
155     try {
156         Target::cast_dynamic(handle);
157     }
158     catch (std::bad_cast const &) {
159         return false;
160     }
161     return true;
162 }
163
164 template <class SocketPolicy>
165 prefix_ void senf::SocketHandle<SocketPolicy>::state(SocketStateMap & map, unsigned lod)
166 {
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
171     // socket handle.
172     map["handle"] = prettyName(typeid(*this));
173     body().state(map,lod);
174 }
175
176 template <class SocketPolicy>
177 prefix_ std::string senf::SocketHandle<SocketPolicy>::dumpState(unsigned lod)
178 {
179     SocketStateMap map;
180     state(map,lod);
181     return detail::dumpState(map);
182 }
183
184 ///////////////////////////////////////////////////////////////////////////
185 // senf::detail::ConvertibleString
186
187 template <class T>
188 prefix_ senf::detail::ConvertibleString::ConvertibleString(T const & other)
189     : std::string(boost::lexical_cast<std::string>(other))
190 {}
191
192 ///////////////////////////////cti.e///////////////////////////////////////
193 #undef prefix_
194
195 \f
196 // Local Variables:
197 // mode: c++
198 // fill-column: 100
199 // c-file-style: "senf"
200 // indent-tabs-mode: nil
201 // ispell-local-dictionary: "american"
202 // compile-command: "scons -u test"
203 // comment-column: 40
204 // End: