From 611114067f455e8391bfefe002541a0e98e0d210 Mon Sep 17 00:00:00 2001
From: Carsten Patzke <carsten.patzke@desy.de>
Date: Tue, 13 Mar 2018 17:09:03 +0100
Subject: [PATCH] Refactored error class

---
 common/cpp/include/common/error.h | 10 ++++++
 receiver/CMakeLists.txt           |  1 +
 receiver/src/main.cpp             |  4 +--
 receiver/src/receiver.cpp         | 10 +++---
 receiver/src/receiver.h           | 45 -------------------------
 receiver/src/receiver_error.h     | 55 +++++++++++++++++++++++++++++++
 6 files changed, 73 insertions(+), 52 deletions(-)
 create mode 100644 receiver/src/receiver_error.h

diff --git a/common/cpp/include/common/error.h b/common/cpp/include/common/error.h
index 3a0b1918c..51f4138bd 100644
--- a/common/cpp/include/common/error.h
+++ b/common/cpp/include/common/error.h
@@ -31,6 +31,7 @@ class ErrorInterface {
     virtual void Append(const std::string& value) noexcept = 0;
     virtual ErrorType GetErrorType() const noexcept = 0;
     virtual ~ErrorInterface() = default; // needed for unique_ptr to delete itself
+
     /*TODO: Add these functions, so it will be really easy and convenient to use the error class
      * virtual inline bool operator == (const Error& rhs) const
      * virtual inline bool operator != (const Error& rhs) const
@@ -41,6 +42,7 @@ class ErrorInterface {
      */
 };
 
