eclipse-cyclonedds / cyclonedds

Eclipse Cyclone DDS project
https://projects.eclipse.org/projects/iot.cyclonedds
Other
799 stars 349 forks source link

Custom Serializer #1969

Closed arthurafarias closed 3 months ago

arthurafarias commented 3 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!

arthurafarias commented 3 months ago

... the same way for istream