BewareMyPower commented on issue #6785:
URL: https://github.com/apache/pulsar/issues/6785#issuecomment-619524556


   The C wrapper of `MessageId` has a `MessageId` field:
   
   ```c++
   struct _pulsar_message_id {
       pulsar::MessageId messageId;
   };
   ```
   
   And you can convert `std::string` to c-style string by:
   
   ```c++
   char* string_to_raw_c_string(const std::string& s) {
     char* c_str = static_cast<char*>(malloc(s.size() + 1));
     memcpy(c_str, s.c_str(), s.size() + 1);  // C++11 ensures that std::string 
has a '\0' terminator
     return c_str;
   }
   ```
   
   So you can implement the C functions like following code:
   
   ```c++
   void pulsar_message_set_topic_name(pulsar_message_id_t *messageId, const 
char* topicName) {
       messageId->messageId->setTopicName(topicName);
   }
   
   char* pulsar_message_get_topic_name(pulsar_message_id_t *messageId) {
       return string_to_raw_c_string(messageId->messageId->getTopicName());
   }
   ```
   
   Remind that the return value of `char*` must be freed. And use 
`PULSAR_PUBLIC` to expose these C functions. I'm not familiar with cgo (we use 
SWIG to wrap C++ client), it seems that it's convenient to convert between 
`char*` and go string in cgo.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to