From d8447664dd337af337cf7942374b0b2d2fcc4af6 Mon Sep 17 00:00:00 2001 From: Manuel Pitz Date: Tue, 9 Apr 2019 14:27:55 +0200 Subject: [PATCH] UPDATE formts init now compatible to gcc 6.3 c++ --- include/villas/formats/msg_format.h | 11 ++--- lib/formats/CMakeLists.txt | 1 - lib/formats/csv.cpp | 75 +++++++++++++++++------------ lib/formats/json.cpp | 38 +++++++++------ lib/formats/json_reserve.cpp | 34 ++++++++----- lib/formats/msg.cpp | 12 ++--- lib/formats/protobuf.cpp | 39 ++++++++------- lib/formats/raw.cpp | 35 ++++++++------ lib/formats/villas_binary.cpp | 67 +++++++++++++++++--------- lib/formats/villas_human.cpp | 37 ++++++++------ 10 files changed, 207 insertions(+), 142 deletions(-) diff --git a/include/villas/formats/msg_format.h b/include/villas/formats/msg_format.h index 2cf3025f9..b0f85acd7 100644 --- a/include/villas/formats/msg_format.h +++ b/include/villas/formats/msg_format.h @@ -47,12 +47,11 @@ extern "C" { #define MSG_DATA_OFFSET(msg) ((char *) (msg) + offsetof(struct msg, data)) /** Initialize a message with default values */ -#define MSG_INIT(len, seq) (struct msg) {\ - .type = MSG_TYPE_DATA, \ - .version = MSG_VERSION, \ - .length = (uint16_t) (len), \ - .sequence = (uint32_t) (seq), \ -} +#define MSG_INIT(len, seq, i) \ + i->type = MSG_TYPE_DATA; \ + i->version = MSG_VERSION; \ + i->length = (uint16_t) (len); \ + i->sequence = (uint32_t) (seq); /** The timestamp of a message in struct timespec format */ #define MSG_TS(msg) (struct timespec) { \ diff --git a/lib/formats/CMakeLists.txt b/lib/formats/CMakeLists.txt index 71b0d3127..d8e3689cb 100644 --- a/lib/formats/CMakeLists.txt +++ b/lib/formats/CMakeLists.txt @@ -71,6 +71,5 @@ list(APPEND FORMAT_SRC ) add_library(formats STATIC ${FORMAT_SRC}) -target_compile_options(formats PRIVATE -x c) target_include_directories(formats PUBLIC ${INCLUDE_DIRS}) target_link_libraries(formats INTERFACE ${LIBRARIES} PUBLIC villas-common) diff --git a/lib/formats/csv.cpp b/lib/formats/csv.cpp index fcb5e5cff..671e5d501 100644 --- a/lib/formats/csv.cpp +++ b/lib/formats/csv.cpp @@ -197,35 +197,50 @@ void csv_header(struct io *io, const struct sample *smp) fprintf(f, "%c", io->delimiter); } -static struct plugin p1 = { - .name = "tsv", - .description = "Tabulator-separated values", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .header = csv_header, - .sprint = csv_sprint, - .sscan = csv_sscan, - .size = 0, - .flags = IO_NEWLINES | - SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA, - .separator = '\t' - } -}; +static struct plugin p1; +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); -static struct plugin p2 = { - .name = "csv", - .description = "Comma-separated values", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .header = csv_header, - .sprint = csv_sprint, - .sscan = csv_sscan, - .size = 0, - .flags = IO_NEWLINES | - SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA, - .separator = ',' - } -}; + p1.name = "tsv"; + p1.description = "Tabulator-separated values"; + p1.type = PLUGIN_TYPE_FORMAT; + p1.format.header = csv_header; + p1.format.sprint = csv_sprint; + p1.format.sscan = csv_sscan; + p1.format.size = 0; + p1.format.flags = IO_NEWLINES | + SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA; + p1.format.separator = '\t'; -REGISTER_PLUGIN(&p1); -REGISTER_PLUGIN(&p2); + vlist_push(&plugins, &p1); +} + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p1); +} + +static struct plugin p2; +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); + + p2.name = "csv"; + p2.description = "Comma-separated values"; + p2.type = PLUGIN_TYPE_FORMAT; + p1.format.header = csv_header; + p1.format.sprint = csv_sprint; + p1.format.sscan = csv_sscan; + p1.format.size = 0; + p1.format.flags = IO_NEWLINES | + SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA; + p1.format.separator = ','; + + vlist_push(&plugins, &p2); +} + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p2); +} diff --git a/lib/formats/json.cpp b/lib/formats/json.cpp index 3c2cbb444..086738e22 100644 --- a/lib/formats/json.cpp +++ b/lib/formats/json.cpp @@ -347,19 +347,27 @@ skip: json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err); return i; } -static struct plugin p = { - .name = "json", - .description = "Javascript Object Notation", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .print = json_print, - .scan = json_scan, - .sprint = json_sprint, - .sscan = json_sscan, - .size = 0, - .flags = SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA, - .delimiter = '\n' - }, -}; +static struct plugin p; -REGISTER_PLUGIN(&p); +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); + + p.name = "json"; + p.description = "Javascript Object Notation"; + p.type = PLUGIN_TYPE_FORMAT; + p.format.print = json_print; + p.format.scan = json_scan; + p.format.sprint = json_sprint; + p.format.sscan = json_sscan; + p.format.size = 0; + p.format.flags = SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA; + p.format.delimiter = '\n'; + + vlist_push(&plugins, &p); +} + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p); +} diff --git a/lib/formats/json_reserve.cpp b/lib/formats/json_reserve.cpp index 4b54a6ae6..9acc935e9 100644 --- a/lib/formats/json_reserve.cpp +++ b/lib/formats/json_reserve.cpp @@ -323,17 +323,25 @@ skip: json = json_loadf(f, JSON_DISABLE_EOF_CHECK, &err); return i; } -static struct plugin p = { - .name = "json.reserve", - .description = "RESERVE JSON format", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .print = json_reserve_print, - .scan = json_reserve_scan, - .sprint = json_reserve_sprint, - .sscan = json_reserve_sscan, - .size = 0 - }, -}; +static struct plugin p; -REGISTER_PLUGIN(&p); +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); + + p.name = "json.reserve"; + p.description = "RESERVE JSON format"; + p.type = PLUGIN_TYPE_FORMAT; + p.format.print = json_reserve_print; + p.format.scan = json_reserve_scan; + p.format.sprint = json_reserve_sprint; + p.format.sscan = json_reserve_sscan; + p.format.size = 0; + + vlist_push(&plugins, &p); +} + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p); +} diff --git a/lib/formats/msg.cpp b/lib/formats/msg.cpp index a53de7fbb..7ba65561b 100644 --- a/lib/formats/msg.cpp +++ b/lib/formats/msg.cpp @@ -106,23 +106,23 @@ int msg_to_sample(struct msg *msg, struct sample *smp, struct vlist *signals) return 0; } -int msg_from_sample(struct msg *msg, struct sample *smp, struct vlist *signals) +int msg_from_sample(struct msg *msgIn, struct sample *smp, struct vlist *signals) { - *msg = MSG_INIT(smp->length, smp->sequence); + MSG_INIT(smp->length, smp->sequence, msgIn); - msg->ts.sec = smp->ts.origin.tv_sec; - msg->ts.nsec = smp->ts.origin.tv_nsec; + msgIn->ts.sec = smp->ts.origin.tv_sec; + msgIn->ts.nsec = smp->ts.origin.tv_nsec; for (unsigned i = 0; i < smp->length; i++) { struct signal *sig = (struct signal *) vlist_at(signals, i); switch (sig->type) { case SIGNAL_TYPE_FLOAT: - msg->data[i].f = smp->data[i].f; + msgIn->data[i].f = smp->data[i].f; break; case SIGNAL_TYPE_INTEGER: - msg->data[i].i = smp->data[i].i; + msgIn->data[i].i = smp->data[i].i; break; default: diff --git a/lib/formats/protobuf.cpp b/lib/formats/protobuf.cpp index 6ffd0652a..a4ada3943 100644 --- a/lib/formats/protobuf.cpp +++ b/lib/formats/protobuf.cpp @@ -29,10 +29,6 @@ #include #include -#ifdef __cplusplus -extern "C" { -#endif - static enum signal_type protobuf_detect_format(Villas__Node__Value *val) { switch (val->value_case) { @@ -225,18 +221,25 @@ int protobuf_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, s return i; } -static struct plugin p = { - .name = "protobuf", - .description = "Google Protobuf", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .sprint = protobuf_sprint, - .sscan = protobuf_sscan, - .flags = IO_HAS_BINARY_PAYLOAD | - SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA - } -}; -REGISTER_PLUGIN(&p); -#ifdef __cplusplus +static struct plugin p; + +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); + + p.name = "protobuf"; + p.description = "Google Protobuf"; + p.type = PLUGIN_TYPE_FORMAT; + p.format.sprint = protobuf_sprint; + p.format.sscan = protobuf_sscan; + p.format.flags = IO_HAS_BINARY_PAYLOAD | + SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA; + + vlist_push(&plugins, &p); } -#endif + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p); +} + diff --git a/lib/formats/raw.cpp b/lib/formats/raw.cpp index 48d33acab..28a7749e8 100644 --- a/lib/formats/raw.cpp +++ b/lib/formats/raw.cpp @@ -408,20 +408,27 @@ int raw_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, struct return 1; } -#define REGISTER_FORMAT_RAW(i, n, d, f) \ -static struct plugin i = { \ - .name = n, \ - .description = d, \ - .type = PLUGIN_TYPE_FORMAT, \ - .format = { \ - .sprint = raw_sprint, \ - .sscan = raw_sscan, \ - .flags = f | IO_HAS_BINARY_PAYLOAD |\ - SAMPLE_HAS_DATA \ - } \ -}; \ -REGISTER_PLUGIN(& i); - +#define REGISTER_FORMAT_RAW(i, n, d, f) \ +static struct plugin i; \ +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { \ + if (plugins.state == STATE_DESTROYED) \ + vlist_init(&plugins); \ + \ + i.name = n; \ + i.description = d; \ + i.type = PLUGIN_TYPE_FORMAT; \ + i.format.sprint = raw_sprint; \ + i.format.sscan = raw_sscan; \ + i.format.flags = f | IO_HAS_BINARY_PAYLOAD | \ + SAMPLE_HAS_DATA; \ + \ + vlist_push(&plugins, &i); \ +} \ + \ +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { \ + if (plugins.state != STATE_DESTROYED) \ + vlist_remove_all(&plugins, &i); \ +} /* Feel free to add additional format identifiers here to suit your needs */ REGISTER_FORMAT_RAW(p_8, "raw.8", "Raw 8 bit", RAW_BITS_8) REGISTER_FORMAT_RAW(p_16be, "raw.16.be", "Raw 16 bit, big endian byte-order", RAW_BITS_16 | RAW_BIG_ENDIAN) diff --git a/lib/formats/villas_binary.cpp b/lib/formats/villas_binary.cpp index e7530dfeb..82d9e6481 100644 --- a/lib/formats/villas_binary.cpp +++ b/lib/formats/villas_binary.cpp @@ -118,32 +118,51 @@ int villas_binary_sscan(struct io *io, const char *buf, size_t len, size_t *rbyt return i; } -static struct plugin p1 = { - .name = "villas.binary", - .description = "VILLAS binary network format", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .sprint = villas_binary_sprint, - .sscan = villas_binary_sscan, - .size = 0, - .flags = IO_HAS_BINARY_PAYLOAD | - SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA - }, -}; +static struct plugin p1; +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); + + p1.name = "villas.binary"; + p1.description = "VILLAS binary network format"; + p1.type = PLUGIN_TYPE_FORMAT; + p1.format.sprint = villas_binary_sprint; + p1.format.sscan = villas_binary_sscan; + p1.format.size = 0; + p1.format.flags = IO_HAS_BINARY_PAYLOAD | + SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA; + + vlist_push(&plugins, &p1); +} + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p1); +} /** The WebSocket node-type usually uses little endian byte order intead of network byte order */ -static struct plugin p2 = { - .name = "villas.web", - .description = "VILLAS binary network format for WebSockets", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .sprint = villas_binary_sprint, - .sscan = villas_binary_sscan, - .size = 0, - .flags = IO_HAS_BINARY_PAYLOAD | VILLAS_BINARY_WEB | - SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA - }, -}; +static struct plugin p2; + +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); + + p2.name = "villas.web"; + p2.description = "VILLAS binary network format for WebSockets"; + p2.type = PLUGIN_TYPE_FORMAT; + p2.format.sprint = villas_binary_sprint; + p2.format.sscan = villas_binary_sscan; + p2.format.size = 0; + p2.format.flags = IO_HAS_BINARY_PAYLOAD | VILLAS_BINARY_WEB | + SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA; + + vlist_push(&plugins, &p2); +} + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p2); +} REGISTER_PLUGIN(&p1); REGISTER_PLUGIN(&p2); diff --git a/lib/formats/villas_human.cpp b/lib/formats/villas_human.cpp index 2e42e83e4..384aed550 100644 --- a/lib/formats/villas_human.cpp +++ b/lib/formats/villas_human.cpp @@ -224,19 +224,26 @@ void villas_human_header(struct io *io, const struct sample *smp) fprintf(f, "%c", io->delimiter); } -static struct plugin p = { - .name = "villas.human", - .description = "VILLAS human readable format", - .type = PLUGIN_TYPE_FORMAT, - .format = { - .header = villas_human_header, - .sprint = villas_human_sprint, - .sscan = villas_human_sscan, - .size = 0, - .flags = IO_NEWLINES | SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA, - .delimiter = '\n', - .separator = '\t' - } -}; +static struct plugin p; +__attribute__((constructor(110))) static void UNIQUE(__ctor)() { + if (plugins.state == STATE_DESTROYED) + vlist_init(&plugins); -REGISTER_PLUGIN(&p); + p.name = "villas.human"; + p.description = "VILLAS human readable format"; + p.type = PLUGIN_TYPE_FORMAT; + p.format.header = villas_human_header; + p.format.sprint = villas_human_sprint; + p.format.sscan = villas_human_sscan; + p.format.size = 0; + p.format.flags = IO_NEWLINES | SAMPLE_HAS_TS_ORIGIN | SAMPLE_HAS_SEQUENCE | SAMPLE_HAS_DATA; + p.format.delimiter = '\n'; + p.format.separator = '\t'; + + vlist_push(&plugins, &p); +} + +__attribute__((destructor(110))) static void UNIQUE(__dtor)() { + if (plugins.state != STATE_DESTROYED) + vlist_remove_all(&plugins, &p); +}