From 225f08e4fd2b690203f61b9ab8c6954699cdec1c Mon Sep 17 00:00:00 2001
From: Florent Lamiraux <florent@laas.fr>
Date: Thu, 22 Jun 2023 16:58:12 +0200
Subject: [PATCH] [SignalPtr] Simplify and clean up

  - it is not possible any more to plug a signal of type B into
    a SignalPtr of type A when B derives from A. This feature was anyway
    not used anymore.
---
 include/dynamic-graph/signal-base.h    | 10 ---
 include/dynamic-graph/signal-ptr.h     |  7 --
 include/dynamic-graph/signal-ptr.t.cpp | 91 ++++----------------------
 include/dynamic-graph/signal.h         |  5 --
 tests/signal-all.cpp                   |  7 --
 tests/signal-ptr.cpp                   | 70 --------------------
 tests/signal-time-dependent.cpp        |  9 ---
 7 files changed, 11 insertions(+), 188 deletions(-)

diff --git a/include/dynamic-graph/signal-base.h b/include/dynamic-graph/signal-base.h
index a260a8e..3bc7629 100644
--- a/include/dynamic-graph/signal-base.h
+++ b/include/dynamic-graph/signal-base.h
@@ -186,16 +186,6 @@ class SignalBase : public boost::noncopyable {
 
   /// \}
 
-  /// \name Test
-  /// \{
-  virtual void checkCompatibility() {
-    DG_THROW ExceptionSignal(ExceptionSignal::PLUG_IMPOSSIBLE,
-                             "Abstract signal not compatible with anything.",
-                             "(while trying to plug <%s>).",
-                             this->getName().c_str());
-  }
-  /// \}
-
  protected:
   std::string name;
   Time signalTime;
diff --git a/include/dynamic-graph/signal-ptr.h b/include/dynamic-graph/signal-ptr.h
index 78d1331..50ceb4b 100644
--- a/include/dynamic-graph/signal-ptr.h
+++ b/include/dynamic-graph/signal-ptr.h
@@ -31,7 +31,6 @@ class SignalPtr : public virtual Signal<T, Time> {
  protected:
   Signal<T, Time> *signalPtr;
   bool modeNoThrow;
-  bool transmitAbstract;
   SignalBase<Time> *abstractTransmitter;
   T *transmitAbstractData;
 
@@ -42,7 +41,6 @@ class SignalPtr : public virtual Signal<T, Time> {
       : Signal<T, Time>(name),
         signalPtr(ptr),
         modeNoThrow(false),
-        transmitAbstract(false),
         abstractTransmitter(NULL) {}
 
   virtual ~SignalPtr() { signalPtr = NULL; }
@@ -51,15 +49,12 @@ class SignalPtr : public virtual Signal<T, Time> {
   /* --- PLUG-IN OPERATION --- */
   Signal<T, Time> *getPtr();                       // throw
   const Signal<T, Time> *getPtr() const;           // throw
-  SignalBase<Time> *getAbstractPtr();              // throw
-  const SignalBase<Time> *getAbstractPtr() const;  // throw
   virtual void plug(SignalBase<Time> *ref);
 
   virtual void unplug() { plug(NULL); }
 
   virtual bool isPlugged() const { return (NULL != signalPtr); }
   virtual SignalBase<Time> *getPluged() const { return signalPtr; }
-  virtual bool isAbstractPluged() const;
   virtual const Time &getTime() const;
 
   /* Equivalent operator-like definitions. */
@@ -91,8 +86,6 @@ class SignalPtr : public virtual Signal<T, Time> {
   virtual inline void setConstantDefault() { setConstantDefault(accessCopy()); }
   inline void unsetConstantDefault() { modeNoThrow = false; }
 
-  virtual void checkCompatibility();
-
  public: /* --- INHERITANCE --- */
   /* SignalPtr could be used as a classical signal, through the normal
    * setting functions. The behavior is to plugged the signalPtr on
diff --git a/include/dynamic-graph/signal-ptr.t.cpp b/include/dynamic-graph/signal-ptr.t.cpp
index ed7a4b3..dec5695 100644
--- a/include/dynamic-graph/signal-ptr.t.cpp
+++ b/include/dynamic-graph/signal-ptr.t.cpp
@@ -12,11 +12,6 @@
 #include <dynamic-graph/debug.h>
 
 namespace dynamicgraph {
-template <class T, class Time>
-bool SignalPtr<T, Time>::isAbstractPluged() const {
-  return ((NULL != signalPtr) || (abstractTransmitter));
-}
-
 template <class T, class Time>
 Signal<T, Time> *SignalPtr<T, Time>::getPtr() {
   dgTDEBUGIN(25);
@@ -42,38 +37,11 @@ const Signal<T, Time> *SignalPtr<T, Time>::getPtr() const {
   return signalPtr;
 }
 
-template <class T, class Time>
-SignalBase<Time> *SignalPtr<T, Time>::getAbstractPtr() {
-  if (!isAbstractPluged()) {
-    DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
-                             "In SignalPtr: SIN ptr not set.",
-                             " (in signal <%s>)", getName().c_str());
-  }
-  if (NULL != signalPtr)
-    return signalPtr;
-  else
-    return abstractTransmitter;
-}
-
-template <class T, class Time>
-const SignalBase<Time> *SignalPtr<T, Time>::getAbstractPtr() const {
-  if (!isAbstractPluged()) {
-    DG_THROW ExceptionSignal(ExceptionSignal::NOT_INITIALIZED,
-                             "In SignalPtr: SIN ptr not set.",
-                             " (in signal <%s>)", getName().c_str());
-  }
-  if (NULL != signalPtr)
-    return signalPtr;
-  else
-    return abstractTransmitter;
-}
-
 template <class T, class Time>
 void SignalPtr<T, Time>::plug(SignalBase<Time> *unknown_ref) {
   dgTDEBUGIN(5);
   if (!unknown_ref) {
     signalPtr = NULL;
-    transmitAbstract = false;
     dgTDEBUGOUT(5);
     return;
   }
@@ -82,56 +50,25 @@ void SignalPtr<T, Time>::plug(SignalBase<Time> *unknown_ref) {
               << typeid(Signal<T, Time>::Tcopy1).name() << "{ " << std::endl;
 
   Signal<T, Time> *ref = dynamic_cast<Signal<T, Time> *>(unknown_ref);
-  if (NULL == ref) {
-    try {
-      unknown_ref->checkCompatibility();
-    } catch (T *t) {
-      dgTDEBUG(25) << "Cast THROW ok." << std::endl;
-      Signal<T, Time>::setReference(t);
-      transmitAbstract = true;
-      abstractTransmitter = unknown_ref;
-      transmitAbstractData = t;
-    } catch (...) {
-      dgTDEBUG(25) << "Fatal error." << std::endl;
-      transmitAbstract = false;
-      DG_THROW ExceptionSignal(ExceptionSignal::PLUG_IMPOSSIBLE,
-                               "Compl. Uncompatible types for plugin.",
-                               "(while trying to plug <%s> on <%s>)"
-                               " with types <%s> on <%s>.",
-                               unknown_ref->getName().c_str(),
-                               this->getName().c_str(), typeid(T).name(),
-                               typeid(unknown_ref).name());
-    }
-  } else {
+  if (NULL != ref) {
     dgTDEBUG(25) << "Cast ok." << std::endl;
-    transmitAbstract = false;
     signalPtr = ref;
   }
   dgTDEBUGOUT(5);
 }
 
-template <class T, class Time>
-void SignalPtr<T, Time>::checkCompatibility() {
-  if (isPlugged() && (!autoref())) {
-    getPtr()->checkCompatibility();
-  } else if (isAbstractPluged() && (!autoref())) {
-    abstractTransmitter->checkCompatibility();
-  } else
-    Signal<T, Time>::checkCompatibility();
-}
-
 template <class T, class Time>
 bool SignalPtr<T, Time>::needUpdate(const Time &t) const {
-  if ((isAbstractPluged()) && (!autoref())) {
-    return getAbstractPtr()->needUpdate(t);
+  if ((isPlugged()) && (!autoref())) {
+    return getPtr()->needUpdate(t);
   } else
     return Signal<T, Time>::needUpdate(t);
 }
 
 template <class T, class Time>
 const Time &SignalPtr<T, Time>::getTime() const {
-  if ((isAbstractPluged()) && (!autoref())) {
-    return getAbstractPtr()->getTime();
+  if ((isPlugged()) && (!autoref())) {
+    return getPtr()->getTime();
   }
   return Signal<T, Time>::getTime();
 }
@@ -150,10 +87,6 @@ const T &SignalPtr<T, Time>::access(const Time &t) {
   } else if (autoref()) {
     dgTDEBUGOUT(15);
     return Signal<T, Time>::access(t);
-  } else if (transmitAbstract) {
-    abstractTransmitter->recompute(t);
-    dgTDEBUGOUT(15);
-    return *transmitAbstractData;
   } else {
     dgTDEBUGOUT(15);
     return getPtr()->access(t);
@@ -166,8 +99,6 @@ const T &SignalPtr<T, Time>::accessCopy() const {
     return Signal<T, Time>::accessCopy();
   else if (autoref())
     return Signal<T, Time>::accessCopy();
-  else if (transmitAbstract)
-    return *transmitAbstractData;
   else
     return getPtr()->accessCopy();
 }
@@ -176,9 +107,9 @@ std::ostream &SignalPtr<T, Time>::writeGraph(std::ostream &os) const {
   std::string LeaderLocalName;
   std::string LeaderNodeName;
   Signal<T, Time>::ExtractNodeAndLocalNames(LeaderLocalName, LeaderNodeName);
-  if (isAbstractPluged() && !autoref()) {
+  if (isPlugged() && !autoref()) {
     std::string itLocalName, itNodeName;
-    getAbstractPtr()->ExtractNodeAndLocalNames(itLocalName, itNodeName);
+    getPtr()->ExtractNodeAndLocalNames(itLocalName, itNodeName);
     os << "\t\"" << itNodeName << "\" -> \"" << LeaderNodeName << "\""
        << std::endl
        << "\t [ headlabel = \"" << LeaderLocalName << "\" , taillabel = \""
@@ -193,10 +124,10 @@ std::ostream &SignalPtr<T, Time>::display(std::ostream &os) const {
                  << "||" << signalPtr;
   { Signal<T, Time>::display(os); }
 
-  if ((isAbstractPluged()) && (!autoref())) {
+  if ((isPlugged()) && (!autoref())) {
     os << " -->-- PLUGGED";
   } else {
-    if (!isAbstractPluged())
+    if (!isPlugged())
       os << " UNPLUGGED";
     else if (autoref())
       os << " AUTOPLUGGED";
@@ -213,8 +144,8 @@ std::ostream &SignalPtr<T, Time>::displayDependencies(std::ostream &os,
                                                       std::string next1,
                                                       std::string next2) const {
   dgTDEBUGIN(25);
-  if ((isAbstractPluged()) && (!autoref())) {
-    getAbstractPtr()->displayDependencies(
+  if ((isPlugged()) && (!autoref())) {
+    getPtr()->displayDependencies(
         os, depth, space, next1 + "-- " + SignalBase<Time>::name + " -->",
         next2);
   } else {
diff --git a/include/dynamic-graph/signal.h b/include/dynamic-graph/signal.h
index ee11c73..60d3a54 100644
--- a/include/dynamic-graph/signal.h
+++ b/include/dynamic-graph/signal.h
@@ -106,11 +106,6 @@ class Signal : public SignalBase<Time> {
     aClassName = typeid(this).name();
   }
 
- public:
-  /// checkCompatibility is used to get the object contained in the
-  /// signal. This used to verify if a dynamic cast is possible or not.
-  virtual void checkCompatibility() { throw Tcopy; }
-
  private:
   const T &setTcopy(const T &t);
   T &getTwork();
diff --git a/tests/signal-all.cpp b/tests/signal-all.cpp
index 7235dfd..13be0ff 100644
--- a/tests/signal-all.cpp
+++ b/tests/signal-all.cpp
@@ -90,13 +90,6 @@ BOOST_AUTO_TEST_CASE(test_base) {
   }
   BOOST_CHECK(res);
 
-  res = false;
-  try {
-    /// Check signal compatibility.
-    sigB.checkCompatibility();
-  } catch (const ExceptionSignal &aea) {
-    res = (aea.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
-  }
   /// Verify set command value
 
   /// set
diff --git a/tests/signal-ptr.cpp b/tests/signal-ptr.cpp
index 39b324a..ec6e01d 100644
--- a/tests/signal-ptr.cpp
+++ b/tests/signal-ptr.cpp
@@ -116,29 +116,6 @@ BOOST_AUTO_TEST_CASE(normal_cst_test) {
   output_test_stream output;
   sigNotPlug.display(output);
   cstSigNotPlug.display(output);
-
-  /// Testing getAbsatractPtr() interface: no plug
-  res = false;
-  try {
-    sigNotPlug.getAbstractPtr();
-  } catch (const ExceptionSignal &aea) {
-    res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
-  }
-  BOOST_CHECK(res);
-
-  /// Testing const getAbstractPtr() interface: no plug case
-  try {
-    cstSigNotPlug.getAbstractPtr();
-  } catch (const ExceptionSignal &aea) {
-    res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
-  }
-  BOOST_CHECK(res);
-
-  try {
-    sigNotPlug.checkCompatibility();
-  } catch (...) {
-  }
-  BOOST_CHECK(res);
 }
 
 BOOST_AUTO_TEST_CASE(normal_test) {
@@ -171,33 +148,10 @@ BOOST_AUTO_TEST_CASE(normal_test) {
   sigPtrARef.plug(0);
   sigPtrARef.plug(&sigRef);
   sigPtrBRef.plug(&sigPtrARef);
-  /// Try to plug an incompatible signal.
-  /// leave
-  bool res = false;
-  try {
-    sigPtrARef.plug(&sigstr);
-  } catch (const ExceptionSignal &aes) {
-    res = (aes.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
-  }
-  BOOST_CHECK(res);
-
   /// Plug the signal.
   sigPtrAbstractRef.plug(&sigRef);
   sigPtrA.getPtr();
   BOOST_CHECK(true);
-  try {
-    sigPtrARef.checkCompatibility();
-  } catch (const ExceptionSignal &aes) {
-    /// Should be NOT_INITIALIZED becase the last plug
-    /// on sigstr failed.
-    res = (aes.getCode() == ExceptionSignal::NOT_INITIALIZED);
-  } catch (const std::exception &e) {
-    std::cout << "Standard Exception:" << e.what() << std::endl;
-  } catch (...) {
-    std::cout << "Anything else: " << std::endl;
-  }
-  sigPtrA.needUpdate(5);
-  BOOST_CHECK(true);
 
   int ltime = sigPtrA.getTime();
   sigPtrA.getPluged();
@@ -205,29 +159,6 @@ BOOST_AUTO_TEST_CASE(normal_test) {
   BOOST_CHECK(true);
 
   sigPtrB.getPtr();
-  /// Test sigPtrAbstract with a normal plug.
-  res = false;
-  try {
-    sigPtrAbstract.getAbstractPtr();
-  } catch (ExceptionSignal &aes) {
-    /// Should be NOT_INITIALIZED becase the last plug
-    /// on sigstr failed.
-    std::cout << "Code: " << aes.getCode() << std::endl;
-    res = (aes.getCode() == ExceptionSignal::NOT_INITIALIZED);
-  } catch (...) {
-    std::cout << "Anything else with sigPtrAbstract.getAbstractPtr()"
-              << std::endl;
-  }
-  BOOST_CHECK(true);
-
-  /// Test the case where the plug ref is zero.
-  sigPtrAbstractRef.plug(0);
-  BOOST_CHECK(true);
-
-  assert(sigRef.isPlugged() != true);
-  SignalBase<int> *t = sigRef.getPluged();
-  // assert(sigPtrA.get()=false);
-
   // TODO Can't check if the constant change
   sigPtrA.setConstantDefault(1.2);
   // getconstant
@@ -251,7 +182,6 @@ BOOST_AUTO_TEST_CASE(normal_test) {
   // getconstant
   sigPtrA.displayDependencies(output);
 
-  cout << t << std::endl;
   cout << "Sig = ";
   sigRef.get(cout);
   cout << std::endl;
diff --git a/tests/signal-time-dependent.cpp b/tests/signal-time-dependent.cpp
index 0cd4ead..227ab53 100644
--- a/tests/signal-time-dependent.cpp
+++ b/tests/signal-time-dependent.cpp
@@ -228,13 +228,4 @@ BOOST_AUTO_TEST_CASE(signaltimedependent) {
   const double &avalue = sig1(6);
   output << avalue;
   BOOST_CHECK(true);
-  /// Verify check compatibility
-  try {
-    sig1.checkCompatibility();
-  }
-  //  catch(double e)
-  catch (...) {
-    std::cout << "Message: test \n";
-  }
-  BOOST_CHECK(true);
 }
-- 
GitLab