Strings

Just like the repeated fields, String fields are of fixed length in Embedded Proto. You specify the length by means of a template parameter when creating the message object or by means of a custom option.

Using the custom option works exactly the same as for repeated fields detailed here. In the following paragraphs we take a look at a simple example using templates:

message text
{
  string txt = 1;
}

The code generated with hold a field object with the type FieldString which has a template parameter to specify the maximum length of the string. In the FieldString object an array is specified of characters. This is done to be able to work without dynamic allocated memory. This in contrast to std::string which does use dynamic allocation.

template<uint32_t txt_LENGTH>
class text final: public ::EmbeddedProto::MessageInterface
{
  private:
    ::EmbeddedProto::FieldString<txt_LENGTH> txt_;
}
Null termination
Be sure to leave space for a null terminator in your strings!

The functions to access and manipulate the FieldString contains functions to obtain the character array and set the string.

FieldString<MAX_LENGTH>& operator=(const char* const &&rhs)
Error set(const DATA_TYPE* data, const uint32_t length)
const DATA_TYPE& operator[](uint32_t index) const

A working example could look like this:

// Define the message object specifying the maximum number 
// of chars in the array. Ten in this case.
text<10> msg;

// Set the string by means of assignment.
msg.mutable_txt() = "Foo Bar";

// Change the capitals to lower case by means of indeces.
msg.mutable_txt()[0] = 'f';
msg.mutable_txt()[4] = 'b';

// Acces the char array.
const char* text = msg.get_txt().get_const();

// Obtain the current number of chars in the string
const uint32_t N_chars = msg.get_txt().get_length();

// It is also possible to obtain the maximum number of chars 
// this string can hold. This would always return the value 
// you have set as the template parameter.
const uint32_t max_N_chars = msg.get_txt().get_max_length();

// You can clear the whole string with the clear function
msg.mutable_txt().clear();