enum: Add functions to add and remove mappings from enum names

Co-authored-by: Thomas Egerer <[email protected]>
This commit is contained in:
Tobias Brunner
2023-02-17 13:37:38 +01:00
co-authored by Thomas Egerer
parent 3cf5653640
commit 0de42047a9
3 changed files with 160 additions and 1 deletions
+44
View File
@@ -1,4 +1,5 @@
/*
* Copyright (C) 2023 Tobias Brunner
* Copyright (C) 2006 Martin Willi
*
* Copyright (C) secunet Security Networks AG
@@ -23,6 +24,49 @@
#include "enum.h"
/*
* Described in header
*/
void enum_add_enum_names(enum_name_t *e, enum_name_t *names)
{
if (e)
{
do
{
if (!e->next)
{
e->next = names;
break;
}
else if (e->next == names)
{
break;
}
}
while ((e = e->next));
}
}
/*
* Described in header
*/
void enum_remove_enum_names(enum_name_t *e, enum_name_t *names)
{
if (e)
{
do
{
if (e->next == names)
{
e->next = names->next;
names->next = NULL;
break;
}
}
while ((e = e->next));
}
}
/**
* See header.
*/
+36 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2019 Tobias Brunner
* Copyright (C) 2009-2023 Tobias Brunner
* Copyright (C) 2006-2008 Martin Willi
*
* Copyright (C) secunet Security Networks AG
@@ -140,6 +140,41 @@ struct enum_name_t {
countof(((char*[]){__VA_ARGS__}))), \
ENUM_FLAG_MAGIC, { unset, __VA_ARGS__ }}; ENUM_END(name, last)
/**
* Define a static enum name that can be added and removed to an existing list
* via enum_add_enum_names() and enum_remove_enum_names(), respectively.
*
* @param name name of the static enum_name element
* @param first enum value of the first enum string
* @param last enum value of the last enum string
* @param ... a list of strings
*/
#define ENUM_EXT(name, first, last, ...) \
ENUM_BEGIN(name, first, last, __VA_ARGS__); static ENUM_END(name, last)
/**
* Register enum names for additional enum values with an existing enum name.
*
* @note Must be called while running single-threaded, e.g. when plugins and
* their features are loaded. Use enum_remove_enum_names() to remove the names
* during deinitialization.
*
* @param e enum names to add new names to
* @param names additional enum names
*/
void enum_add_enum_names(enum_name_t *e, enum_name_t *names);
/**
* Remove previously registered enum names.
*
* @note Must be called while running single-threaded, e.g. when plugins and
* their features are unloaded.
*
* @param e enum names to remove previously added names from
* @param names additional enum names to remove
*/
void enum_remove_enum_names(enum_name_t *e, enum_name_t *names);
/**
* Convert a enum value to its string representation.
*