PPI: Implement Jack reset() capability
g0dil [Mon, 20 Jul 2009 16:10:16 +0000 (16:10 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1274 270642c3-0616-0410-b53a-bc976706d245

PPI/Jack.cci
PPI/Jack.cti
PPI/Jack.hh
PPI/Jack.test.cc

index 9ae3da5..f40a3e9 100644 (file)
 
 prefix_ senf::ppi::connector::GenericActiveInputJack::
 GenericActiveInputJack(GenericActiveInput & input)
-    : input_ (input)
+    : input_ (&input)
 {}
 
 prefix_ senf::ppi::connector::GenericActiveInput &
 senf::ppi::connector::GenericActiveInputJack::connector()
 {
-    return input_;
+    return *input_;
+}
+
+prefix_ void senf::ppi::connector::GenericActiveInputJack::reset(GenericActiveInput & input)
+{
+    if (input_->connected()) {
+        GenericPassiveOutput & peer (input_->peer());
+        input_->disconnect();
+        connect(peer, input);
+    }
+    input_ = & input;
 }
 
 prefix_ senf::ppi::connector::GenericActiveOutputJack::
 GenericActiveOutputJack(GenericActiveOutput & output)
-    : output_ (output)
+    : output_ (&output)
 {}
 
 prefix_ senf::ppi::connector::GenericActiveOutput &
 senf::ppi::connector::GenericActiveOutputJack::connector()
 {
-    return output_;
+    return *output_;
+}
+
+prefix_ void senf::ppi::connector::GenericActiveOutputJack::reset(GenericActiveOutput & output)
+{
+    if (output_->connected()) {
+        GenericPassiveInput & peer (output_->peer());
+        output_->disconnect();
+        connect(output, peer);
+    }
+    output_ = & output;
 }
 
 prefix_ senf::ppi::connector::GenericPassiveInputJack::
 GenericPassiveInputJack(GenericPassiveInput & input)
-    : input_ (input)
+    : input_ (&input)
 {}
 
 prefix_ senf::ppi::connector::GenericPassiveInput &
 senf::ppi::connector::GenericPassiveInputJack::connector()
 {
-    return input_;
+    return *input_;
+}
+
+prefix_ void senf::ppi::connector::GenericPassiveInputJack::reset(GenericPassiveInput & input)
+{
+    if (input_->connected()) {
+        GenericActiveOutput & peer (input_->peer());
+        input_->disconnect();
+        connect(peer, input);
+    }
+    input_ = & input;
 }
 
 prefix_ senf::ppi::connector::GenericPassiveOutputJack::
 GenericPassiveOutputJack(GenericPassiveOutput & output)
-    : output_ (output)
+    : output_ (&output)
 {}
 
 prefix_ senf::ppi::connector::GenericPassiveOutput &
 senf::ppi::connector::GenericPassiveOutputJack::connector()
 {
-    return output_;
+    return *output_;
+}
+
+prefix_ void
+senf::ppi::connector::GenericPassiveOutputJack::reset(GenericPassiveOutput & output)
+{
+    if (output_->connected()) {
+        GenericActiveInput & peer (output_->peer());
+        output_->disconnect();
+        connect(output, peer);
+    }
+    output_ = & output;
 }
 
 prefix_ senf::ppi::connector::ActiveInputJack<senf::Packet>::
@@ -85,6 +126,16 @@ ActiveInputJack(GenericActiveInputJack input)
     : GenericActiveInputJack (input.connector()) 
 {}
 
+prefix_ void senf::ppi::connector::ActiveInputJack<senf::Packet>::reset(GenericActiveInput & input)
+{
+    GenericActiveInputJack::reset(input);
+}
+
+prefix_ void senf::ppi::connector::ActiveInputJack<senf::Packet>::reset(GenericActiveInputJack input)
+{
+    GenericActiveInputJack::reset(input.connector());
+}
+
 prefix_ senf::ppi::connector::ActiveOutputJack<senf::Packet>::
 ActiveOutputJack(GenericActiveOutput & output)
     : GenericActiveOutputJack (output) 
@@ -95,6 +146,17 @@ ActiveOutputJack(GenericActiveOutputJack & output)
     : GenericActiveOutputJack (output.connector()) 
 {}
 
+prefix_ void senf::ppi::connector::ActiveOutputJack<senf::Packet>::reset(GenericActiveOutput & output)
+{
+    GenericActiveOutputJack::reset(output);
+}
+
+prefix_ void
+senf::ppi::connector::ActiveOutputJack<senf::Packet>::reset(GenericActiveOutputJack & output)
+{
+    GenericActiveOutputJack::reset(output.connector());
+}
+
 prefix_ senf::ppi::connector::PassiveInputJack<senf::Packet>::
 PassiveInputJack(GenericPassiveInput & input)
     : GenericPassiveInputJack (input) 
@@ -105,6 +167,17 @@ PassiveInputJack(GenericPassiveInputJack & input)
     : GenericPassiveInputJack (input.connector()) 
 {}
 
+prefix_ void senf::ppi::connector::PassiveInputJack<senf::Packet>::reset(GenericPassiveInput & input)
+{
+    GenericPassiveInputJack::reset(input);
+}
+
+prefix_ void
+senf::ppi::connector::PassiveInputJack<senf::Packet>::reset(GenericPassiveInputJack & input)
+{
+    GenericPassiveInputJack::reset(input.connector());
+}
+
 prefix_ senf::ppi::connector::PassiveOutputJack<senf::Packet>::
 PassiveOutputJack(GenericPassiveOutput & output)
     : GenericPassiveOutputJack (output) 
@@ -115,6 +188,18 @@ PassiveOutputJack(GenericPassiveOutputJack & output)
     : GenericPassiveOutputJack (output.connector()) 
 {}
 
+prefix_ void
+senf::ppi::connector::PassiveOutputJack<senf::Packet>::reset(GenericPassiveOutput & output)
+{
+    GenericPassiveOutputJack::reset(output);
+}
+
+prefix_ void
+senf::ppi::connector::PassiveOutputJack<senf::Packet>::reset(GenericPassiveOutputJack & output)
+{
+    GenericPassiveOutputJack::reset(output.connector());
+}
+
 prefix_ void senf::ppi::connect(connector::GenericActiveOutputJack & source,
                                 connector::GenericPassiveInputJack & target)
 {
index 30a4c64..bdd3eca 100644 (file)
@@ -56,6 +56,31 @@ senf::ppi::connector::ActiveInputJack<PacketType>::ActiveInputJack(ActiveInputJa
 {}
 
 template <class PacketType>
+prefix_ void
+senf::ppi::connector::ActiveInputJack<PacketType>::reset(ActiveInput<PacketType> & input)
+{
+    GenericActiveInputJack::reset(input);
+}
+
+template <class PacketType>
+prefix_ void senf::ppi::connector::ActiveInputJack<PacketType>::reset(ActiveInput<> & input)
+{
+    GenericActiveInputJack::reset(input);
+}
+
+template <class PacketType>
+prefix_ void senf::ppi::connector::ActiveInputJack<PacketType>::reset(ActiveInputJack & input)
+{
+    GenericActiveInputJack::reset(input);
+}
+
+template <class PacketType>
+prefix_ void senf::ppi::connector::ActiveInputJack<PacketType>::reset(ActiveInputJack<> & input)
+{
+    GenericActiveInputJack::reset(input);
+}
+
+template <class PacketType>
 prefix_ senf::ppi::connector::ActiveOutputJack<PacketType>::
 ActiveOutputJack(ActiveOutput<PacketType> & output)
     : GenericActiveOutputJack (output)
@@ -80,6 +105,33 @@ ActiveOutputJack(ActiveOutputJack<> & output)
 {}
 
 template <class PacketType>
+prefix_ void
+senf::ppi::connector::ActiveOutputJack<PacketType>::reset(ActiveOutput<PacketType> & output)
+{
+    GenericActiveOutputJack::reset(output);
+}
+
+template <class PacketType>
+prefix_ void senf::ppi::connector::ActiveOutputJack<PacketType>::reset(ActiveOutput<> & output)
+{
+    GenericActiveOutputJack::reset(output);
+}
+
+template <class PacketType>
+prefix_ void
+senf::ppi::connector::ActiveOutputJack<PacketType>::reset(ActiveOutputJack & output)
+{
+    GenericActiveOutputJack::reset(output);
+}
+
+template <class PacketType>
+prefix_ void
+senf::ppi::connector::ActiveOutputJack<PacketType>::reset(ActiveOutputJack<> & output)
+{
+    GenericActiveOutputJack::reset(output);
+}
+
+template <class PacketType>
 prefix_ senf::ppi::connector::PassiveInputJack<PacketType>::
 PassiveInputJack(PassiveInput<PacketType> & input)
     : GenericPassiveInputJack (input)
@@ -104,6 +156,32 @@ PassiveInputJack(PassiveInputJack<> & input)
 {}
 
 template <class PacketType>
+prefix_ void
+senf::ppi::connector::PassiveInputJack<PacketType>::reset(PassiveInput<PacketType> & input)
+{
+    GenericPassiveInputJack::reset(input);
+}
+
+template <class PacketType>
+prefix_ void senf::ppi::connector::PassiveInputJack<PacketType>::reset(PassiveInput<> & input)
+{
+    GenericPassiveInputJack::reset(input);
+}
+
+template <class PacketType>
+prefix_ void senf::ppi::connector::PassiveInputJack<PacketType>::reset(PassiveInputJack & input)
+{
+    GenericPassiveInputJack::reset(input);
+}
+
+template <class PacketType>
+prefix_ void
+senf::ppi::connector::PassiveInputJack<PacketType>::reset(PassiveInputJack<> & input)
+{
+    GenericPassiveInputJack::reset(input);
+}
+
+template <class PacketType>
 prefix_ senf::ppi::connector::PassiveOutputJack<PacketType>::
 PassiveOutputJack(PassiveOutput<PacketType> & output)
     : GenericPassiveOutputJack (output)
@@ -127,6 +205,34 @@ PassiveOutputJack(PassiveOutputJack<> & output)
     : GenericPassiveOutputJack (output.connector())
 {}
 
+template <class PacketType>
+prefix_ void
+senf::ppi::connector::PassiveOutputJack<PacketType>::reset(PassiveOutput<PacketType> & output)
+{
+    GenericPassiveOutputJack::reset(output);
+}
+
+template <class PacketType>
+prefix_ void
+senf::ppi::connector::PassiveOutputJack<PacketType>::reset(PassiveOutput<> & output)
+{
+    GenericPassiveOutputJack::reset(output);
+}
+
+template <class PacketType>
+prefix_ void
+senf::ppi::connector::PassiveOutputJack<PacketType>::reset(PassiveOutputJack & output)
+{
+    GenericPassiveOutputJack::reset(output);
+}
+
+template <class PacketType>
+prefix_ void
+senf::ppi::connector::PassiveOutputJack<PacketType>::reset(PassiveOutputJack<> & output)
+{
+    GenericPassiveOutputJack::reset(output);
+}
+
 template <class T>
 prefix_ void
 senf::ppi::connect(connector::GenericActiveOutputJack & source, T & target,
index a47d15d..9d57186 100644 (file)
@@ -55,8 +55,11 @@ namespace connector {
 
         GenericActiveInput & connector(); ///< Get referenced connector
 
+        void reset(GenericActiveInput & input); ///< Change connector
+                                        /**< Will update any existing connection accordingly */
+
     private:
-        GenericActiveInput & input_;
+        GenericActiveInput * input_;
     };
 
     /** \brief Jack referencing an ActiveOutput
@@ -69,8 +72,11 @@ namespace connector {
 
         GenericActiveOutput & connector(); ///< Get referenced connector
 
+        void reset(GenericActiveOutput & output); ///< Change connector
+                                        /**< Will update any existing connection accordingly */
+
     private:
-        GenericActiveOutput & output_;
+        GenericActiveOutput * output_;
     };
 
     /** \brief Jack referencing a PassiveInput
@@ -83,8 +89,11 @@ namespace connector {
 
         GenericPassiveInput & connector(); ///< Get referenced connector
 
+        void reset(GenericPassiveInput & input); ///< Change connector
+                                        /**< Will update any existing connection accordingly */
+
     private:
-        GenericPassiveInput & input_;
+        GenericPassiveInput * input_;
     };
     
     /** \brief Jack referencing a PassiveOutput
@@ -97,8 +106,11 @@ namespace connector {
 
         GenericPassiveOutput & connector(); ///< Get referenced connector
 
+        void reset(GenericPassiveOutput & output); ///< Change connector
+                                        /**< Will update any existing connection accordingly */
+
     private:
-        GenericPassiveOutput & output_;
+        GenericPassiveOutput * output_;
     };
 
     /** \brief Jack with packet type referencing an ActiveInput
@@ -113,6 +125,12 @@ namespace connector {
         
         explicit ActiveInputJack(ActiveInputJack & input);
         explicit ActiveInputJack(ActiveInputJack<> & input);
+
+        void reset(ActiveInput<PacketType> & input);
+        void reset(ActiveInput<> & input);
+        
+        void reset(ActiveInputJack & input);
+        void reset(ActiveInputJack<> & input);
     };
 
 #ifndef DOXYGEN
@@ -124,6 +142,9 @@ namespace connector {
     public:
         explicit ActiveInputJack(GenericActiveInput & input);
         explicit ActiveInputJack(GenericActiveInputJack input);
+
+        void reset(GenericActiveInput & input);
+        void reset(GenericActiveInputJack input);
     };
 
 #endif
@@ -140,6 +161,12 @@ namespace connector {
 
         explicit ActiveOutputJack(ActiveOutputJack & output);
         explicit ActiveOutputJack(ActiveOutputJack<> & output);
+
+        void reset(ActiveOutput<PacketType> & output);
+        void reset(ActiveOutput<> & output);
+
+        void reset(ActiveOutputJack & output);
+        void reset(ActiveOutputJack<> & output);
     };
 
 #ifndef DOXYGEN
@@ -151,6 +178,9 @@ namespace connector {
     public:
         explicit ActiveOutputJack(GenericActiveOutput & output);
         explicit ActiveOutputJack(GenericActiveOutputJack & output);
+
+        void reset(GenericActiveOutput & output);
+        void reset(GenericActiveOutputJack & output);
     };
 
 #endif
@@ -167,6 +197,12 @@ namespace connector {
 
         explicit PassiveInputJack(PassiveInputJack & input);
         explicit PassiveInputJack(PassiveInputJack<> & input);
+
+        void reset(PassiveInput<PacketType> & input);
+        void reset(PassiveInput<> & input);
+
+        void reset(PassiveInputJack & input);
+        void reset(PassiveInputJack<> & input);
     };
 
 #ifndef DOXYGEN
@@ -178,6 +214,9 @@ namespace connector {
     public:
         explicit PassiveInputJack(GenericPassiveInput & input);
         explicit PassiveInputJack(GenericPassiveInputJack & input);
+
+        void reset(GenericPassiveInput & input);
+        void reset(GenericPassiveInputJack & input);
     };
 
 #endif
@@ -194,6 +233,12 @@ namespace connector {
 
         explicit PassiveOutputJack(PassiveOutputJack & output);
         explicit PassiveOutputJack(PassiveOutputJack<> & output);
+
+        void reset(PassiveOutput<PacketType> & output);
+        void reset(PassiveOutput<> & output);
+
+        void reset(PassiveOutputJack & output);
+        void reset(PassiveOutputJack<> & output);
     };
 
 #ifndef DOXYGEN
@@ -205,6 +250,9 @@ namespace connector {
     public:
         explicit PassiveOutputJack(GenericPassiveOutput & output);
         explicit PassiveOutputJack(GenericPassiveOutputJack & output);
+
+        void reset(GenericPassiveOutput & output);
+        void reset(GenericPassiveOutputJack & output);
     };
 
 #endif
index 10a20e3..bc6b0c1 100644 (file)
@@ -45,12 +45,14 @@ namespace {
         senf::ppi::connector::ActiveInput<> input;
         senf::ppi::connector::PassiveOutput<> output;
 
-        ActiveDummyForward()
+        ActiveDummyForward() : n (0)
             { route(input, output); output.onRequest(&ActiveDummyForward::request); }
 
+        unsigned n;
+
     private:
         void request()
-            { output(input()); }
+            { ++n; output(input()); }
     };
 
     class PassiveDummyForward 
@@ -61,38 +63,50 @@ namespace {
         senf::ppi::connector::PassiveInput<> input;
         senf::ppi::connector::ActiveOutput<> output;
 
-        PassiveDummyForward()
+        PassiveDummyForward() : n (0)
             { route(input, output); input.onRequest(&PassiveDummyForward::request); }
 
+        unsigned n;
+
     private:
         void request()
-            { output(input()); }
+            { ++n; output(input()); }
     };
 
-    class ActiveGroup
+    struct ActiveGroup
     {
-    public:
         senf::ppi::connector::ActiveInputJack<> input;
         senf::ppi::connector::PassiveOutputJack<> output;
 
         ActiveGroup()
-            : input (forward.input), output (forward.output) {}
+            : input (forward1.input), output (forward1.output) {}
 
-    private:
-        ActiveDummyForward forward;
+        void flip()
+        {
+            input.reset(forward2.input);
+            output.reset(forward2.output);
+        }
+
+        ActiveDummyForward forward1;
+        ActiveDummyForward forward2;
     };
 
-    class PassiveGroup
+    struct PassiveGroup
     {
-    public:
         senf::ppi::connector::PassiveInputJack<> input;
         senf::ppi::connector::ActiveOutputJack<> output;
 
         PassiveGroup()
-            : input (forward.input), output (forward.output) {}
+            : input (forward1.input), output (forward1.output) {}
         
-    private:
-        PassiveDummyForward forward;
+        void flip()
+        {
+            input.reset(forward2.input);
+            output.reset(forward2.output);
+        }
+
+        PassiveDummyForward forward1;
+        PassiveDummyForward forward2;
     };
 
 }
@@ -109,10 +123,25 @@ BOOST_AUTO_UNIT_TEST(jacks)
         
         senf::ppi::init();
         
-        senf::Packet p (senf::DataPacket::create());
-        source.submit(p);
+        {
+            senf::Packet p (senf::DataPacket::create());
+            source.submit(p);
+
+            BOOST_CHECK(p == sink.request());
+        }
+
+        group.flip();
+        senf::ppi::init();
         
-        BOOST_CHECK(p == sink.request());
+        {
+            senf::Packet p (senf::DataPacket::create());
+            source.submit(p);
+
+            BOOST_CHECK(p == sink.request());
+        }
+
+        BOOST_CHECK_EQUAL( group.forward1.n, 1u );
+        BOOST_CHECK_EQUAL( group.forward2.n, 1u );
     }
      
     {
@@ -125,10 +154,25 @@ BOOST_AUTO_UNIT_TEST(jacks)
 
         senf::ppi::init();
 
-        senf::Packet p (senf::DataPacket::create());
-        source.submit(p);
+        {
+            senf::Packet p (senf::DataPacket::create());
+            source.submit(p);
+
+            BOOST_CHECK(p == sink.pop_front());
+        }
+
+        group.flip();
+        senf::ppi::init();
+
+        {
+            senf::Packet p (senf::DataPacket::create());
+            source.submit(p);
+
+            BOOST_CHECK(p == sink.pop_front());
+        }
 
-        BOOST_CHECK(p == sink.front());
+        BOOST_CHECK_EQUAL( group.forward1.n, 1u );
+        BOOST_CHECK_EQUAL( group.forward2.n, 1u );
     }
 }