Commit 86993dec authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼

refactor enums to class enums

parent 719ad763
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
#include <villas/common.h> #include <villas/common.h>
struct buffer { struct buffer {
enum state state; enum State state;
char *buf; char *buf;
size_t len; size_t len;
......
...@@ -24,25 +24,25 @@ ...@@ -24,25 +24,25 @@
#pragma once #pragma once
/* Common states for most objects in VILLAScommon (paths, nodes, hooks, plugins) */ /* Common states for most objects in VILLAScommon (paths, nodes, hooks, plugins) */
enum state { enum class State {
STATE_DESTROYED = 0, DESTROYED = 0,
STATE_INITIALIZED = 1, INITIALIZED = 1,
STATE_PARSED = 2, PARSED = 2,
STATE_CHECKED = 3, CHECKED = 3,
STATE_STARTED = 4, STARTED = 4,
STATE_LOADED = 4, /* alias for STATE_STARTED used by struct plugin */ LOADED = 4, /* alias for STARTED used by struct plugin */
STATE_OPENED = 4, /* alias for STATE_STARTED used by struct io */ OPENED = 4, /* alias for STARTED used by struct io */
STATE_STOPPED = 5, STOPPED = 5,
STATE_UNLOADED = 5, /* alias for STATE_STARTED used by struct plugin */ UNLOADED = 5, /* alias for STARTED used by struct plugin */
STATE_CLOSED = 5, /* alias for STATE_STARTED used by struct io */ CLOSED = 5, /* alias for STARTED used by struct io */
STATE_PENDING_CONNECT = 6, PENDING_CONNECT = 6,
STATE_CONNECTED = 7, CONNECTED = 7,
STATE_PAUSED = 8, PAUSED = 8,
STATE_STARTING = 9, STARTING = 9,
STATE_STOPPING = 10, STOPPING = 10,
STATE_PAUSING = 11, PAUSING = 11,
STATE_RESUMING = 12, RESUMING = 12,
STATE_PREPARED = 13 PREPARED = 13
}; };
/** Callback to destroy list elements. /** Callback to destroy list elements.
...@@ -52,4 +52,4 @@ enum state { ...@@ -52,4 +52,4 @@ enum state {
typedef int (*dtor_cb_t)(void *); typedef int (*dtor_cb_t)(void *);
/** Convert state enum to human readable string. */ /** Convert state enum to human readable string. */
const char * state_print(enum state s); const char * state_print(enum State s);
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
/** Static list initialization */ /** Static list initialization */
#define LIST_INIT_STATIC(l) \ #define LIST_INIT_STATIC(l) \
__attribute__((constructor(105))) static void UNIQUE(__ctor)() {\ __attribute__((constructor(105))) static void UNIQUE(__ctor)() {\
if ((l)->state == STATE_DESTROYED) \ if ((l)->state == State::DESTROYED) \
vlist_init(l); \ vlist_init(l); \
} \ } \
__attribute__((destructor(105))) static void UNIQUE(__dtor)() { \ __attribute__((destructor(105))) static void UNIQUE(__dtor)() { \
...@@ -58,7 +58,7 @@ typedef int (*cmp_cb_t)(const void *, const void *); ...@@ -58,7 +58,7 @@ typedef int (*cmp_cb_t)(const void *, const void *);
/* The list data structure. */ /* The list data structure. */
struct vlist { struct vlist {
enum state state; /**< The state of this list. */ enum State state; /**< The state of this list. */
void **array; /**< Array of pointers to list elements */ void **array; /**< Array of pointers to list elements */
size_t capacity; /**< Size of list::array in elements */ size_t capacity; /**< Size of list::array in elements */
size_t length; /**< Number of elements of list::array which are in use */ size_t length; /**< Number of elements of list::array which are in use */
......
...@@ -37,7 +37,7 @@ class TableColumn { ...@@ -37,7 +37,7 @@ class TableColumn {
friend Table; friend Table;
public: public:
enum align { enum class Alignment {
LEFT, LEFT,
RIGHT RIGHT
}; };
...@@ -48,20 +48,20 @@ protected: ...@@ -48,20 +48,20 @@ protected:
int width; /**< Width of the column. */ int width; /**< Width of the column. */
public: public:
TableColumn(int w, enum align a, const std::string &t, const std::string &f, const std::string &u = "") : TableColumn(int w, enum Alignment a, const std::string &t, const std::string &f, const std::string &u = "") :
width(w), width(w),
title(t), title(t),
format(f), format(f),
unit(u), unit(u),
align(a) align(a)
{ } { }
std::string title; /**< The title as shown in the table header. */ std::string title; /**< The title as shown in the table header. */
std::string format; /**< The format which is used to print the table rows. */ std::string format; /**< The format which is used to print the table rows. */
std::string unit; /**< An optional unit which will be shown in the table header. */ std::string unit; /**< An optional unit which will be shown in the table header. */
enum align align; enum Alignment align;
int getWidth() const int getWidth() const
{ {
return _width; return _width;
...@@ -74,9 +74,9 @@ protected: ...@@ -74,9 +74,9 @@ protected:
int resize(int w); int resize(int w);
int width; int width;
std::vector<TableColumn> columns; std::vector<TableColumn> columns;
public: public:
Table(const std::vector<TableColumn> &cols) : Table(const std::vector<TableColumn> &cols) :
width(-1), width(-1),
......
...@@ -34,7 +34,7 @@ int buffer_init(struct buffer *b, size_t size) ...@@ -34,7 +34,7 @@ int buffer_init(struct buffer *b, size_t size)
if (!b->buf) if (!b->buf)
return -1; return -1;
b->state = STATE_INITIALIZED; b->state = State::INITIALIZED;
return 0; return 0;
} }
...@@ -44,7 +44,7 @@ int buffer_destroy(struct buffer *b) ...@@ -44,7 +44,7 @@ int buffer_destroy(struct buffer *b)
if (b->buf) if (b->buf)
free(b->buf); free(b->buf);
b->state = STATE_DESTROYED; b->state = State::DESTROYED;
return 0; return 0;
} }
......
...@@ -25,42 +25,42 @@ ...@@ -25,42 +25,42 @@
#include <stdlib.h> #include <stdlib.h>
const char * state_print(enum state s) const char * state_print(enum State s)
{ {
switch (s) { switch (s) {
case STATE_DESTROYED: case State::DESTROYED:
return "destroyed"; return "destroyed";
break; break;
case STATE_INITIALIZED: case State::INITIALIZED:
return "initialized"; return "initialized";
break; break;
case STATE_PARSED: case State::PARSED:
return "parsed"; return "parsed";
break; break;
case STATE_CHECKED: case State::CHECKED:
return "checked"; return "checked";
break; break;
case STATE_STARTED: case State::STARTED:
return "running"; return "running";
break; break;
case STATE_STOPPED: case State::STOPPED:
return "stopped"; return "stopped";
break; break;
case STATE_PENDING_CONNECT: case State::PENDING_CONNECT:
return "pending-connect"; return "pending-connect";
break; break;
case STATE_CONNECTED: case State::CONNECTED:
return "connected"; return "connected";
break; break;
case STATE_PAUSED: case State::PAUSED:
return "paused"; return "paused";
break; break;
......
...@@ -165,9 +165,9 @@ void Hist::plot() const ...@@ -165,9 +165,9 @@ void Hist::plot() const
} }
std::vector<TableColumn> cols = { std::vector<TableColumn> cols = {
{ -9, TableColumn::align::RIGHT, "Value", "%+9.3g" }, { -9, TableColumn::Alignment::RIGHT, "Value", "%+9.3g" },
{ -6, TableColumn::align::RIGHT, "Count", "%6ju" }, { -6, TableColumn::Alignment::RIGHT, "Count", "%6ju" },
{ 0, TableColumn::align::LEFT, "Plot", "%s", "occurences" } { 0, TableColumn::Alignment::LEFT, "Plot", "%s", "occurences" }
}; };
Table table = Table(cols); Table table = Table(cols);
......
...@@ -55,14 +55,14 @@ static int cmp_sort(const void *a, const void *b, void *thunk) { ...@@ -55,14 +55,14 @@ static int cmp_sort(const void *a, const void *b, void *thunk) {
int vlist_init(struct vlist *l) int vlist_init(struct vlist *l)
{ {
assert(l->state == STATE_DESTROYED); assert(l->state == State::DESTROYED);
pthread_mutex_init(&l->lock, nullptr); pthread_mutex_init(&l->lock, nullptr);
l->length = 0; l->length = 0;
l->capacity = 0; l->capacity = 0;
l->array = nullptr; l->array = nullptr;
l->state = STATE_INITIALIZED; l->state = State::INITIALIZED;
return 0; return 0;
} }
...@@ -71,7 +71,7 @@ int vlist_destroy(struct vlist *l, dtor_cb_t destructor, bool release) ...@@ -71,7 +71,7 @@ int vlist_destroy(struct vlist *l, dtor_cb_t destructor, bool release)
{ {
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state != STATE_DESTROYED); assert(l->state != State::DESTROYED);
for (size_t i = 0; i < vlist_length(l); i++) { for (size_t i = 0; i < vlist_length(l); i++) {
void *e = vlist_at(l, i); void *e = vlist_at(l, i);
...@@ -87,7 +87,7 @@ int vlist_destroy(struct vlist *l, dtor_cb_t destructor, bool release) ...@@ -87,7 +87,7 @@ int vlist_destroy(struct vlist *l, dtor_cb_t destructor, bool release)
l->length = -1; l->length = -1;
l->capacity = 0; l->capacity = 0;
l->array = nullptr; l->array = nullptr;
l->state = STATE_DESTROYED; l->state = State::DESTROYED;
pthread_mutex_unlock(&l->lock); pthread_mutex_unlock(&l->lock);
pthread_mutex_destroy(&l->lock); pthread_mutex_destroy(&l->lock);
...@@ -99,7 +99,7 @@ void vlist_push(struct vlist *l, void *p) ...@@ -99,7 +99,7 @@ void vlist_push(struct vlist *l, void *p)
{ {
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
/* Resize array if out of capacity */ /* Resize array if out of capacity */
if (l->length >= l->capacity) { if (l->length >= l->capacity) {
...@@ -117,7 +117,7 @@ int vlist_remove(struct vlist *l, size_t idx) ...@@ -117,7 +117,7 @@ int vlist_remove(struct vlist *l, size_t idx)
{ {
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
if (idx >= l->length) if (idx >= l->length)
return -1; return -1;
...@@ -139,7 +139,7 @@ int vlist_insert(struct vlist *l, size_t idx, void *p) ...@@ -139,7 +139,7 @@ int vlist_insert(struct vlist *l, size_t idx, void *p)
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
if (idx >= l->length) if (idx >= l->length)
return -1; return -1;
...@@ -171,7 +171,7 @@ void vlist_remove_all(struct vlist *l, void *p) ...@@ -171,7 +171,7 @@ void vlist_remove_all(struct vlist *l, void *p)
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
for (size_t i = 0; i < vlist_length(l); i++) { for (size_t i = 0; i < vlist_length(l); i++) {
if (vlist_at(l, i) == p) if (vlist_at(l, i) == p)
...@@ -211,7 +211,7 @@ int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx) ...@@ -211,7 +211,7 @@ int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx)
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
for (size_t i = 0; i < vlist_length(l); i++) { for (size_t i = 0; i < vlist_length(l); i++) {
e = vlist_at(l, i); e = vlist_at(l, i);
...@@ -230,7 +230,7 @@ void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx) ...@@ -230,7 +230,7 @@ void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx)
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
for (size_t i = 0; i < vlist_length(l); i++) { for (size_t i = 0; i < vlist_length(l); i++) {
e = vlist_at(l, i); e = vlist_at(l, i);
...@@ -249,7 +249,7 @@ void vlist_sort(struct vlist *l, cmp_cb_t cmp) ...@@ -249,7 +249,7 @@ void vlist_sort(struct vlist *l, cmp_cb_t cmp)
{ {
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
#ifdef __APPLE__ #ifdef __APPLE__
qsort_r(l->array, l->length, sizeof(void *), (void *) cmp, cmp_sort); qsort_r(l->array, l->length, sizeof(void *), (void *) cmp, cmp_sort);
...@@ -277,7 +277,7 @@ ssize_t vlist_index(struct vlist *l, void *p) ...@@ -277,7 +277,7 @@ ssize_t vlist_index(struct vlist *l, void *p)
pthread_mutex_lock(&l->lock); pthread_mutex_lock(&l->lock);
assert(l->state == STATE_INITIALIZED); assert(l->state == State::INITIALIZED);
for (size_t i = 0; i < vlist_length(l); i++) { for (size_t i = 0; i < vlist_length(l); i++) {
e = vlist_at(l, i); e = vlist_at(l, i);
......
...@@ -55,7 +55,7 @@ Plugin::parse(json_t *cfg) ...@@ -55,7 +55,7 @@ Plugin::parse(json_t *cfg)
return -1; return -1;
this->path = std::string(path); this->path = std::string(path);
this->state = STATE_PARSED; this->state = State::PARSED;
return 0; return 0;
} }
...@@ -63,7 +63,7 @@ Plugin::parse(json_t *cfg) ...@@ -63,7 +63,7 @@ Plugin::parse(json_t *cfg)
int int
Plugin::load() Plugin::load()
{ {
assert(this->state == STATE_PARSED); assert(this->state == State::PARSED);
assert(not this->path.empty()); assert(not this->path.empty());
this->handle = dlopen(this->path.c_str(), RTLD_NOW); this->handle = dlopen(this->path.c_str(), RTLD_NOW);
...@@ -71,7 +71,7 @@ Plugin::load() ...@@ -71,7 +71,7 @@ Plugin::load()
if (this->handle == nullptr) if (this->handle == nullptr)
return -1; return -1;
this->state = STATE_LOADED; this->state = State::LOADED;
return 0; return 0;
} }
...@@ -81,13 +81,13 @@ Plugin::unload() ...@@ -81,13 +81,13 @@ Plugin::unload()
{ {
int ret; int ret;
assert(this->state == STATE_LOADED); assert(this->state == State::LOADED);
ret = dlclose(this->handle); ret = dlclose(this->handle);
if (ret != 0) if (ret != 0)
return -1; return -1;
this->state = STATE_UNLOADED; this->state = State::UNLOADED;
return 0; return 0;
} }
......
...@@ -141,8 +141,13 @@ void Popen::open() ...@@ -141,8 +141,13 @@ void Popen::open()
} }
/* Change working directory */ /* Change working directory */
if (!working_dir.empty()) if (!working_dir.empty()) {
chdir(working_dir.c_str()); int ret;
ret = chdir(working_dir.c_str());
if (ret)
exit(127);
}
execvpe(shell ? _PATH_BSHELL : command.c_str(), (char * const *) argv.data(), (char * const *) envp.data()); execvpe(shell ? _PATH_BSHELL : command.c_str(), (char * const *) argv.data(), (char * const *) envp.data());
exit(127); exit(127);
......
...@@ -83,7 +83,7 @@ void Table::header() ...@@ -83,7 +83,7 @@ void Table::header()
w = columns[i]._width + strlen(col) - strlenp(col); w = columns[i]._width + strlen(col) - strlenp(col);
u = columns[i]._width + strlen(unit) - strlenp(unit); u = columns[i]._width + strlen(unit) - strlenp(unit);
if (columns[i].align == TableColumn::align::LEFT) { if (columns[i].align == TableColumn::Alignment::LEFT) {
strcatf(&line1, " %-*.*s\e[0m", w, w, col); strcatf(&line1, " %-*.*s\e[0m", w, w, col);
strcatf(&line2, " %-*.*s\e[0m", u, u, unit); strcatf(&line2, " %-*.*s\e[0m", u, u, unit);
} }
...@@ -133,7 +133,7 @@ void Table::row(int count, ...) ...@@ -133,7 +133,7 @@ void Table::row(int count, ...)
int r = strlen(col); int r = strlen(col);
int w = columns[i]._width + r - l; int w = columns[i]._width + r - l;
if (columns[i].align == TableColumn::align::LEFT) if (columns[i].align == TableColumn::Alignment::LEFT)
strcatf(&line, " %-*.*s\e[0m ", w, w, col); strcatf(&line, " %-*.*s\e[0m ", w, w, col);
else else
strcatf(&line, " %*.*s\e[0m ", w, w, col); strcatf(&line, " %*.*s\e[0m ", w, w, col);
......
...@@ -39,7 +39,7 @@ TestSuite(list, .description = "List datastructure"); ...@@ -39,7 +39,7 @@ TestSuite(list, .description = "List datastructure");
Test(list, vlist_lookup) Test(list, vlist_lookup)
{ {
struct vlist l; struct vlist l;
l.state = STATE_DESTROYED; l.state = State::DESTROYED;
vlist_init(&l); vlist_init(&l);
...@@ -62,7 +62,7 @@ Test(list, vlist_lookup) ...@@ -62,7 +62,7 @@ Test(list, vlist_lookup)
Test(list, vlist_search) Test(list, vlist_search)
{ {
struct vlist l; struct vlist l;
l.state = STATE_DESTROYED; l.state = State::DESTROYED;
vlist_init(&l); vlist_init(&l);
...@@ -103,7 +103,7 @@ static int dtor(void *ptr) ...@@ -103,7 +103,7 @@ static int dtor(void *ptr)
Test(list, destructor) Test(list, destructor)
{ {
struct vlist l; struct vlist l;
l.state = STATE_DESTROYED; l.state = State::DESTROYED;
struct content elm; struct content elm;
elm.destroyed = 0; elm.destroyed = 0;
...@@ -123,7 +123,7 @@ Test(list, basics) ...@@ -123,7 +123,7 @@ Test(list, basics)
uintptr_t i; uintptr_t i;
int ret; int ret;
struct vlist l; struct vlist l;
l.state = STATE_DESTROYED; l.state = State::DESTROYED;
vlist_init(&l); vlist_init(&l);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment