Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for simple structs? #6

Open
sharkdp opened this issue Jun 19, 2019 · 8 comments
Open

Add support for simple structs? #6

sharkdp opened this issue Jun 19, 2019 · 8 comments

Comments

@sharkdp
Copy link
Owner

sharkdp commented Jun 19, 2019

It would be cool to somehow add support for simple structs/classes like

struct CustomDatatype {
  int field1;
  std::string field2;
};

//

CustomDatatype c{42, "hello"};
dbg(c);

We would somehow have to iterate over all members... without modifying to original struct definition. Not sure if this is possible in C++ (without reflection).

@hmenke
Copy link
Contributor

hmenke commented Jun 20, 2019

You can use magic_get (https://www.youtube.com/watch?v=abdeAew3gmQ). Requires C++14.

@sharkdp
Copy link
Owner Author

sharkdp commented Jun 20, 2019

magic_get looks fantastic, thank you for the reference.

If there is a small subset of the code that we could include in dbg.h (assuming there are no licensing problems, see similar discussion in #4 and #5), I think that would be a great addition. It looks like there is no way to get the actual name of the fields, so the best we could do would probably be an output like:

dbg(c); // CustomDatatype{42, "hello"}

(which would already be great).

@AlexBAV
Copy link

AlexBAV commented Jun 20, 2019

There's also cista, which uses another approach for simple type reflection (see to_tuple.h for example).

@sharkdp
Copy link
Owner Author

sharkdp commented Jun 20, 2019

There's also cista, which uses another approach for simple type reflection (see to_tuple.h for example).

Thank you for the reference! Unfortunately, it seems to rely on C++17 functionality, but I would really like dbg(…) to be C++11 compatible.

@alexeyr
Copy link

alexeyr commented Apr 5, 2021

Would it be good enough if magic_get-derived functionality was under #ifdef __cplusplus, so people using C++14 or later get it automatically and those limited to C++11 define showValue as usual?

For C++11, I think it's possible to define a macro so SHOW(CustomDatatype, field1, field2) expands to

inline bool pretty_print(std::ostream& stream, const CustomDatatype& value) {
  stream << "CustomDatatype" "{" "field1" ": " << value.field1 << ", " "field2" ": " << value.field2 << "}";
  return true;
}

based on this technique but it's tricky.

@sharkdp
Copy link
Owner Author

sharkdp commented Apr 13, 2021

Agreed. If the concerns above (license, code size) can be clarified, I'd be willing to accept this addition.

@winwinashwin
Copy link
Contributor

@sharkdp Here is a proposal to incorporate this feature. This is a working example that I came up with based on the above discussions and some snippets from here.

Sample driver code

struct MetaData {
    std::string name;
    int64_t id;
};

struct Entity3D {
    int32_t x, y, z;
    MetaData meta;
};

DBG_REGISTER_STRUCT(MetaData, name, id)
DBG_REGISTER_STRUCT(Entity3D, x, y, z, meta)

int32_t main() {
    Entity3D e = {3, 2, 1, {"dummy_name", 98765}};
    dbg(e);  // [main.cc:34 (main)] e = {'x': 3, 'y': 2, 'z': 1, 'meta': {'name': "dummy_name", 'id': 98765}} (Entity3D)
}

If this is satisfactory I'll be glad to open a PR!

@sharkdp
Copy link
Owner Author

sharkdp commented Feb 28, 2023

Thank you. If we add support for this, then I would definitely look for a solution which does not require any additional macro calls like DBG_REGISTER_STRUCT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants