From 7314865f0eab4c1f8d2cdb6e4b5cd4016ed0a54d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCrgen=20Hannappel?= <juergen.hannappel@desy.de>
Date: Mon, 5 Jul 2021 18:02:57 +0200
Subject: [PATCH] start producer c api with splitting off common part

---
 common/cpp/include/asapo/common/common_c.h    |  8 +++++
 .../common/internal/asapo_common_c_glue.h     | 25 ++++++++++++++++
 common/cpp/src/common/common_c_glue.cpp       | 29 +++++++++++++++++++
 consumer/api/c/include/asapo/consumer_c.h     |  3 +-
 consumer/api/cpp/src/consumer_c_glue.cpp      | 20 +------------
 producer/CMakeLists.txt                       |  1 +
 producer/api/c/CMakeLists.txt                 |  1 +
 producer/api/c/include/asapo/producer_c.h     | 10 +++++++
 producer/api/cpp/CMakeLists.txt               |  3 +-
 producer/api/cpp/src/producer_c_glue.cpp      | 17 +++++++++++
 10 files changed, 95 insertions(+), 22 deletions(-)
 create mode 100644 common/cpp/include/asapo/common/common_c.h
 create mode 100644 common/cpp/include/asapo/common/internal/asapo_common_c_glue.h
 create mode 100644 common/cpp/src/common/common_c_glue.cpp
 create mode 100644 producer/api/c/CMakeLists.txt
 create mode 100644 producer/api/c/include/asapo/producer_c.h
 create mode 100644 producer/api/cpp/src/producer_c_glue.cpp

diff --git a/common/cpp/include/asapo/common/common_c.h b/common/cpp/include/asapo/common/common_c.h
new file mode 100644
index 000000000..a08e9ac2a
--- /dev/null
+++ b/common/cpp/include/asapo/common/common_c.h
@@ -0,0 +1,8 @@
+#ifndef __COMMON_C_H__
+#define __COMMON_C_H__
+
+#ifndef __COMMON_C_INTERFACE_IMPLEMENTATION__
+typedef void* AsapoConsumerHandle;
+#endif
+
+#endif
diff --git a/common/cpp/include/asapo/common/internal/asapo_common_c_glue.h b/common/cpp/include/asapo/common/internal/asapo_common_c_glue.h
new file mode 100644
index 000000000..889a32115
--- /dev/null
+++ b/common/cpp/include/asapo/common/internal/asapo_common_c_glue.h
@@ -0,0 +1,25 @@
+#ifndef __COMMON_C_GLUE_H__
+#define __COMMON_C_GLUE_H__
+#include <memory>
+
+class AsapoHandle {
+  public:
+    virtual ~AsapoHandle() {};
+};
+
+template<class T>
+class AsapoHandlerHolder final : public AsapoHandle {
+  public:
+    AsapoHandlerHolder(bool manage_memory = true) : handle{nullptr}, manage_memory_{manage_memory} {};
+    AsapoHandlerHolder(T* handle_i, bool manage_memory = true) : handle{handle_i}, manage_memory_{manage_memory} {};
+    ~AsapoHandlerHolder() override {
+        if (!manage_memory_) {
+            handle.release();
+        }
+    }
+    std::unique_ptr<T> handle{nullptr};
+  protected:
+    bool manage_memory_{true};
+};
+
+#endif
diff --git a/common/cpp/src/common/common_c_glue.cpp b/common/cpp/src/common/common_c_glue.cpp
new file mode 100644
index 000000000..1b60c1dfb
--- /dev/null
+++ b/common/cpp/src/common/common_c_glue.cpp
@@ -0,0 +1,29 @@
+#define __COMMON_C_INTERFACE_IMPLEMENTATION__
+#include "asapo/asapo_common_c_glue.h
+
+//! handle for credentials to access a source from a producer
+/// created by asapo_create_source_credentials()
+/// free after use with asapo_free_handle()
+/// \sa asapo::SourceCredentials
+typedef AsapoHandlerHolder<asapo::SourceCredentials>* AsapoSourceCredentialsHandle;
+
+extern "C" {
+
+//! free handle memory, set handle to NULL
+/// \param[in] pointer to an ASAPO handle
+    void asapo_free_handle(void** handle) {
+        if (*handle == nullptr) {
+            return;
+        }
+        auto a_handle = static_cast<AsapoHandle*>(*handle);
+        delete a_handle;
+        *handle = nullptr;
+    }
+
+//! creates a new ASAPO handle
+/// \return initialized ASAPO handle
+    void* asapo_new_handle() {
+        return NULL;
+    }
+
+}
diff --git a/consumer/api/c/include/asapo/consumer_c.h b/consumer/api/c/include/asapo/consumer_c.h
index 442e39aa4..f033896c6 100644
--- a/consumer/api/c/include/asapo/consumer_c.h
+++ b/consumer/api/c/include/asapo/consumer_c.h
@@ -3,8 +3,7 @@
 
 #ifndef __CONSUMER_C_INTERFACE_IMPLEMENTATION__
 typedef int AsapoBool;