+
 class ErrorTemplateInterface {
   public:
     virtual ErrorType GetErrorType() const noexcept = 0;
@@ -49,6 +51,7 @@ class ErrorTemplateInterface {
      * virtual inline bool operator ErrorTemplateInterface() const
      */
 
+    /*
     virtual inline bool operator == (const Error* rhs) const {
         return rhs != nullptr &&
                operator==(*rhs);
@@ -58,6 +61,7 @@ class ErrorTemplateInterface {
         return rhs != nullptr &&
                operator!=(*rhs);
     }
+     */
 
     virtual inline bool operator == (const Error& rhs) const {
         return rhs != nullptr &&
@@ -77,6 +81,12 @@ static inline bool operator != (const Error& lhs, const ErrorTemplateInterface&
     return rhs.operator != (lhs);
 }
 
+static inline std::ostream& operator<<(std::ostream& os, const Error& err) {
+    os << err->Explain();
+    return os;
+}
+
+
 class SimpleError: public ErrorInterface {
   private:
     std::string error_;
diff --git a/receiver/CMakeLists.txt b/receiver/CMakeLists.txt
index 6f24c5dec..8842c661a 100644
--- a/receiver/CMakeLists.txt
+++ b/receiver/CMakeLists.txt
@@ -2,6 +2,7 @@ set(TARGET_NAME receiver)
 set(SOURCE_FILES
         src/receiver.h src/receiver.cpp
         src/network_producer_peer.h src/network_producer_peer.cpp src/network_producer_peer_handlers.cpp
+        src/receiver_error.h
         )
 
 
diff --git a/receiver/src/main.cpp b/receiver/src/main.cpp
index 871af6f08..fb55e94fa 100644
--- a/receiver/src/main.cpp
+++ b/receiver/src/main.cpp
@@ -10,7 +10,7 @@ int main (int argc, char* argv[]) {
 
     receiver->StartListener(address, &err);
     if(err) {
-        std::cerr << "Fail to start receiver: " << err->Explain() << std::endl;
+        std::cerr << "Fail to start receiver: " << err << std::endl;
         return 1;
     }
     std::cout << "StartListener on " << address << std::endl;
@@ -21,7 +21,7 @@ int main (int argc, char* argv[]) {
     std::cout << "Stop listener..." << std::endl;
     receiver->StopListener(&err);//TODO might not work since Accept is a blocking call :/
     if(err) {
-        std::cerr << "Fail to stop receiver: " << err->Explain() << std::endl;
+        std::cerr << "Fail to stop receiver: " << err << std::endl;
         return 1;
     }
 
diff --git a/receiver/src/receiver.cpp b/receiver/src/receiver.cpp
index e7be0646e..67efa98c8 100644
--- a/receiver/src/receiver.cpp
+++ b/receiver/src/receiver.cpp
@@ -1,7 +1,7 @@
 #include <cstring>
 #include <iostream>
 #include "receiver.h"
-#include "network_producer_peer.h"
+#include "receiver_error.h"
 
 const int hidra2::Receiver::kMaxUnacceptedConnectionsBacklog = 5;
 
@@ -48,10 +48,10 @@ void hidra2::Receiver::AcceptThreadLogic() {
         std::string address;
         FileDescriptor peer_fd;
 
-        Error io_error;
-        auto client_info_tuple = io->InetAccept(listener_fd_, &io_error);
-        if(io_error) {
-            std::cerr << "An error occurred while accepting an incoming connection" << std::endl;
+        Error err;
+        auto client_info_tuple = io->InetAccept(listener_fd_, &err);
+        if(err) {
+            std::cerr << "An error occurred while accepting an incoming connection: " << err << std::endl;
             return;
         }
         std::tie(address, peer_fd) = *client_info_tuple;
diff --git a/receiver/src/receiver.h b/receiver/src/receiver.h
index 8fdf6be54..5656a1fe7 100644
--- a/receiver/src/receiver.h
+++ b/receiver/src/receiver.h
@@ -9,51 +9,6 @@
 
 namespace hidra2 {
 
-enum class ReceiverErrorType {
-    kAlreadyListening,
-};
-
-class ReceiverError : public SimpleError {
-  private:
-    ReceiverErrorType receiver_error_type_;
-  public:
-    ReceiverError(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleError(error,
-                ErrorType::kReceiverError) {
-        receiver_error_type_ = receiver_error_type;
-    }
-
-    ReceiverErrorType GetReceiverErrorType() const noexcept {
-        return receiver_error_type_;
-    }
-};
-
-class ReceiverErrorTemplate : public SimpleErrorTemplate {
-  protected:
-    ReceiverErrorType receiver_error_type_;
-  public:
-    ReceiverErrorTemplate(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleErrorTemplate(error,
-                ErrorType::kIOError) {
-        receiver_error_type_ = receiver_error_type;
-    }
-
-    inline ReceiverErrorType GetReceiverErrorType() const noexcept {
-        return receiver_error_type_;
-    }
-
-    inline Error Generate() const noexcept override {
-        return Error(new ReceiverError(error_, receiver_error_type_));
-    }
-
-    inline bool operator == (const Error& rhs) const override {
-        return SimpleErrorTemplate::operator==(rhs)
-               && GetReceiverErrorType() == ((ReceiverError*)rhs.get())->GetReceiverErrorType();
-    }
-};
-
-namespace ReceiverErrorTemplates {
-auto const kAlreadyListening = ReceiverErrorTemplate{"Receiver is already listening", ReceiverErrorType::kAlreadyListening};
-};
-
 class Receiver : public HasIO {
     friend NetworkProducerPeer;
   private:
diff --git a/receiver/src/receiver_error.h b/receiver/src/receiver_error.h
new file mode 100644
index 000000000..fc9a7443c
--- /dev/null
+++ b/receiver/src/receiver_error.h
@@ -0,0 +1,55 @@
+#ifndef HIDRA2_RECEIVER_ERROR_H
+#define HIDRA2_RECEIVER_ERROR_H
+
+#include "common/error.h"
+
+namespace hidra2 {
+
+enum class ReceiverErrorType {
+    kAlreadyListening,
+};
+
+class ReceiverError : public SimpleError {
+  private:
+    ReceiverErrorType receiver_error_type_;
+  public:
+    ReceiverError(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleError(error,
+                ErrorType::kReceiverError) {
+        receiver_error_type_ = receiver_error_type;
+    }
+
+    ReceiverErrorType GetReceiverErrorType() const noexcept {
+        return receiver_error_type_;
+    }
+};
+
+class ReceiverErrorTemplate : public SimpleErrorTemplate {
+  protected:
+    ReceiverErrorType receiver_error_type_;
+  public:
+    ReceiverErrorTemplate(const std::string& error, ReceiverErrorType receiver_error_type) : SimpleErrorTemplate(error,
+                ErrorType::kIOError) {
+        receiver_error_type_ = receiver_error_type;
+    }
+
+    inline ReceiverErrorType GetReceiverErrorType() const noexcept {
+        return receiver_error_type_;
+    }
+
+    inline Error Generate() const noexcept override {
+        return Error(new ReceiverError(error_, receiver_error_type_));
+    }
+
+    inline bool operator==(const Error& rhs) const override {
+        return SimpleErrorTemplate::operator==(rhs)
+               && GetReceiverErrorType() == ((ReceiverError*) rhs.get())->GetReceiverErrorType();
+    }
+};
+
+namespace ReceiverErrorTemplates {
+auto const
+kAlreadyListening = ReceiverErrorTemplate{"Receiver is already listening", ReceiverErrorType::kAlreadyListening};
+};
+}
+
+#endif //HIDRA2_RECEIVER_ERROR_H
-- 
GitLab