eclipse-cyclonedds / cyclonedds-cxx

Other
96 stars 75 forks source link

Custom Serializer #482

Closed arthurafarias closed 7 months ago

arthurafarias commented 7 months ago

Hello guys

I was evaluating the posibility of IDLCXX generator generates a weak definition of << operator overloading or a call to an external implementation of it.

I was trying define a custom character formatter so I could translate, for example to a JSON or YML format of the generated object but I see no way of doing it.

By example...

In the HelloWorldSubscriber Example we have

std::cout << "=== [Subscriber] Message received: " << msg << std::endl;

https://vscode.dev/github/eclipse-cyclonedds/cyclonedds-cxx/blob/master/examples/helloworld/subscriber.cpp#L75

the operator << is defined at

namespace HelloWorldData
{
std::ostream& operator<<(std::ostream& os, Msg const& rhs)
{
  (void) rhs;
  os << "[";
  os << "userID: " << rhs.userID();
  os << ", message: " << rhs.message();
  os << "]";
  return os;
}

https://vscode.dev/github/eclipse-cyclonedds/cyclonedds-cxx/blob/master/build/examples/helloworld/HelloWorldData.cpp#L13

With this, I see no way of defining a custom serializer for the Msg object as string.

If I define another, it will be ambiguous.

isn't there a way of defining, by example a virtual function toString(), on the generated object that receives the ostream object as parameter and can be override so we can define a custom serialization?

Best regards!