Socket: Fix dynamic_socket_cast() / check_socket_cast() to support crosscasts
g0dil [Thu, 15 May 2008 07:26:10 +0000 (07:26 +0000)]
Console: Clean up operator<< implementation for Client using socket crosscasts

git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@844 270642c3-0616-0410-b53a-bc976706d245

Console/Server.cc
Console/Server.ih
Socket/SocketHandle.cti
Socket/SocketHandle.test.cc
Socket/SocketPolicy.test.hh

index ac9f034..64b669e 100644 (file)
@@ -249,22 +249,26 @@ prefix_ void senf::console::Client::v_write(boost::posix_time::ptime timestamp,
 
 prefix_ std::ostream & senf::console::operator<<(std::ostream & os, Client const & client)
 {
-    //    typedef senf::ClientSocketHandle<MakeSocketPolicy<INet4AddressingPolicy>::policy > v4Socket;
-    if( senf::check_socket_cast<TCPv4ServerSocketHandle::ClientSocketHandle>( client.handle())) {
-        os<<senf::dynamic_socket_cast<TCPv4ServerSocketHandle::ClientSocketHandle>( client.handle()).peer();
-    }
-    else if( senf::check_socket_cast<TCPv6ServerSocketHandle::ClientSocketHandle>( client.handle())) {
-        os<<senf::dynamic_socket_cast<TCPv6ServerSocketHandle::ClientSocketHandle>( client.handle()).peer();
-    }
-    else{
-        os<<((void *)&client);
-    }
+    typedef ClientSocketHandle< MakeSocketPolicy<
+        INet4AddressingPolicy,ConnectedCommunicationPolicy>::policy > V4Socket;
+    typedef ClientSocketHandle< MakeSocketPolicy<
+        INet6AddressingPolicy,ConnectedCommunicationPolicy>::policy > V6Socket;
+
+    if (check_socket_cast<V4Socket>(client.handle()))
+        os << dynamic_socket_cast<V4Socket>(client.handle()).peer();
+    else if (check_socket_cast<V6Socket>(client.handle()))
+        os << dynamic_socket_cast<V6Socket>(client.handle()).peer();
+    else
+        os << static_cast<void const *>(&client);
+
     return os;
 }
+
 prefix_ std::ostream & senf::console::operator<<(std::ostream & os, Client * client)
 {
-    return os<<*client;
+    return os << *client;
 }
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Server.mpp"
index eabef7e..6bbaf5a 100644 (file)
@@ -80,14 +80,20 @@ namespace detail {
 
         virtual ~ClientReader() = 0;
 
+        // Called by subclasses to get information from the Client
+
         Client & client() const;
         std::string promptString() const;
         ClientHandle handle() const;
         std::ostream & stream() const;
+
+        // Called by subclasses to perform actions in the Client
+
         void stopClient();
-        
         void handleInput(std::string const & input) const;
 
+        // Called by the Client
+
         void disablePrompt();
         void enablePrompt();
         void translate(std::string & data);
index cbd9089..b04f1f8 100644 (file)
@@ -133,11 +133,11 @@ prefix_ Target senf::static_socket_cast(Source handle)
 template <class Target, class Source>
 prefix_ Target senf::dynamic_socket_cast(Source handle)
 {
-    BOOST_STATIC_ASSERT((
-        boost::is_convertible<Source*,FileHandle*>::value &&
-        boost::is_convertible<Target*,FileHandle*>::value &&
-        ( boost::is_convertible<Source,Target>::value ||
-          boost::is_convertible<Target,Source>::value ) ));
+//     BOOST_STATIC_ASSERT((
+//         boost::is_convertible<Source*,FileHandle*>::value &&
+//         boost::is_convertible<Target*,FileHandle*>::value &&
+//         ( boost::is_convertible<Source,Target>::value ||
+//           boost::is_convertible<Target,Source>::value ) ));
     try {
         return Target::cast_dynamic(handle);
     }
@@ -147,11 +147,11 @@ prefix_ Target senf::dynamic_socket_cast(Source handle)
 template <class Target, class Source>
 prefix_ bool senf::check_socket_cast(Source handle)
 {
-    BOOST_STATIC_ASSERT((
-        boost::is_convertible<Source*,FileHandle*>::value &&
-        boost::is_convertible<Target*,FileHandle*>::value &&
-        ( boost::is_convertible<Source,Target>::value ||
-          boost::is_convertible<Target,Source>::value ) ));
+//     BOOST_STATIC_ASSERT((
+//         boost::is_convertible<Source*,FileHandle*>::value &&
+//         boost::is_convertible<Target*,FileHandle*>::value &&
+//         ( boost::is_convertible<Source,Target>::value ||
+//           boost::is_convertible<Target,Source>::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
index c88c636..c8071f3 100644 (file)
@@ -66,6 +66,12 @@ BOOST_AUTO_UNIT_TEST(socketHandle)
         >::policy OtherSocketPolicy;
     typedef senf::SocketHandle<OtherSocketPolicy> OtherSocketHandle;
 
+    typedef senf::MakeSocketPolicy<
+        senf::test::SomeCommunicationPolicy,
+        senf::test::SomeAddressingPolicy
+        >::policy AnotherSocketPolicy;
+    typedef senf::SocketHandle<AnotherSocketPolicy> AnotherSocketHandle;
+
     {
         MySocketHandle myh;
         OtherSocketHandle osh (myh);
@@ -75,6 +81,7 @@ BOOST_AUTO_UNIT_TEST(socketHandle)
         SomeSocketHandle ssh = senf::static_socket_cast<SomeSocketHandle>(osh);
 
         BOOST_CHECK_NO_THROW( senf::dynamic_socket_cast<SomeSocketHandle>(osh) );
+        BOOST_CHECK_NO_THROW( senf::dynamic_socket_cast<AnotherSocketHandle>(osh) );
 
         typedef senf::SocketHandle< senf::MakeSocketPolicy<
             OtherSocketPolicy,
index d921a47..00bf252 100644 (file)
@@ -31,7 +31,6 @@
 ///////////////////////////////hh.p////////////////////////////////////////
 
 namespace senf {
-
 namespace test {
 
     struct SomeAddressingPolicy : public senf::AddressingPolicyBase