Project

General

Profile

Feature #11710

Add "generics".

Added by Richard Neswold almost 4 years ago. Updated almost 4 years ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Target version:
-
Start date:
02/12/2016
Due date:
% Done:

0%

Estimated time:
Duration:

Description

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.

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:

Reply::Data<BPM::sample>& obj = dynamic_cast<Reply::Data<BPM::sample>&>(rawObj);

// obj.data 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.

History

#1 Updated by Richard Neswold almost 4 years ago

  • Description updated (diff)

Add more detail to the rough specification.



Also available in: Atom PDF