chrberger / libcluon

libcluon is a small and efficient, single-file and header-only library written in modern C++ to power microservices.
Mozilla Public License 2.0
101 stars 13 forks source link

Feature request/clarification - C++ generated enum of message types #26

Closed Expan75 closed 7 months ago

Expan75 commented 8 months ago

Hi!

Thank you for open sourcing! Looking over unpacking, I was wondering if there's a way of outputting an enum alongside the generated C++ header for the message?

cluon::OD4Session od4(111,
    [](cluon::data::Envelope &&envelope) noexcept {
      if (envelope.dataType() == 2001) {
        MyTestMessage1 receivedMsg = cluon::extractMessage<MyTestMessage1>(std::move(envelope));

        PrimeChecker pc;
        std::cout << receivedMsg.myValue() << " is" 
          << (pc.isPrime(receivedMsg.myValue()) ? " " : " not ")
          << "a prime." << std::endl;
      }
    });

^Use case would be to swap out 2001 with an enum that's generated. Ideally this enum would also be present as generated proto enum (which I think is represented in the protobuf spec? If not, forgive my ignorance!). When I follow the docs the message id gets added as comments in the .proto file.

Cheers!

// Erik

chrberger commented 8 months ago

Hi Erik,

Instead of writing 2001, you can also use MyTestMessage1::ID() as static class member to check for the message ID that is auto-generated.An enum over all messages inside an .odvd file bears the risk to break an application’s code unintentionally if the structure of an .odvd changes substantially. Using the auto-generated ID from a concrete message of interest is closest to the scope of a message.

Best regards,

       Christian Berger