简体   繁体   中英

Access structure members by using structure name only

I don't know what type on question i am asking.but i only need suggestion or idea to find the way.

I have many structure with large number of members like below

typedef struct _Bank0
{
unsigned char main_control_char;
unsigned short input_port_short;
:
:
}Pack Bank0;

typedef struct _Bank1
{
unsigned char ddr3_control_char;
unsigned char ddl_control_char;
:
:
}Pack Bank1;

I want to write a test function for this register bank, if i give bank number (that is structure name) it should display all the register in that bank.

i just have to avoid repeat programing for testing register, i am trying in following way

select register bank= Bank1(* user  will enter this value) //
//now i want to show all register name in bank 1 for example//
ddr3_control_char
ddl_control_char

after this i want to send data to the selected register. can any one suggest me any idea.i just don't want to copy paste register name again because the length of my code will be to more so to avoid it i want suggestion.

You can implement this functionality for every Bank type individually and then use inheritance, eg

using ValueType = size_t;
using Dump = std::unordered_map<std::string, Value_type>;
struct Bank {
    virtual Dump dump() const = 0;
};
struct Bank0 : public Bank {
    Dump dump() const override {
        return Dump();
    }
};
struct Bank1 : public Bank {
    Dump dump() const override {
        return Dump();
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM