Home > Net >  Is there a way to use designated initializers to initialize parts of the same array in separate file
Is there a way to use designated initializers to initialize parts of the same array in separate file

Time:08-17

I have thousands of "commands" that are all defined in their own files, and I want to be able to do things with them programmatically using an enum as a key.

The only way I can think of to do this is to have an externally linked initialization function for each "command" that is called by some other code to add a function pointer to a data structure. This sucks because then I have to declare these initialization functions in a header, define them in the individual files, and call them from somewhere else.

This method adds several lines of boiler plate for each "command," which is made worse because the boilerplate is spread across several files.

If it is possible, I'd like to have a single const array of function pointers to these static functions. Is there anything in the C99 standard that would allow me to do this?

The following code is a simplified representation of my problem, and some of it is definitely wrong, because I don't know how to do this or if it is even possible in C99.

Header included in all files

typedef enum
{
    EXAMPLE_ENUM_0
    EXAMPLE_ENUM_1
    EXAMPLE_ENUM_2
    EXAMPLE_ENUM_MAX_NUM_T,
} example_enum_t;

typedef void (*p_func_t)(void);

File 0

static void static_function_0(void)
{
    void * p_peripheral = 0x00000200;
    *p_peripheral = PERIPH_0_CONFIG;
}

extern int initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {
    [EXAMPLE_ENUM_0] = &static_function_0
};

File 1

static void static_function_1(void)
{
    void * p_peripheral = 0x00000300;
    *p_peripheral = PERIPH_1_CONFIG;
}

extern int initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {
    [EXAMPLE_ENUM_1] = &static_function_1
};

File 2

static void static_function_2(void)
{
    void * p_peripheral = 0x00000400;
    *p_peripheral = PERIPH_2_CONFIG;
}

extern int initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {
    [EXAMPLE_ENUM_2] = &static_function_2
};

File where I want to use all the function pointers

p_func_t initialized_in_multiple_files[EXAMPLE_ENUM_MAX_NUM_T] = {0U};

for (uint32_t index = 0U; index < EXAMPLE_ENUM_MAX_NUM_T; index  = 1U)
{
    p_func_t p_func = initialized_in_multiple_files[index];
    if (NULL != p_func)
    {
        p_func();
    }
}

CodePudding user response:

Is there a way to use designated initializers to initialize parts of the same array in separate files?

There can be many declarations of the same object, but only one definition. A declaration that initializes the object is a definition. Designated initializers do not alter this. Moreover, there is no partial initialization. Any initializer for an object initializes the whole object.

I'd like to have a single const array of function pointers to these static functions. Is there anything in the C99 standard that would allow me to do this?

No, not if the functions have internal linkage and are defined in separate files. However, you can work around the constness part by declaring a corresponding pointer to const elements in the header but defining the array itself non-const. Code in the translation unit where it is defined could then pass it (decayed to a pointer) to various functions to fill in its elements, but code in other translation units, seeing its const declaration in the header, would not be able to alter its contents (at least, not without casting away the constness).

Alternatively, if you give the functions external linkage then you could write one big initializer in the file of your choice.

Either way, you might be able to write a code generator to help you. That could significantly reduce the amount of boilerplate code you have to maintain manually.

  • Related