class MicroString
Defined at line 69 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
The MicroString class holds a `char` buffer.
The basic usage provides `Set` and `Get` functions that deal with
`absl::string_view`.
It has several layers of optimizations for different sized payloads, as well
as some features for unowned payloads.
It can be in one of several representations, each with their own properties:
- Inline: When enabled, Inline instances store the bytes inlined in the
class. They require no memory allocation.
This representation holds the size in the first (lsb) byte (left
shifted to allow for tags) and the rest of the bytes are the data.
The inline buffer can span beyond the `MicroString` class (see
`MicroStringExtra` below). To support this most operations take
the `inline_capacity` dynamically so that `MicroStringExtra` and
the runtime can pass the real buffer size.
- MicroRep: Cheapest out of line representation. It is two `uint8_t` for
capacity and size, then the char buffer.
- LargeRep: The following representations use LargeRep as the header,
differentiating themselves via the `capacity` field.
* kOwned: A `char` array follows the base. Similar to MicroRep, but with
2^32 byte limit, instead of 2^8.
* kAlias: The base points into an aliased unowned buffer. The base itself
is owned. Used for `SetAlias`.
Copying the MicroString will make its own copy of the data, as
alias lifetime is not guaranteed beyond the original message.
* kUnowned: Similar to kAlias, but the base is also unowned. Both the base
and the payload are guaranteed immutable and immortal. Used
for global strings, like non-empty default values.
Requires no memory allocation. Copying the MicroString will
maintain the unowned status and require no memory allocation.
* kString: The object holds a StringRep. The base points into the
`std::string` instance. Used for `SetString` to allow taking
ownership of `std::string` payloads.
Copying the MicroString will not maintain the kString state, as
it is unnecessary. The copy will use normal reps.
All the functions that write to the inline space take the inline capacity as
a parameter. This allows the subclass to extend the capacity while the base
class handles the logic. It also allows external callers, like reflection, to
pass the dynamically known capacity.
Public Members
static const bool kAllowExtraCapacity
static const size_t kInlineCapacity
static const size_t kMaxMicroRepCapacity
Protected Members
void * rep_
static const uintptr_t kIsLargeRepTag
static const uintptr_t kIsMicroRepTag
static const int kTagShift
static const size_t kMaxInlineCapacity
Public Methods
void Clear ()
Resets the object to the empty string.
Does not necessarily release any memory.
Defined at line 180 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
MicroString MakeDefaultValuePrototype (absl::string_view default_value)
Like the constructor above, but for DynamicMessage where we don't have a
generated UnownedPayload to pass.
The instance created has to be destroyed with
`DestroyDefaultValuePrototype`.
void DestroyDefaultValuePrototype ()
void ClearToDefault (const UnownedPayload & unowned_input, Arena * arena)
To match the API of ArenaStringPtr.
It resets the value to the passed default, trying to keep preexisting
buffer if we are on an arena. This reduces arena bloat when reusing a
message.
void ClearToDefault (const MicroString & other, Arena * arena)
Like above, but takes a prototype `MicroString` that has the unowned rep.
Used for reflection that does not have access to the `UnownedPayload`.
void MicroString ()
Empty string.
Defined at line 131 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void MicroString (Arena * )
Defined at line 133 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void MicroString (Arena * arena, const MicroString & other)
Defined at line 135 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void ~MicroString ()
Trivial destructor.
The payload must be destroyed via `Destroy()` when not in an arena. If
using arenas, no destruction is necessary and calls to `Destroy()` are
invalid.
Defined at line 142 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void MicroString (const UnownedPayload & unowned_input)
Defined at line 155 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void InitDefault ()
Resets value to the default constructor state.
Disregards initial value of rep_ (so this is the *ONLY* safe method to call
after construction or when reinitializing after becoming the active field
in a oneof union).
Defined at line 169 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void Destroy ()
Destroys the payload.
REQUIRES: no arenas. Trying to destroy a string constructed with arenas is
invalid and there is no checking for it.
Defined at line 174 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void Set (const MicroString & other, Arena * arena)
Sets the payload to `other`. Copy behavior depends on the kind of payload.
Defined at line 189 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void Set (const MicroString &other,Arena *arena,size_tinline_capacity)
Defined at line 193 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void Set (absl::string_view data, Arena * arena)
Sets the payload to `data`. Always copies the data.
Defined at line 204 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void Set (absl::string_viewdata,Arena *arena,size_tinline_capacity)
Defined at line 207 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
template <typename... Args>
void Set (const std::string & data, Args... args)
Extra overloads to allow for other implicit conversions.
Eg types that convert to `std::string` (like
`std::reference_wrapper
<std
::string>`).
Defined at line 215 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
template <typename... Args>
void Set (std::string && data, Args... args)
Defined at line 219 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
template <typename... Args>
void Set (const char * data, Args... args)
Defined at line 223 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void InternalSwap (MicroString * other, size_t inline_capacity)
Defined at line 293 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void SetAlias (absl::string_viewdata,Arena *arena,size_tinline_capacity)
Sets the payload to `data`. Might copy the data or alias the input buffer.
void SetUnowned (const UnownedPayload & unowned_input, Arena * arena)
Set the payload to `unowned`. Will not allocate memory, but might free
memory if already set.
template <typename F>
void SetInChunks (size_tsize,Arena *arena,Fsetter,size_tinline_capacity)
Set the string, but the input comes in individual chunks.
This function is designed to be called from the parser.
`size` is the expected total size of the string. It is ok to append fewer
bytes than `size`, but never more. The final size of the string will be
whatever was appended to it.
`size` is used as a hint to reserve space, but the implementation might
decide not to do so for very large values and just grow on append.
The `setter` callback is passed an `append` callback that it can use to
append the chunks one by one.
Eg
str.SetInChunks(10, arena, [](auto append) {
append("12345");
append("67890");
});
The callback approach reduces the dispatch overhead to be done only once
instead of on each append call.
Defined at line 477 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
size_t Capacity ()
The capacity for write access of this string.
It can be 0 if the payload is not writable. For example, aliased buffers.
absl::string_view Get ()
Defined at line 274 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
UnownedPayload MakeUnownedPayload (absl::string_view data)
To be used by constexpr constructors of fields with non-empty default
values. It will alias `data` so it must be an immutable input, like a
literal string.
Defined at line 287 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
size_t SpaceUsedExcludingSelfLong ()
Protected Methods
void ClearSlow ()
void SetImpl (absl::string_viewdata,Arena *arena,size_tinline_capacity)
void DestroySlow ()
MicroRep * AllocateMicroRep (size_t size, Arena * arena)
Allocate the corresponding rep, and sets its size and capacity.
The actual capacity might be larger than the requested one.
The data bytes are uninitialized.
rep_ is updated to point to the new rep without any cleanup of the old
value.
LargeRep * AllocateOwnedRep (size_t size, Arena * arena)
StringRep * AllocateStringRep (Arena * arena)
LargeRepKind large_rep_kind ()
Defined at line 331 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
MicroRep * micro_rep ()
Defined at line 340 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
size_t MicroRepSize (size_t capacity)
Defined at line 348 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
size_t OwnedRepSize (size_t capacity)
Defined at line 351 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
LargeRep * large_rep ()
Defined at line 355 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
StringRep * string_rep ()
Defined at line 362 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
bool is_micro_rep ()
Defined at line 367 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
bool is_large_rep ()
Defined at line 371 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
bool is_inline ()
Defined at line 375 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
size_t inline_size ()
Defined at line 376 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void set_inline_size (size_t size)
Defined at line 380 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
char * inline_head ()
Defined at line 389 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
const char * inline_head ()
Defined at line 399 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
absl::string_view inline_view ()
Defined at line 402 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
template <typename Self>
void MicroString (FromOtherTag,const Self &other,Arena *arena)
Defined at line 410 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
template <typename Self>
void SetFromOtherImpl (Self &self,const Self &other,Arena *arena)
Defined at line 421 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
template <typename Self>
void SetMaybeConstant (Self &self,absl::string_viewdata,Arena *arena)
Defined at line 443 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
void SetString (std::string &&data,Arena *arena,size_tinline_capacity)
Sets the payload to `str`. Might copy the data or take ownership of `str`.
void SetFromOtherSlow (const MicroString &other,Arena *arena,size_tinline_capacity)
Enumerations
enum LargeRepKind
| Name | Value | Comments |
|---|---|---|
| kAlias | 0 |
The buffer is unowned, but the large_rep payload is owned. |
| kUnowned | 1 |
The whole payload is unowned. |
| kString | 2 |
The payload is a StringRep payload. |
| kOwned | 3 |
An owned LargeRep+chars payload. |
Defined at line 320 of file ../../third_party/protobuf/src/src/google/protobuf/micro_string.h
Records
Friends
class MicroStringTestPeer