Wrap constant and predicate registries into a single environment type

This commit is contained in:
Vftdan 2024-08-17 11:02:26 +02:00
parent 011772a24a
commit 2676b5acdf
10 changed files with 47 additions and 16 deletions

View File

@ -449,3 +449,15 @@ resolve_event_predicate(EventPredicateHandleRegistry * registry, const ConstantR
}
return load_single_predicate(setting, registry, constants);
}
long long
env_resolve_constant_or(InitializationEnvironment * env, const config_setting_t * setting, long long dflt)
{
return resolve_constant_or(&env->constants, setting, dflt);
}
EventPredicateHandle
env_resolve_event_predicate(InitializationEnvironment * env, const config_setting_t * setting)
{
return resolve_event_predicate(&env->predicates, &env->constants, setting);
}

View File

@ -18,7 +18,7 @@ predicates = {
max = "event_type.KEY";
}
);
}
};
};
nodes = {

View File

@ -31,12 +31,21 @@ typedef struct {
typedef TYPED_HASH_TABLE(long long) ConstantRegistry;
typedef TYPED_HASH_TABLE(EventPredicateHandle) EventPredicateHandleRegistry;
typedef struct initialization_environment InitializationEnvironment;
typedef struct {
GraphNodeConfigSection nodes;
GraphChannelConfigSection channels;
ConstantRegistry constants;
EventPredicateHandleRegistry predicates;
union {
struct {
ConstantRegistry constants;
EventPredicateHandleRegistry predicates;
};
struct initialization_environment {
const ConstantRegistry constants;
EventPredicateHandleRegistry predicates;
} environment;
};
} FullConfig;
bool load_config(const config_setting_t *config_root, FullConfig *config);
@ -44,10 +53,20 @@ void reset_config(FullConfig *config);
long long resolve_constant_or(const ConstantRegistry * registry, const config_setting_t * setting, long long dflt);
EventPredicateHandle resolve_event_predicate(EventPredicateHandleRegistry * registry, const ConstantRegistry * constants, const config_setting_t * setting);
// These are not inline to make non-breaking ABI changes
long long env_resolve_constant_or(InitializationEnvironment * env, const config_setting_t * setting, long long dflt);
EventPredicateHandle env_resolve_event_predicate(InitializationEnvironment * env, const config_setting_t * setting);
__attribute__((unused)) inline static long long
resolve_constant(const ConstantRegistry * registry, const config_setting_t * setting)
{
return resolve_constant_or(registry, setting, 0);
}
__attribute__((unused)) inline static long long
env_resolve_constant(InitializationEnvironment * env, const config_setting_t * setting)
{
return env_resolve_constant_or(env, setting, 0);
}
#endif /* end of include guard: CONFIG_H_ */

View File

@ -88,12 +88,12 @@ graph_channel_init(GraphChannel * ch, GraphNode * start, size_t start_idx, Graph
}
GraphNode *
graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
if (!spec || !spec->create) {
return NULL;
}
return spec->create(spec, config, constants);
return spec->create(spec, config, env);
}
void

View File

@ -29,14 +29,14 @@ struct graph_channel {
};
struct graph_node_specification {
GraphNode * (*create)(GraphNodeSpecification * self, GraphNodeConfig * config, const ConstantRegistry * constants);
GraphNode * (*create)(GraphNodeSpecification * self, GraphNodeConfig * config, InitializationEnvironment * env);
void (*destroy)(GraphNodeSpecification * self, GraphNode * target);
void (*register_io)(GraphNodeSpecification * self, GraphNode * target, ProcessingState * state);
char *name;
};
void graph_channel_init(GraphChannel * ch, GraphNode * start, size_t start_idx, GraphNode * end, size_t end_idx);
GraphNode *graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants);
GraphNode *graph_node_new(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env);
void graph_node_delete(GraphNode * self);
void graph_node_register_io(GraphNode * self, ProcessingState * state);
void graph_channel_list_init(GraphChannelList * lst);

2
main.c
View File

@ -39,7 +39,7 @@ main(int argc, char ** argv)
fprintf(stderr, "Unknown node type \"%s\" for node %ld \"%s\"\n", type_name, i, loaded_config.nodes.items[i].name);
exit(1);
}
if (!(nodes[i] = graph_node_new(spec, &loaded_config.nodes.items[i], &loaded_config.constants))) {
if (!(nodes[i] = graph_node_new(spec, &loaded_config.nodes.items[i], &loaded_config.environment))) {
perror("Failed to create node");
fprintf(stderr, "%ld \"%s\"\n", i, loaded_config.nodes.items[i].name);
exit(1);

View File

@ -69,7 +69,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
EvdevGraphNode * node = T_ALLOC(1, EvdevGraphNode);
if (!node) {
@ -77,7 +77,7 @@ create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRe
}
const char *filename = NULL;
if (config) {
node->namespace = resolve_constant(constants, config_setting_get_member(config->options, "namespace"));
node->namespace = env_resolve_constant(env, config_setting_get_member(config->options, "namespace"));
config_setting_lookup_string(config->options, "file", &filename);
}
if (filename == NULL) {

View File

@ -49,7 +49,7 @@ handle_io(EventPositionBase * self, int fd, bool is_output)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
GetcharGraphNode * node = T_ALLOC(1, GetcharGraphNode);
if (!node) {
@ -70,7 +70,7 @@ create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRe
.handle_io = handle_io,
.enabled = true,
},
.namespace = resolve_constant(constants, config_setting_get_member(config->options, "namespace")),
.namespace = env_resolve_constant(env, config_setting_get_member(config->options, "namespace")),
};
return &node->as_GraphNode;
}

View File

@ -28,10 +28,10 @@ handle_event(EventPositionBase * self, EventNode * event)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
(void) config;
(void) constants;
(void) env;
GraphNode * node = T_ALLOC(1, GraphNode);
if (!node) {
return node;

View File

@ -21,10 +21,10 @@ handle_event(EventPositionBase * self, EventNode * event)
}
static GraphNode *
create(GraphNodeSpecification * spec, GraphNodeConfig * config, const ConstantRegistry * constants)
create(GraphNodeSpecification * spec, GraphNodeConfig * config, InitializationEnvironment * env)
{
(void) config;
(void) constants;
(void) env;
GraphNode * node = T_ALLOC(1, GraphNode);
if (!node) {
return node;