Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-109126

Switching on oneof field value

    XMLWordPrintable

Details

    • Task
    • Resolution: Fixed
    • P3: Somewhat important
    • 6.6.0
    • None
    • Protocol Buffers
    • None
    • 13
    • ae2f22dce (dev), 618d6c8aa (dev)
    • Foundation Sprint 78, Foundations Sprint 79

    Description

      Generate separate enum for each oneof field in generated classes in addition to QtProtobufFieldEnum.
      e.g.

      .proto:

      message OneofMessage
      {
          int32 testFieldInt = 1;
          oneof testOneof
          {
              int32 testOneofFieldInt = 42;
              ComplexMessage testOneofComplexField = 3;
              ComplexMessage testOneofSecondComplexField = 4;
          }
          oneof secondOneof
          {
              int32 secondFieldInt = 43;
              ComplexMessage secondComplexField = 5;
              ComplexMessage secondSecondComplexField = 6;
          }
      }

      Generated C++:

      // Existing protobuf enum
      enum QtProtobufFieldEnum {
      TestFieldIntProtoFieldNumber = 1,
      TestOneofFieldIntProtoFieldNumber = 42,
      TestOneofComplexFieldProtoFieldNumber = 3,
      TestOneofSecondComplexFieldProtoFieldNumber = 4,
      SecondFieldIntProtoFieldNumber = 43,
      SecondComplexFieldProtoFieldNumber = 5,
      SecondSecondComplexFieldProtoFieldNumber = 6,
      };
      Q_ENUM(QtProtobufFieldEnum)
        
      // testOneof enum
      enum testOneofField {
      InvalidFieldNumber = QtProtobuf::InvalidFieldNumber,
      testOneofFieldInt = 42,
      testOneofComplexField = 3,
      testOneofSecondComplexField = 4,
      };
      Q_ENUM(testOneofField)
      // secondOneof enum
      enum secondOneofField {
      InvalidFieldNumber = QtProtobuf::InvalidFieldNumber,
      secondFieldInt = 43,
      secondComplexField = 5,
      secondSecondComplexField = 6,
      };
      Q_ENUM(secondOneofField)
      

       

      Additionally, methods returning field number of Oneof should return enum instead:

      testOneofField testOneofField() const;
      secondOneofField secondOneofField() const;

      That allows client of generated C++ class to easily switch on oneof field value:

      OneofMessage test = ...;
       switch (test.testOneofCase() /* getter for a oneof field named 'testOneof' */) {
         case OneofMessage::testOneofCase::None:
           // no value
           break;
         case OneofMessage::testOneofCase::testOneofComplexField:
           // the contained 'ComplexField' field is initialized
           ...
         // etc...
       }

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              kokujawa Konrad Kujawa (Inactive)
              manordheim Mårten Nordheim
              Vladimir Minenko Vladimir Minenko
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes