A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://protobuf.dev/reference/cpp/string-view below:

String View APIs | Protocol Buffers Documentation

String View APIs

Covers various string_view migrations

C++ string field APIs that use std::string significantly constrain the internal protobuf implementation and its evolution. For example, mutable_string_field() returns std::string* that forces us to use std::string to store the field. This complicates its interaction on arenas and we have to maintain arena donation states to track whether string payload allocation is from arena or heap.

Long-term, we would like to migrate all of our runtime and generated APIs to accept string_view as inputs and return them from accessors. This document describes the state of the migration as of our 30.x release.

String Field Accessors

As part of edition 2023, the string_type feature was released with a VIEW option to allow for the incremental migration to generated string_view APIs. Using this feature will affect the C++ Generated Code of string and bytes fields.

Interaction with ctype

In edition 2023, you can still specify ctype at the field level, while you can specify string_type at either the file or field level. It is not allowed to specify both on the same field. If string_type is set at the file-level, ctype specified on fields will take precedent.

Except for the VIEW option, all possible values of string_type have a corresponding ctype value that is spelled the same and gives the same behavior. For example, both enums have a CORD value.

In edition 2024 and beyond, it will no longer be possible to specify ctype.

Generated Singular Fields

For either of these field definitions in edition 2023:

bytes foo = 1 [features.(pb.cpp).string_type=VIEW];
string foo = 1 [features.(pb.cpp).string_type=VIEW];

The compiler will generate the following accessor methods:

Generated Repeated Fields

For either of these field definitions:

repeated string foo = 1 [features.(pb.cpp).string_type=VIEW];
repeated bytes foo = 1 [features.(pb.cpp).string_type=VIEW];

The compiler will generate the following accessor methods:

Generated Oneof Fields

For any of these oneof field definitions:

oneof example_name {
    string foo = 1 [features.(pb.cpp).string_type=VIEW];
    ...
}
oneof example_name {
    bytes foo = 1 [features.(pb.cpp).string_type=VIEW];
    ...
}

The compiler will generate the following accessor methods:

Enumeration Name Helper

Beginning in edition 2024, a new feature enum_name_uses_string_view is introduced and defaults to true. Unless disabled, for an enum like:

enum Foo {
  VALUE_A = 0;
  VALUE_B = 5;
  VALUE_C = 1234;
}

The protocol buffer compiler, in addition to the Foo enum, will generate the following new function in addition to the standard generated code:

This can be reverted back to the old behavior by adding a feature override like:

enum Foo {
  option features.(pb.cpp).enum_name_uses_string_view = false;

  VALUE_A = 0;
  VALUE_B = 5;
  VALUE_C = 1234;
}

In which case the name helper will switch back to const string& Foo_Name(int value).


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4