Jan Roorda
Jan Roorda

Reputation: 143

C++ initializer list from other file

I have a question about the c++ initializer list.

I have a class with const structs that need to be initialized in the initializer list, because they're const. This works perfectly this way:

bglib::bglib()
    : ble_class_evt_handlers(   {{ble_class_system_evt_handlers,7},
                                {ble_class_flash_evt_handlers,1},
                                {ble_class_attributes_evt_handlers,3},
                                {ble_class_connection_evt_handlers,5},
                                {ble_class_attclient_evt_handlers,7},
                                {ble_class_sm_evt_handlers,5},
                                {ble_class_gap_evt_handlers,2},
                                {ble_class_hardware_evt_handlers,4},
                                {NULL,0},
                                {ble_class_dfu_evt_handlers,1},
                                } ),
      ble_class_rsp_handlers(   {{ble_class_system_rsp_handlers,18},
                                {ble_class_flash_rsp_handlers,9},
                                {ble_class_attributes_rsp_handlers,6},
                                {ble_class_connection_rsp_handlers,9},
                                {ble_class_attclient_rsp_handlers,12},
                                {ble_class_sm_rsp_handlers,8},
                                {ble_class_gap_rsp_handlers,11},
                                {ble_class_hardware_rsp_handlers,21},
                                {ble_class_test_rsp_handlers,7},
                                {ble_class_dfu_rsp_handlers,4},
                                }  )

{}

The cpp header file contains this:

const struct ble_class_handler_t ble_class_evt_handlers[ble_cls_last];
const struct ble_class_handler_t ble_class_rsp_handlers[ble_cls_last];

Now my question is: Is there any possible way to move the init functions to another file, which i refer to in the initializer list of the bglib class? For example, the init list calls a method that initializes the arrays (i know its not possible to call methods from the init list but just to make clear what i want to do)

Reason for this is that i need to initialize a whole lot more arrays like this, and i think its ugly to have an initializer lists that contains hundreds of lines of code.

PS. I know its ugly to use const arrays this way in c++, but i'm including a C library into a C++ project, and i dont have time to completely rewrite the C library.

Thanks in advance!

Upvotes: 1

Views: 295

Answers (1)

2785528
2785528

Reputation: 5576

Have you (would you) consider the following:

bglib::bglib(): 
   ble_class_evt_handlers( 
#include  "ble_class_evt_handlers_init.inc"
                         ),
  ble_class_rsp_handlers( 
#include  "ble_class_system_rsp_handlers_init.inc"
                        )
{}

or perhaps

bglib::bglib() : 
#include "bglib_init.inc"
{}

Upvotes: 2

Related Questions