Feature #11710

Updated by Richard Neswold over 4 years ago

Charlie King and I had a discussion about adding the concept of "generics" to the protocol compiler. It is similar to templates in C++ or generics in Java and Swift. In the universe of the protocol compiler, it's were you define most of the protocol, but leave part of it open so a user can, later, drop in a type to complete it.

Add a @public@ keyword? By default, all data types can be created in the target language, but only the marshalers/unmarshalers of the @requests@ and @replies@ are public. We could add a @public@ keyword to tag @structs@ and @enums@ so their serialization functions are available, too.

h3. Example

Here's an example that tries to explain it (although the final implementation may take a different approach.) A BPM system has structured data and describes it in a @BPM.proto@ file.

public struct sample {
int cycle;
int rate;
double data[];

Note this "protocol" doesn't define any request or reply messages. Its data types are intended to be used in other protocols. Next we define an acquisition protocol that can use it:

request data {

reply data<T> {
int64 timestamp;
int16 status;
T data;

Here the reply message uses generics to allow the user of the protocol to decide how the field should be unmarshaled. In C++ code, you could retrieve the message like this:

<pre><code class="cpp">
Reply::Data<BPM::sample>& obj = dynamic_cast<Reply::Data<BPM::sample>&>(rawObj);

// is a data type specific to BPMs.

This feature requires the client to know what data he's asking for, which isn't a bad constraint.