Replies: 1 comment
-
Ice C++ does not currently provide a way to "print" a struct or class instance. Ice C# in Ice 3.8 provides this feature indirectly for structs - Slice structs are mapped to C# record (class or struct), and a C# record has a synthesized For C++, using ice_tuple() or the Ice serialization framework does not work for printing. The Ice encoding is a compact encoding that doesn't include the field names - while here, for readability, you want the field names in your output. A solution is to write your own "print" function for each generated C++ struct, for example: // C++
// in the same namespace as MyStruct
std::ostream& operator<<(std::ostream&, const MyStruct&);
// implementation shows field names + field values, possibly type name too Since you have hundreds of these structs, the best would be to update the Slice compiler (slice2cpp) to generate these "print" functions for you. It would be more maintainable. I would also suggest to make this generation opt-in to avoid code bloat, for example: // Slice
["cpp:printable"] // instructs the Slice compiler to generate operator<< to print this struct
struct Foo { ... } You could look at the C# record ToString for a possible representation: |
Beta Was this translation helpful? Give feedback.
-
In our usage, there is a case: we need to convert hundreds of structs/classes(most with nesting, some has optional member) defined with ICE into log files that can be viewed by end users.
we known that the Protobuf lib, we can use "SerializeToString()" to achieve this, are there any methods that might help me out in ice?
We have already tried using :
member function:ice_tuple()
micro: "#define PRINT_VARIABLE_NAME_AND_VALUE(variable) std::cout << #variable << " = " << variable << std::endl; "
but it still cannot be well adapted to the optional and nested situations.
Beta Was this translation helpful? Give feedback.
All reactions