From 2c7bc130c75665c4639cca1d6af142c35a233449 Mon Sep 17 00:00:00 2001
From: Daniel <d.hornung@indiscale.com>
Date: Thu, 29 Jul 2021 12:14:49 +0200
Subject: [PATCH] WIP: Compiles now for multiple cpp files.

- Fixed CMakeLists.txt.
- New caosdbClass.m, just for development for now.
- New _info.cpp which converts some data.
---
 CMakeLists.txt        |  9 ++++--
 src/caosdbClass.m     | 23 +++++++++++++++
 src/private/_info.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 95 insertions(+), 3 deletions(-)
 create mode 100644 src/caosdbClass.m
 create mode 100644 src/private/_info.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2d8b05f..5212bd2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -54,7 +54,7 @@ endif()
 
 
 #######################################################
-### Compile *.mex files
+### Compile into *.mex files
 #######################################################
 add_subdirectory(src)
 set(PKG_INST_DIR "${PROJECT_SOURCE_DIR}/inst")
@@ -68,9 +68,11 @@ string(REGEX REPLACE ";" ":" _MKOCTFILE_RPATH "${CONAN_LIB_DIRS}")
 set(_MKOCTFILE_OPTIONS "-Wl,-rpath,${_MKOCTFILE_RPATH}" "--mex" "-std=gnu++17"
     "-L/usr/local/lib" ${_MKOCTFILE_INCLUDES} ${_MKOCTFILE_LIB_DIRS} ${_MKOCTFILE_LIBS})
 
+add_custom_target(mex ALL)
 file(GLOB_RECURSE _CPP_SOURCES RELATIVE "${PROJECT_SOURCE_DIR}/src" src/*.cpp)
 foreach(sourcefile ${_CPP_SOURCES})
-    STRING(REGEX REPLACE ".cpp$" ".mex" _mex_ext_file ${sourcefile})
+    string(REGEX REPLACE ".cpp$" ".mex" _mex_ext_file ${sourcefile})
+    STRING(REGEX REPLACE "/" "__" _target_name "${sourcefile}")
     set(_mex_ext_file "${PKG_INST_DIR}/${_mex_ext_file}")
     set(_mkoct_output "-o" "${_mex_ext_file}")
     set(_abs_source "${PROJECT_SOURCE_DIR}/src/${sourcefile}")
@@ -79,7 +81,8 @@ foreach(sourcefile ${_CPP_SOURCES})
         ARGS ${_MKOCTFILE_OPTIONS} ${_mkoct_output} ${_abs_source}
         MAIN_DEPENDENCY ${_abs_source}
         )
-    add_custom_target(mex ALL DEPENDS ${_mex_ext_file})
+    add_custom_target(${_target_name} DEPENDS ${_mex_ext_file})
+    add_dependencies(mex ${_target_name})
 endforeach(sourcefile)
 
 
diff --git a/src/caosdbClass.m b/src/caosdbClass.m
new file mode 100644
index 0000000..44ba543
--- /dev/null
+++ b/src/caosdbClass.m
@@ -0,0 +1,23 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                    Hello                                    %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+classdef caosdbClass < handle
+  properties
+    connection;             % Will use the default connection
+  endproperties
+
+  methods
+    function obj = caosdbClass(connection="")
+      obj.connection = connection;
+      disp(["created: >", connection, "<"]);
+    endfunction
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                     info                                    %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+    function res = info(obj)
+      info_result = _info(obj.connection)
+      res = info_result
+    endfunction
+  endmethods
+endclassdef
diff --git a/src/private/_info.cpp b/src/private/_info.cpp
new file mode 100644
index 0000000..7bb639a
--- /dev/null
+++ b/src/private/_info.cpp
@@ -0,0 +1,66 @@
+#include "caosdb/connection.h" // for Connection, ConnectionManager
+#include "caosdb/constants.h"  // for LIBCAOSDB_VERSION_MAJOR, LIBCAOSDB_VE...
+#include "caosdb/info.h"       // for VersionInfo
+#include "mex.h"               // for mxArray, mexFunction
+#include "mexproto.h"          // for mexPrintf, mxCreateString, mxGetChars
+#include <cstring>             // for strcmp
+#include <memory>              // for unique_ptr, __shared_ptr_access, shar...
+#include <string>              // for allocator, char_traits, operator+
+
+using std::string;
+
+mxArray* info(const string &connection_name);
+
+// auto print_version() -> const char * {
+//   mexPrintf("Octave caosdb client %s\n\n", FULL_VERSION.c_str());
+//   mexPrintf("We don't miss the H of caos.\n");
+//   return FULL_VERSION.c_str();
+// };
+
+mxArray* info(string const &connection_name) {
+  const auto &connection =
+    caosdb::connection::ConnectionManager::GetDefaultConnection();
+  const auto &version_info = connection->GetVersionInfo();
+  mexPrintf("Server Version: v%d.%d.%d-%s-%s\n", version_info->GetMajor(),
+            version_info->GetMinor(), version_info->GetPatch(),
+            version_info->GetPreRelease().c_str(),
+            version_info->GetBuild().c_str());
+  const char *keys[] = {"major", "minor", "patch", "pre_release", "build"};
+  mwSize dims[2] = {1, 1};
+  mxArray* info_struct = mxCreateStructArray(2, dims, 5, keys);
+
+  auto* major = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
+  UINT64_T* major_data = (UINT64_T*) mxGetData(major);
+  major_data[0] = version_info->GetMajor();
+  mxSetData(major, major_data);
+
+  mxSetField(info_struct, 0, "major", major);
+  mxSetField(info_struct, 0, "pre_release",
+             mxCreateString(version_info->GetPreRelease().c_str()));
+  mxSetField(info_struct, 0, "build",
+             mxCreateString("foobuild"));
+  return info_struct;
+}
+/**
+ * 
+ */
+/**
+ * @brief      Get info about the server
+ *
+ * @details    At the moment, this is just the version of the server.
+ *
+ * @param connection_name A string with the connection name. May be omitted or an empty string.
+ *
+ * @return     return type
+ */
+void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
+                 const mxArray *prhs[]) {
+
+  string conn_name("");
+  if (nrhs >= 1)  {
+    conn_name = mxGetChars(prhs[0]);
+  }
+  auto info_struct = info(conn_name);
+  plhs[0] = info_struct;
+  // plhs[0] = mxCreateString("foostring");
+}
-- 
GitLab