// Daemon
collecty_daemon* daemon;
+
+ // Implementation
+ const collecty_graph_impl* impl;
};
static void collecty_graph_free(collecty_graph* self) {
}
int collecty_graph_create(collecty_graph** graph,
- collecty_ctx* ctx, collecty_daemon* daemon) {
+ collecty_ctx* ctx, collecty_daemon* daemon, const collecty_graph_impl* impl) {
collecty_graph* self = NULL;
// Allocate some memory
// Store a reference to the daemon
self->daemon = collecty_daemon_ref(daemon);
+ // Store the implementation
+ self->impl = impl;
+
// Return the pointer
*graph = self;
return 0;
#include "ctx.h"
#include "daemon.h"
-int collecty_graph_create(collecty_graph** graph, collecty_ctx* ctx, collecty_daemon* daemon);
+typedef struct collecty_graph_impl {
+ const char* name;
+
+ // Available
+ int (*available)(collecty_ctx* ctx, collecty_daemon* daemon);
+} collecty_graph_impl;
+
+int collecty_graph_create(collecty_graph** graph,
+ collecty_ctx* ctx, collecty_daemon* daemon, const collecty_graph_impl* impl);
collecty_graph* collecty_graph_ref(collecty_graph* self);
collecty_graph* collecty_graph_unref(collecty_graph* self);
#include "ctx.h"
#include "daemon.h"
+#include "graph.h"
#include "graphs.h"
+// Register all graphs
+static const collecty_graph_impl* graph_impls[] = {
+ NULL,
+};
+
struct collecty_graphs {
collecty_ctx* ctx;
int nrefs;
// Daemon
collecty_daemon* daemon;
+
+ // Graphs
+ collecty_graph** graphs;
+ unsigned int num_graphs;
};
+static int collecty_graphs_init(collecty_graphs* self) {
+ collecty_graph** graphs = NULL;
+ collecty_graph* graph = NULL;
+ int r;
+
+ // Initialize all graphs
+ for (const collecty_graph_impl** impl = graph_impls; *impl; impl++) {
+ r = collecty_graph_create(&graph, self->ctx, self->daemon, *impl);
+ if (r < 0)
+ return r;
+
+ // Make space to store the graph
+ graphs = reallocarray(self->graphs, self->num_graphs + 1, sizeof(*self->graphs));
+ if (!graphs) {
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Store a reference to the graph in the array
+ graphs[self->num_graphs++] = collecty_graph_ref(graph);
+
+ // Replace the array
+ self->graphs = graphs;
+
+ // Unref the graph
+ collecty_graph_unref(graph);
+ graph = NULL;
+ }
+
+ return 0;
+
+ERROR:
+ if (graph)
+ collecty_graph_unref(graph);
+
+ return r;
+}
+
static void collecty_graphs_free(collecty_graphs* self) {
+ if (self->graphs) {
+ for (unsigned int i = 0; i < self->num_graphs; i++)
+ collecty_graph_unref(self->graphs[i]);
+ free(self->graphs);
+ }
if (self->daemon)
collecty_daemon_unref(self->daemon);
if (self->ctx)
int collecty_graphs_create(collecty_graphs** graphs,
collecty_ctx* ctx, collecty_daemon* daemon) {
collecty_graphs* self = NULL;
+ int r;
// Allocate some memory
self = calloc(1, sizeof(*self));
// Store a reference to the daemon
self->daemon = collecty_daemon_ref(daemon);
+ // Setup all graphs
+ r = collecty_graphs_init(self);
+ if (r < 0)
+ goto ERROR;
+
// Return the pointer
*graphs = self;
return 0;
+
+ERROR:
+ if (self)
+ collecty_graphs_unref(self);
+
+ return r;
}
collecty_graphs* collecty_graphs_ref(collecty_graphs* self) {