-
-typedef void* AsapoConsumerHandle;
+#include <asapo/common_c.h>
 typedef void* AsapoSourceCredentialsHandle;
 typedef void* AsapoErrorHandle;
 typedef void* AsapoMessageMetaHandle;
diff --git a/consumer/api/cpp/src/consumer_c_glue.cpp b/consumer/api/cpp/src/consumer_c_glue.cpp
index a4a2e5937..3c8d11fe6 100644
--- a/consumer/api/cpp/src/consumer_c_glue.cpp
+++ b/consumer/api/cpp/src/consumer_c_glue.cpp
@@ -1,28 +1,10 @@
 #define __CONSUMER_C_INTERFACE_IMPLEMENTATION__
 #include "asapo/asapo_consumer.h"
+#include "asapo/common/internal/asapo_common_c_glue.h"
 
 //! boolean type
 typedef int AsapoBool;
 
-class AsapoHandle {
-  public:
-    virtual ~AsapoHandle() {};
-};
-
-template<class T>
-class AsapoHandlerHolder final : public AsapoHandle {
-  public:
-    AsapoHandlerHolder(bool manage_memory = true) : handle{nullptr}, manage_memory_{manage_memory} {};
-    AsapoHandlerHolder(T* handle_i, bool manage_memory = true) : handle{handle_i}, manage_memory_{manage_memory} {};
-    ~AsapoHandlerHolder() override {
-        if (!manage_memory_) {
-            handle.release();
-        }
-    }
-    std::unique_ptr<T> handle{nullptr};
-  protected:
-    bool manage_memory_{true};
-};
 
 //! handle for an asapo consumer
 /// created by asapo_create_consumer()
diff --git a/producer/CMakeLists.txt b/producer/CMakeLists.txt
index 7f0b9114f..2a4bbf8c7 100644
--- a/producer/CMakeLists.txt
+++ b/producer/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_subdirectory(api/cpp)
+add_subdirectory(api/c)
 
 if (BUILD_PYTHON)
     add_subdirectory(api/python)
diff --git a/producer/api/c/CMakeLists.txt b/producer/api/c/CMakeLists.txt
new file mode 100644
index 000000000..3aa21699d
--- /dev/null
+++ b/producer/api/c/CMakeLists.txt
@@ -0,0 +1 @@
+install(DIRECTORY include/ DESTINATION include)
diff --git a/producer/api/c/include/asapo/producer_c.h b/producer/api/c/include/asapo/producer_c.h
new file mode 100644
index 000000000..cc4d942e2
--- /dev/null
+++ b/producer/api/c/include/asapo/producer_c.h
@@ -0,0 +1,10 @@
+#ifndef __PRODUCER_C_H__
+#define __PRODUCER_C_H__
+#include "asapo/common_c.h"
+#ifndef __PRODUCER_C_INTERFACE_IMPLEMENTATION__
+
+typedef void* AsapoProducerHandle;
+#endif
+
+
+#endif
diff --git a/producer/api/cpp/CMakeLists.txt b/producer/api/cpp/CMakeLists.txt
index 24b572235..f7bbf1bb4 100644
--- a/producer/api/cpp/CMakeLists.txt
+++ b/producer/api/cpp/CMakeLists.txt
@@ -7,7 +7,8 @@ set(SOURCE_FILES
         src/request_handler_filesystem.cpp
         src/receiver_discovery_service.cpp
         src/receiver_data_server_request_handler_factory.cpp
-        src/producer_request.cpp)
+        src/producer_request.cpp
+				src/producer_c_glue.cpp)
 
 
 ################################
diff --git a/producer/api/cpp/src/producer_c_glue.cpp b/producer/api/cpp/src/producer_c_glue.cpp
new file mode 100644
index 000000000..ad46ac2da
--- /dev/null
+++ b/producer/api/cpp/src/producer_c_glue.cpp
@@ -0,0 +1,17 @@
+#define __PRODUCER_C_INTERFACE_IMPLEMENTATION__
+
+#include "asapo/common/internal/asapo_common_c_glue.h"
+#include "asapo/asapo_producer.h"
+
+
+//! handle for an asapo producer
+/// created by asapo_create_producer()
+/// free after use with asapo_free_handle()
+/// all operations are done with asapo_producer_xxx() functions
+/// \sa asapo::Producer
+typedef AsapoHandlerHolder<asapo::Producer>* AsapoProducerHandle;
+
+extern "C" {
+
+
+}
-- 
GitLab