0

I have a C++ structure that have a few values, for example

struct test_sub
{
int G;
float H;
int I;
};
struct test
{
std::string A;
std::string B;
std::string C;
std::string D;
std::string E;
std::string F;
test_sub new_struct;
};


More elements(int, string or even another struct) could be added or removed in future iterations of my project.

I have to create GUI elements(currently in terminal itself) where I have to show Label as structure element name and its value, for example:

A = A value
B = B value
C = C value
D = D value
E = E value
F = F value
G = 22
H = 5.25
I = 6

Currently I have used hardcoded values to print, as below:

test st_test;
.
.
.
std::cout<<"A = "<<st_test.A<<std::endl;
std::cout<<"B = "<<st_test.B<<std::endl;
std::cout<<"C = "<<st_test.C<<std::endl;
std::cout<<"D = "<<st_test.D<<std::endl;
std::cout<<"E = "<<st_test.E<<std::endl;
std::cout<<"F = "<<st_test.F<<std::endl;
std::cout<<"G = "<<st_test.new_struct.G<<std::endl;
std::cout<<"H = "<<st_test.new_struct.H<<std::endl;
std::cout<<"I = "<<st_test.new_struct.I<<std::endl;

Please provide suggestions to remove this hardcoded situation. Thanks in advance.

Navin Rahim
  • 35
  • 10
  • 2
    Use `std::map` instead of hardocing the values. – Quimby Aug 13 '20 at 11:46
  • @Quimby could you please provide a quick example on how `std::map` would help in removing the hardcoding? – Navin Rahim Aug 13 '20 at 11:49
  • If you use proper GUI framework, e.g. Qt, it usually provides some reflection support to do what you want nicely. – Dan M. Aug 13 '20 at 11:52
  • @DanM. Currently I am using terminal to print the values. In future, I am planning to use nanogui(https://github.com/wjakob/nanogui). It would be helpful if you could point out the feature in Qt, which I can refer, to replicate it. – Navin Rahim Aug 13 '20 at 11:56
  • 1
    @NavinRahim it's not so much of a "feature" but rather just how GUI functions. I.e. see the nanogui example with addVariable s. If you want to reduce the field naming boilerplate, when Qt has the concept of properties which you can then iterate on at runtime, i.e. see https://stackoverflow.com/questions/36518686/dump-all-properties-of-a-qobject-derived-object , more info here: https://doc.qt.io/qt-5/properties.html . It might be overkill for your case (for which maybe ditching fields and just using map is enough), but you decide. – Dan M. Aug 13 '20 at 12:05
  • If you want to use nanogui, then read the section on creating widgets... **Simple mode** might be what you want to use. – Phil1970 Aug 13 '20 at 12:07
  • @Phil1970 Thanks for the answer. Even in simple mode, I would have to create multiple widgets with hardcoded values; and I would have to change the code whenever there is a change in the structure. – Navin Rahim Aug 13 '20 at 12:13
  • @NavinRahim Re how to use map, see https://godbolt.org/z/fjW9cd Though `*(new sub(1))` can be replaced with something more elegant I guess. –  Aug 13 '20 at 12:18
  • Well, it is somewhat expected as the structure often does not have enough information to create the UI and having a separation between the data and the UI is often a good design practice as you can change the UI without changing the structure (and the data layer do not need to know the UI). – Phil1970 Aug 13 '20 at 12:25
  • @anki Thanks for the answer. Will try to implement it. Getting the label name from the structure would have made it better. – Navin Rahim Aug 13 '20 at 13:23

0 Answers0