From f9147bbd876729d80dc8ddc8163cd9258acaed10 Mon Sep 17 00:00:00 2001
From: florian <f.spreckelsen@inidscale.com>
Date: Wed, 15 Sep 2021 10:45:56 +0200
Subject: [PATCH] DRAFT: Begin datatype creators and destructors

---
 include/ccaosdb.h |  6 +++++-
 src/ccaosdb.cpp   | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/include/ccaosdb.h b/include/ccaosdb.h
index 61b8681..dc979d7 100644
--- a/include/ccaosdb.h
+++ b/include/ccaosdb.h
@@ -441,7 +441,11 @@ int caosdb_entity_delete_property(caosdb_entity_property *out);
 int caosdb_entity_create_parent(caosdb_entity_parent *out);
 int caosdb_entity_delete_parent(caosdb_entity_parent *out);
 // TODO(fspreck) implementations, also list_datatypes, list_values...
-int caosdb_entity_create_datatype(caosdb_entity_datatype *out);
+// DATATYPE CONSTRUCTORS for atomic and reference datatypes and lists thereof
+int caosdb_entity_create_atomic_datatype(caosdb_entity_datatype *out, const char *name);
+int caosdb_entity_create_reference_datatype(caosdb_entity_datatype *out, const char *name);
+int caosdb_entity_create_atomic_list_datatype(caosdb_entity_datatype *out, const char *name);
+int caosdb_entity_create_reference_list_datatype(caosdb_entity_datatype *out, const char *name);
 int caosdb_entity_delete_datatype(caosdb_entity_datatype *out);
 // VALUE CONSTRUCTORS (resolve overloaded constructors)
 int caosdb_entity_create_int_value(caosdb_entity_value *out, const int64_t value);
diff --git a/src/ccaosdb.cpp b/src/ccaosdb.cpp
index 8a7ae41..cf1d75b 100644
--- a/src/ccaosdb.cpp
+++ b/src/ccaosdb.cpp
@@ -48,6 +48,8 @@ extern "C" {
 
 #define WRAPPED_MESSAGE_CAST(name) static_cast<caosdb::entity::Message *>(name->wrapped_message)
 
+#define WRAPPED_DATATYPE_CAST(name) static_cast<caosdb::entity::Datatype *>(name->wrapped_datatype)
+
 #define WRAPPED_VALUE_CAST(name) static_cast<caosdb::entity::AbstractValue *>(name->wrapped_value)
 
 #define ENUM_NAME_FROM_VALUE(arg, etype)                                                           \
@@ -56,6 +58,11 @@ extern "C" {
 #define ENUM_VALUE_FROM_NAME(arg, etype)                                                           \
   caosdb::utility::getEnumValueFromName<caosdb::entity::etype>(arg)
 
+#define RETURN_ASSIGNEMENT_ERROR_IF_DELETABLE(name)                                                \
+  if (name->_deletable) {                                                                          \
+    return caosdb::StatusCode::EXTERN_C_ASSIGNMENT_ERROR;                                          \
+  }
+
 /*
  * Macro for wrapping every function into a try-catch clause. If an exception
  * occurs, the given StatusCode is being returned.
@@ -683,6 +690,31 @@ ERROR_RETURN_CODE(GENERIC_ERROR, int caosdb_entity_delete_parent(caosdb_entity_p
   return 0;
 })
 
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_create_atomic_datatype(caosdb_entity_datatype *out,
+                                                           const char *name),
+                  {})
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_create_reference_datatype(caosdb_entity_datatype *out,
+                                                              const char *name),
+                  {})
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_create_atomic_list_datatype(caosdb_entity_datatype *out,
+                                                                const char *name),
+                  {})
+ERROR_RETURN_CODE(GENERIC_ERROR,
+                  int caosdb_entity_create_reference_list_datatype(caosdb_entity_datatype *out,
+                                                                   const char *name),
+                  {})
+
+ERROR_RETURN_CODE(GENERIC_ERROR, int caosdb_entity_delete_datatype(caosdb_entity_datatype *out), {
+  if (out->_deletable) {
+    delete WRAPPED_DATATYPE_CAST(out);
+  }
+  out->_deletable = false;
+  return 0;
+})
+
 CREATE_VALUE(int_value, const int64_t value)
 CREATE_VALUE(string_value, const char *value)
 CREATE_VALUE(double_value, const double value)
-- 
GitLab