0

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.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
Ganesh Khose
  • 11
  • 1
  • 8
  • 2
    Please select only one of C or C++. They are different languages and the answer is likely to be different depending on what language is really being used. – kaylum Feb 15 '20 at 06:58
  • There is a way to do this with preprocessor magic. But that is only worth the effort if those long lists of members change regularily. If not then you are better off with repeating the code and can compromise by doing the code repeating by copy-pasting in your IDE editor. So, let me know whether you predict to have to repeatedly change the structures. If I am convinced I can dig up my prepro magic wand... – Yunnosch Feb 15 '20 at 07:13
  • 1
    @kaylum i want to do with c++ – Ganesh Khose Feb 15 '20 at 07:18
  • @Yunnosch yes some registers form some bank will change regularly,but not all register – Ganesh Khose Feb 15 '20 at 07:20
  • @GaneshKhose Do you insist on using printf with C++? – Yunnosch Feb 15 '20 at 07:20
  • Not convinced. How can registers in a bank change regularily. Also, I offered the magic while being convinced that this is about C. I need extra convincing for C++-prepro chimeras... – Yunnosch Feb 15 '20 at 07:22
  • @Yunnosch no i dont, i can use if its working fine for me – Ganesh Khose Feb 15 '20 at 07:24
  • @Yunnosch if i get suggestion i can use printf.but i want to show all members of structure on console – Ganesh Khose Feb 15 '20 at 07:26
  • @Yunnosch i have some functions depending on that the value of register change. – Ganesh Khose Feb 15 '20 at 07:28
  • @Yunnosch i have some register which will control luminance that time if user change its value the register will update every time – Ganesh Khose Feb 15 '20 at 07:32
  • You seem to be describing that the values inside the registers change. Please convince me that the structure of the register changes regularily. Also this confusion makes the question unclear in my opinion... – Yunnosch Feb 15 '20 at 07:34
  • @Yunnosch no it will not change it is fixed.all members in structure are fixed. – Ganesh Khose Feb 15 '20 at 07:36
  • Then just copy-paste-edit in an editor to minimise typing effort and error probability. Saving code length is not worth taking any special means if it makes the code less readable. – Yunnosch Feb 15 '20 at 07:37
  • But in case you are interested, the prepro-magic I was referring to is explained here: https://stackoverflow.com/a/43377322/7733418 (my answer). – Yunnosch Feb 15 '20 at 07:40
  • @Yunnosch ok.thanks for help – Ganesh Khose Feb 15 '20 at 07:41
  • @Yunnosch i did not understand code you linked in above comment. – Ganesh Khose Feb 15 '20 at 09:11
  • It can be applied to only once make a list defining the structure of one of your banks and then use that structure twice; once to define the struct datatype and once to output it. I.e. you would not have to write the list of members twice but can define the type AND print it from one place. The answer shows how to do that for a similar case of using the same info twice, for different purposes. That concept can be applied to your problem, too. But using copy-paste-edit in an editor for you is the better way anyway - if you don't convince me otherwise. – Yunnosch Feb 15 '20 at 09:31
  • @Yunnosch i did not understand what to write in place of X_##mode(a) – Ganesh Khose Feb 15 '20 at 09:55
  • That one is literal, the whole code is useable as is. No placeholder pseudocode. – Yunnosch Feb 15 '20 at 09:56
  • If you need a tailoring to your question you will have to more precisely clarify what you want to do. Show an [mre] of what you need to do. Show the version where you DO repeat the needed code. Show how the structure (not the values) of the register bank changes, how the version with copied code needs to be changed. Make short example structs and show how you need to repeatedly change. Note that I still believe that you do NOT need to do that. I need the details to really understand your goal. Otherwise my answer will be judged off the mark and downvoted. Please add examples and details. – Yunnosch Feb 15 '20 at 10:00
  • Note, if you elaborate your question you might be misunderstood to have changed the question, which is not appreciated when a question already has an answer. To avoid that, make sure to explain how the existing answer misunderstood your existing question and that the edit only gives details, instead of asking a new question. To be safe consider making a NEW question. – Yunnosch Feb 15 '20 at 10:02
  • @Yunnosch ok i will post new question – Ganesh Khose Feb 15 '20 at 10:08
  • Consider linking to this question and explaining the difference or the non-difference in relation to you respecting the answer here, even if misunderstood. You are in a tricky situation and misunderstanding-based downvotes lurk on your path. Good luck. If I notice your new question I will try to protect you. – Yunnosch Feb 15 '20 at 10:10
  • This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_Bank0`, `_Bank1`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Feb 15 '20 at 15:42

1 Answers1

1

You can implement this functionality for every Bank type individually and then use inheritance, e.g.

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();
    }
};
Tarek Dakhran
  • 2,021
  • 11
  • 21
  • it show error that expected nested-name-specifier before 'ValueType' and "Dump". also what should i enter in size_t in using ValueType = size_t; – Ganesh Khose Feb 15 '20 at 09:25
  • C++ does not support reflection out of the box. You can enumerate them yourself in some data structure or class method and return as an `unordered_map` – Tarek Dakhran Feb 15 '20 at 11:00