// $Id$ // // Copyright (C) 2006 // Fraunhofer Institute for Open Communication Systems (FOKUS) // // The contents of this file are subject to the Fraunhofer FOKUS Public License // Version 1.0 (the "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // http://senf.berlios.de/license.html // // The Fraunhofer FOKUS Public License Version 1.0 is based on, // but modifies the Mozilla Public License Version 1.1. // See the full license text for the amendments. // // Software distributed under the License is distributed on an "AS IS" basis, // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License // for the specific language governing rights and limitations under the License. // // The Original Code is Fraunhofer FOKUS code. // // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. // (registered association), Hansastraße 27 c, 80686 Munich, Germany. // All Rights Reserved. // // Contributor(s): // Stefan Bund /** \file \brief SocketHandle inline template implementation */ #include "SocketHandle.ih" // Custom includes #include #include #include #define prefix_ inline //-///////////////////////////////////////////////////////////////////////////////////////////////// //-///////////////////////////////////////////////////////////////////////////////////////////////// // senf::SocketHandle template prefix_ senf::SocketHandle::SocketHandle() {} template template prefix_ senf::SocketHandle::SocketHandle(SocketHandle other, typename IsCompatible::type *) : FileHandle(other) {} template template prefix_ typename senf::SocketHandle::template IsCompatible::type const & senf::SocketHandle::operator=(SocketHandle other) { assign(other); return *this; } template prefix_ senf::SocketHandle::SocketHandle(std::auto_ptr body) : FileHandle(std::auto_ptr(body.release())) {} template prefix_ senf::SocketHandle::SocketHandle(FileHandle other, bool isChecked) : FileHandle(other) { SENF_ASSERT( isChecked, "Internal failure: Wrong overload called !!" ); SENF_ASSERT( ! valid() || dynamic_cast(&FileHandle::body()), "Internal failure: Replacing or re-assigning non-empty incompatible handle"); } template prefix_ senf::SocketBody & senf::SocketHandle::body() { SENF_ASSERT( dynamic_cast(&FileHandle::body()), "Internal failure: Invalid body found it's way into SocketHandle"); return static_cast(FileHandle::body()); } template prefix_ senf::SocketBody const & senf::SocketHandle::body() const { SENF_ASSERT( dynamic_cast(&FileHandle::body()), "Internal failure: Invalid body found it's way into SocketHandle"); return static_cast(FileHandle::body()); } template prefix_ senf::SocketProtocol & senf::SocketHandle::protocol() const { return body().protocol(); } template prefix_ void senf::SocketHandle::assign(FileHandle other) { FileHandle::operator=(other); } template prefix_ senf::SocketHandle senf::SocketHandle::cast_static(FileHandle handle) { return SocketHandle(handle,true); } template prefix_ senf::SocketHandle senf::SocketHandle::cast_dynamic(FileHandle handle) { // throws bad_cast if the body is not a SocketBody SocketBody & body (dynamic_cast(FileHandle::body(handle))); // throws bad_cast if the policy is not compatible (already wrapped ...) SPolicy::checkBaseOf(body.protocol().policy()); return cast_static(handle); } template prefix_ Target senf::static_socket_cast(Source handle) { BOOST_STATIC_ASSERT(( boost::is_convertible::value && boost::is_convertible::value && ( boost::is_convertible::value || boost::is_convertible::value ) )); SENF_ASSERT( check_socket_cast(handle), "Invalid static_socket_cast" ); return Target::cast_static(handle); } template prefix_ Target senf::dynamic_socket_cast(Source handle) { // BOOST_STATIC_ASSERT(( // boost::is_convertible::value && // boost::is_convertible::value && // ( boost::is_convertible::value || // boost::is_convertible::value ) )); try { return Target::cast_dynamic(handle); } SENF_WRAP_EXC(std::bad_cast) } template prefix_ bool senf::check_socket_cast(Source handle) { // BOOST_STATIC_ASSERT(( // boost::is_convertible::value && // boost::is_convertible::value && // ( boost::is_convertible::value || // boost::is_convertible::value ) )); // we don't have a non-throwing variant of cast_dynamic // for two reasons: // a) since the handle is passed back by value, we cannot return // something like a null handle // b) it is simpler to implement cast_dynamic throwing bad_cast on // failure than implementing cast_check try { Target::cast_dynamic(handle); } catch (std::bad_cast const &) { return false; } return true; } template prefix_ void senf::SocketHandle::state(SocketStateMap & map, unsigned lod) { // We use typeid here even though the type of *this is static // (SocketHandle is not polymorphic and has no vtable). This will // automatically include the SocketPolicy template parameter in // the type name and therefore show the \e static policy of the // socket handle. map["handle"] << prettyName(typeid(*this)); if (valid()) { map["valid"] << "true"; body().state(map,lod); } else map["valid"] << "false"; } template prefix_ std::string senf::SocketHandle::dumpState(unsigned lod) { SocketStateMap map; state(map,lod); return detail::dumpState(map); } template template prefix_ Facet & senf::SocketHandle::facet() { try { return dynamic_cast(protocol()); } SENF_WRAP_EXC(std::bad_cast) } //-///////////////////////////////////////////////////////////////////////////////////////////////// // senf::ProtocolSocketBody template prefix_ senf::ProtocolSocketBody::ProtocolSocketBody(bool isServer) : SocketBody(isServer) {} template prefix_ senf::ProtocolSocketBody::ProtocolSocketBody(bool isServer, int fd) : SocketBody(isServer, fd) {} //-///////////////////////////////////////////////////////////////////////////////////////////////// template prefix_ std::ostream & senf::operator<<(std::ostream & os, SocketHandle handle) { os << handle.dumpState(); return os; } //-///////////////////////////////////////////////////////////////////////////////////////////////// #undef prefix_ // Local Variables: // mode: c++ // fill-column: 100 // c-file-style: "senf" // indent-tabs-mode: nil // ispell-local-dictionary: "american" // compile-command: "scons -u test" // comment-column: 40 // End: