Skip to content
Snippets Groups Projects
Commit 55d304ad authored by florian's avatar florian
Browse files

ENH: Add creators and deleter for values

parent cd50b367
No related branches found
No related tags found
1 merge request!24API: Introduce value and datatype structs to Extern C
......@@ -431,7 +431,6 @@ int caosdb_entity_delete_parent(caosdb_entity_parent *out);
int caosdb_entity_create_datatype(caosdb_entity_datatype *out);
int caosdb_entity_delete_datatype(caosdb_entity_datatype *out);
// VALUE CONSTRUCTORS (resolve overloaded constructors)
int caosdb_entity_create_value(caosdb_entity_value *out, caosdb_entity_value *in);
int caosdb_entity_create_int_value(caosdb_entity_value *out, const int64_t value);
int caosdb_entity_create_string_value(caosdb_entity_value *out, const char *value);
int caosdb_entity_create_double_value(caosdb_entity_value *out, const double value);
......
......@@ -23,6 +23,7 @@
#include "caosdb/connection.h"
#include "caosdb/constants.h"
#include "caosdb/data_type.h" // for DataType, AtomicDat...
#include "caosdb/value.h"
#include "caosdb/utility.h"
#include "caosdb/status_code.h"
#include "caosdb/logging.h"
......@@ -46,6 +47,8 @@ extern "C" {
#define WRAPPED_MESSAGE_CAST(name) static_cast<caosdb::entity::Message *>(name->wrapped_message)
#define WRAPPED_VALUE_CAST(name) static_cast<caosdb::entity::Value *>(name->wrapped_value)
#define ENUM_NAME_FROM_VALUE(arg, etype) \
caosdb::utility::getEnumNameFromValue<caosdb::entity::etype>(arg)
......@@ -146,6 +149,38 @@ extern "C" {
body_part return 0; \
})
/**
* Macro for scalar value creators
*/
#define CREATE_VALUE(fname, arg) \
ERROR_RETURN_CODE(GENERIC_ERROR, \
int caosdb_entity_create_##fname(caosdb_entity_value *out, arg), { \
if (out->_deletable) { \
return caosdb::StatusCode::EXTERN_C_ASSIGNMENT_ERROR; \
} \
out->wrapped_value = new caosdb::entity::Value(value); \
out->_deletable = true; \
return 0; \
})
/**
* Macro for list-value creators
*/
#define CREATE_LIST_VALUE(fname, type, arg, assign) \
ERROR_RETURN_CODE( \
GENERIC_ERROR, \
int caosdb_entity_create_##fname(caosdb_entity_value *out, arg, const int length), { \
if (out->_deletable) { \
return caosdb::StatusCode::EXTERN_C_ASSIGNMENT_ERROR; \
} \
std::vector<type> value_vec; \
for (int i = 0; i < length; i++) { \
value_vec.push_back(assign); \
} \
out->wrapped_value = new caosdb::entity::Value(value_vec); \
out->_deletable = true; \
return 0; \
})
int caosdb_constants_LIBCAOSDB_VERSION_MAJOR() { return caosdb::LIBCAOSDB_VERSION_MAJOR; }
int caosdb_constants_LIBCAOSDB_VERSION_MINOR() { return caosdb::LIBCAOSDB_VERSION_MINOR; }
......@@ -625,6 +660,23 @@ ERROR_RETURN_CODE(GENERIC_ERROR, int caosdb_entity_delete_parent(caosdb_entity_p
return 0;
})
CREATE_VALUE(int_value, const int64_t value)
CREATE_VALUE(string_value, const char *value)
CREATE_VALUE(double_value, const double value)
CREATE_VALUE(bool_value, const bool value)
CREATE_LIST_VALUE(int_list_value, int64_t, const int64_t *value, value[i])
CREATE_LIST_VALUE(string_list_value, std::string, const char **value, std::string(value[i]))
CREATE_LIST_VALUE(double_list_value, double, const double *value, value[i])
CREATE_LIST_VALUE(bool_list_value, bool, const bool *value, value[i])
ERROR_RETURN_CODE(GENERIC_ERROR, int caosdb_entity_delete_value(caosdb_entity_value *out), {
if (out->_deletable) {
delete WRAPPED_VALUE_CAST(out);
}
out->_deletable = false;
return 0;
})
CAOSDB_ENTITY_GET(id, GetId())
ERROR_RETURN_CODE(GENERIC_ERROR,
int caosdb_entity_entity_get_role(caosdb_entity_entity *entity, char **out), {
......
......@@ -127,6 +127,36 @@ TEST_F(test_ccaosdb, test_query) {
EXPECT_EQ(return_code, 0);
}
TEST_F(test_ccaosdb, test_value) {
caosdb_entity_value string_value;
int return_code(caosdb_entity_create_string_value(&string_value, "value"));
EXPECT_EQ(return_code, 0);
caosdb_entity_value int_value;
return_code = caosdb_entity_create_int_value(&int_value, 27);
EXPECT_EQ(return_code, 0);
caosdb_entity_value bool_value;
return_code = caosdb_entity_create_bool_value(&bool_value, true);
EXPECT_EQ(return_code, 0);
caosdb_entity_value double_value;
return_code = caosdb_entity_create_double_value(&double_value, 2.7);
EXPECT_EQ(return_code, 0);
// TODO(fspreck) Test is... and as... functions
return_code = caosdb_entity_delete_value(&string_value);
EXPECT_EQ(return_code, 0);
return_code = caosdb_entity_delete_value(&int_value);
EXPECT_EQ(return_code, 0);
return_code = caosdb_entity_delete_value(&bool_value);
EXPECT_EQ(return_code, 0);
return_code = caosdb_entity_delete_value(&double_value);
EXPECT_EQ(return_code, 0);
}
TEST_F(test_ccaosdb, test_entity) {
caosdb_entity_entity entity;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment