Reflection of well known types in C++

142 views
Skip to first unread message

Chris Buffett

unread,
Jul 27, 2018, 2:08:13 PM7/27/18
to Protocol Buffers
I'm wondering if protobuf supports a way to determine if a type is a well-known type via reflection in C++? I'm working on a marshalling layer for a custom encoding format and I need to unbox well-known types into their primitive type (e.g., Double to double). I've so far been unable to find any information on if it's possible to definitively tell if a type is well known or not, other than .

For example, if I have the following protobuf,

message A
{
    message B
    {
        double val = 1;
        google.protobuf.Doublevalue prev_val = 2;
    }

    repeated B values;
}

I need to convert into something with the following format
class B
{
    double val;
    double prev_val;
}

class A
{
    list<B> values;
}

The issue I see is that based on the protobuf format, the output would be ambiguous. I could also generate the following because I'm unable to tell whether I should unbox the type or generate a wrapping class.

class B
{
    double val;
    class C
    {
        double prev_val;
    }
}

Josh Humphries

unread,
Jul 27, 2018, 3:44:26 PM7/27/18
to chris....@gmail.com, Protocol Buffers
They are well-known by their fully-qualified name, e.g. "google.protobuf.DoubleValue". And you can get the fully-qualified names via C++ reflection.

So you're marshaling logic woudl need to special-case the relevant message names and subsequently can make assumptions about their structure (since they are "well known"). As far as "auto-unboxing", keep in mind that one significant use case for these well-known types is that they can be nil, whereas there is obviously no analog for the unboxed primitive type (zero is the closest, but then you cannot distinguish between whether the original was nil or explicit zero).

----
Josh Humphries
jh...@bluegosling.com



--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages