Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2023 Google LLC. All rights reserved. |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 3 | // |
Hong Shin | c482a8a | 2023-11-09 09:30:34 -0800 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
| 7 | |
Hong Shin | 096b139 | 2024-07-03 10:04:09 -0700 | [diff] [blame] | 8 | #include "google/protobuf/compiler/hpb/gen_repeated_fields.h" |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 9 | |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "google/protobuf/descriptor.pb.h" |
| 14 | #include "absl/strings/string_view.h" |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 15 | #include "google/protobuf/compiler/hpb/context.h" |
Hong Shin | 096b139 | 2024-07-03 10:04:09 -0700 | [diff] [blame] | 16 | #include "google/protobuf/compiler/hpb/gen_accessors.h" |
| 17 | #include "google/protobuf/compiler/hpb/gen_enums.h" |
| 18 | #include "google/protobuf/compiler/hpb/gen_extensions.h" |
| 19 | #include "google/protobuf/compiler/hpb/gen_utils.h" |
| 20 | #include "google/protobuf/compiler/hpb/names.h" |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 21 | #include "google/protobuf/descriptor.h" |
Joshua Haberman | fd6d872 | 2024-09-08 21:52:55 -0700 | [diff] [blame] | 22 | #include "upb_generator/c/names.h" |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 23 | #include "upb_generator/common.h" |
| 24 | #include "upb_generator/file_layout.h" |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 25 | |
Hong Shin | 7a2b37f | 2024-07-09 15:42:34 -0700 | [diff] [blame] | 26 | namespace google::protobuf::hpb_generator { |
| 27 | |
Hong Shin | 096b139 | 2024-07-03 10:04:09 -0700 | [diff] [blame] | 28 | namespace protobuf = ::proto2; |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 29 | |
| 30 | // Adds using accessors to reuse base Access class members from a Proxy/CProxy. |
| 31 | void WriteRepeatedFieldUsingAccessors(const protobuf::FieldDescriptor* field, |
| 32 | absl::string_view class_name, |
| 33 | absl::string_view resolved_field_name, |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 34 | Context& ctx, bool read_only) { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 35 | if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 36 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 37 | R"cc( |
| 38 | using $0Access::$1; |
| 39 | using $0Access::$1_size; |
| 40 | )cc", |
| 41 | class_name, resolved_field_name); |
| 42 | if (!read_only) { |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 43 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 44 | R"cc( |
| 45 | using $0Access::add_$1; |
Hong Shin | 36075ea | 2024-10-09 12:44:27 -0700 | [diff] [blame] | 46 | using $0Access::add_alias_$1; |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 47 | using $0Access::mutable_$1; |
| 48 | )cc", |
| 49 | class_name, resolved_field_name); |
| 50 | } |
| 51 | } else { |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 52 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 53 | R"cc( |
| 54 | using $0Access::$1; |
| 55 | using $0Access::$1_size; |
| 56 | )cc", |
| 57 | class_name, resolved_field_name); |
| 58 | if (!read_only) { |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 59 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 60 | R"cc( |
| 61 | using $0Access::add_$1; |
| 62 | using $0Access::mutable_$1; |
| 63 | using $0Access::resize_$1; |
| 64 | using $0Access::set_$1; |
| 65 | )cc", |
| 66 | class_name, resolved_field_name); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void WriteRepeatedFieldsInMessageHeader(const protobuf::Descriptor* desc, |
| 72 | const protobuf::FieldDescriptor* field, |
| 73 | absl::string_view resolved_field_name, |
| 74 | absl::string_view resolved_upbc_name, |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 75 | Context& ctx) { |
| 76 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 77 | R"cc( |
| 78 | inline size_t $1_size() const { |
| 79 | size_t len; |
| 80 | $0_$2(msg_, &len); |
| 81 | return len; |
| 82 | } |
| 83 | )cc", |
| 84 | MessageName(desc), resolved_field_name, resolved_upbc_name); |
| 85 | |
| 86 | if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 87 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 88 | R"cc( |
| 89 | $1 $2(size_t index) const; |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 90 | const ::hpb::RepeatedField<const $4>::CProxy $2() const; |
| 91 | ::hpb::Ptr<::hpb::RepeatedField<$4>> mutable_$2(); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 92 | absl::StatusOr<$0> add_$2(); |
Hong Shin | 36075ea | 2024-10-09 12:44:27 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Re-points submsg of repeated field to given target. |
| 95 | * |
| 96 | * REQUIRES: both messages must be in the same arena. |
| 97 | */ |
| 98 | bool add_alias_$2($0 target); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 99 | $0 mutable_$2(size_t index) const; |
| 100 | )cc", |
| 101 | MessagePtrConstType(field, /* const */ false), // $0 |
| 102 | MessagePtrConstType(field, /* const */ true), // $1 |
| 103 | resolved_field_name, // $2 |
| 104 | resolved_upbc_name, // $3 |
| 105 | MessageBaseType(field, /* maybe_const */ false) // $4 |
| 106 | ); |
| 107 | } else if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_STRING) { |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 108 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 109 | R"cc( |
| 110 | $0 $1(size_t index) const; |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 111 | const ::hpb::RepeatedField<$0>::CProxy $1() const; |
| 112 | ::hpb::Ptr<::hpb::RepeatedField<$0>> mutable_$1(); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 113 | bool add_$1($0 val); |
| 114 | void set_$1(size_t index, $0 val); |
| 115 | bool resize_$1(size_t len); |
| 116 | )cc", |
| 117 | CppConstType(field), resolved_field_name); |
| 118 | } else { |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 119 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 120 | R"cc( |
| 121 | $0 $1(size_t index) const; |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 122 | const ::hpb::RepeatedField<$0>::CProxy $1() const; |
| 123 | ::hpb::Ptr<::hpb::RepeatedField<$0>> mutable_$1(); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 124 | bool add_$1($0 val); |
| 125 | void set_$1(size_t index, $0 val); |
| 126 | bool resize_$1(size_t len); |
| 127 | )cc", |
| 128 | CppConstType(field), resolved_field_name); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void WriteRepeatedMessageAccessor(const protobuf::Descriptor* message, |
| 133 | const protobuf::FieldDescriptor* field, |
| 134 | const absl::string_view resolved_field_name, |
| 135 | const absl::string_view class_name, |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 136 | Context& ctx) { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 137 | const char arena_expression[] = "arena_"; |
| 138 | absl::string_view upbc_name = field->name(); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 139 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 140 | R"cc( |
| 141 | $1 $0::$2(size_t index) const { |
| 142 | size_t len; |
| 143 | auto* ptr = $3_$5(msg_, &len); |
| 144 | assert(index < len); |
Hong Shin | caa8f9d | 2024-08-22 12:00:45 -0700 | [diff] [blame] | 145 | return ::hpb::interop::upb::MakeCHandle<$4>( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 146 | (upb_Message*)*(ptr + index), arena_); |
| 147 | } |
| 148 | )cc", |
| 149 | class_name, MessagePtrConstType(field, /* is_const */ true), |
| 150 | resolved_field_name, MessageName(message), |
| 151 | MessageBaseType(field, /* maybe_const */ false), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 152 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 153 | R"cc( |
| 154 | absl::StatusOr<$1> $0::add_$2() { |
| 155 | auto new_msg = $3_add_$6(msg_, $5); |
| 156 | if (!new_msg) { |
Hong Shin | 9a24fc9 | 2024-08-20 15:20:52 -0700 | [diff] [blame] | 157 | return ::hpb::MessageAllocationError(); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 158 | } |
Hong Shin | 36075ea | 2024-10-09 12:44:27 -0700 | [diff] [blame] | 159 | return hpb::interop::upb::MakeHandle<$4>((upb_Message *)new_msg, $5); |
| 160 | } |
| 161 | |
| 162 | bool $0::add_alias_$2($1 target) { |
| 163 | ABSL_CHECK_EQ(arena_, hpb::interop::upb::GetArena(target)); |
| 164 | size_t size = 0; |
| 165 | $3_$2(msg_, &size); |
| 166 | auto elements = $3_resize_$2(msg_, size + 1, arena_); |
| 167 | if (!elements) { |
| 168 | return false; |
| 169 | } |
| 170 | elements[size] = ($9 *)hpb::interop::upb::GetMessage(target); |
| 171 | return true; |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 172 | } |
| 173 | )cc", |
| 174 | class_name, MessagePtrConstType(field, /* const */ false), |
| 175 | resolved_field_name, MessageName(message), |
| 176 | MessageBaseType(field, /* maybe_const */ false), arena_expression, |
Hong Shin | 36075ea | 2024-10-09 12:44:27 -0700 | [diff] [blame] | 177 | upbc_name, ClassName(message), field->index(), |
| 178 | upb::generator::CApiMessageType(field->message_type()->full_name())); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 179 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 180 | R"cc( |
| 181 | $1 $0::mutable_$2(size_t index) const { |
| 182 | size_t len; |
| 183 | auto* ptr = $3_$6(msg_, &len); |
| 184 | assert(index < len); |
Hong Shin | d4a8995 | 2024-08-29 07:56:08 -0700 | [diff] [blame] | 185 | return hpb::interop::upb::MakeHandle<$4>((upb_Message*)*(ptr + index), $5); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 186 | } |
| 187 | )cc", |
| 188 | class_name, MessagePtrConstType(field, /* is_const */ false), |
| 189 | resolved_field_name, MessageName(message), |
| 190 | MessageBaseType(field, /* maybe_const */ false), arena_expression, |
| 191 | upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 192 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 193 | R"cc( |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 194 | const ::hpb::RepeatedField<const $1>::CProxy $0::$2() const { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 195 | size_t size; |
| 196 | const upb_Array* arr = _$3_$4_$5(msg_, &size); |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 197 | return ::hpb::RepeatedField<const $1>::CProxy(arr, arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 198 | }; |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 199 | ::hpb::Ptr<::hpb::RepeatedField<$1>> $0::mutable_$2() { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 200 | size_t size; |
| 201 | upb_Array* arr = _$3_$4_$6(msg_, &size, arena_); |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 202 | return ::hpb::RepeatedField<$1>::Proxy(arr, arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 203 | } |
| 204 | )cc", |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 205 | class_name, // $0 |
| 206 | MessageBaseType(field, /* maybe_const */ false), // $1 |
| 207 | resolved_field_name, // $2 |
| 208 | MessageName(message), // $3 |
| 209 | upbc_name, // $4 |
| 210 | upb::generator::kRepeatedFieldArrayGetterPostfix, // $5 |
| 211 | upb::generator::kRepeatedFieldMutableArrayGetterPostfix // $6 |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 212 | ); |
| 213 | } |
| 214 | |
| 215 | void WriteRepeatedStringAccessor(const protobuf::Descriptor* message, |
| 216 | const protobuf::FieldDescriptor* field, |
| 217 | const absl::string_view resolved_field_name, |
| 218 | const absl::string_view class_name, |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 219 | Context& ctx) { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 220 | absl::string_view upbc_name = field->name(); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 221 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 222 | R"cc( |
| 223 | $1 $0::$2(size_t index) const { |
| 224 | size_t len; |
| 225 | auto* ptr = $3_mutable_$4(msg_, &len); |
| 226 | assert(index < len); |
Hong Shin | 9d835e6 | 2024-09-12 12:34:47 -0700 | [diff] [blame] | 227 | return hpb::interop::upb::FromUpbStringView(*(ptr + index)); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 228 | } |
| 229 | )cc", |
| 230 | class_name, CppConstType(field), resolved_field_name, |
| 231 | MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 232 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 233 | R"cc( |
| 234 | bool $0::resize_$1(size_t len) { |
| 235 | return $2_resize_$3(msg_, len, arena_); |
| 236 | } |
| 237 | )cc", |
| 238 | class_name, resolved_field_name, MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 239 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 240 | R"cc( |
| 241 | bool $0::add_$2($1 val) { |
Hong Shin | 8681742 | 2024-09-19 11:06:34 -0700 | [diff] [blame] | 242 | return $3_add_$4(msg_, |
| 243 | hpb::interop::upb::CopyToUpbStringView(val, arena_), |
| 244 | arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 245 | } |
| 246 | )cc", |
| 247 | class_name, CppConstType(field), resolved_field_name, |
| 248 | MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 249 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 250 | R"cc( |
| 251 | void $0::set_$2(size_t index, $1 val) { |
| 252 | size_t len; |
| 253 | auto* ptr = $3_mutable_$4(msg_, &len); |
| 254 | assert(index < len); |
Hong Shin | 8681742 | 2024-09-19 11:06:34 -0700 | [diff] [blame] | 255 | *(ptr + index) = hpb::interop::upb::CopyToUpbStringView(val, arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 256 | } |
| 257 | )cc", |
| 258 | class_name, CppConstType(field), resolved_field_name, |
| 259 | MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 260 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 261 | R"cc( |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 262 | const ::hpb::RepeatedField<$1>::CProxy $0::$2() const { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 263 | size_t size; |
| 264 | const upb_Array* arr = _$3_$4_$5(msg_, &size); |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 265 | return ::hpb::RepeatedField<$1>::CProxy(arr, arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 266 | }; |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 267 | ::hpb::Ptr<::hpb::RepeatedField<$1>> $0::mutable_$2() { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 268 | size_t size; |
| 269 | upb_Array* arr = _$3_$4_$6(msg_, &size, arena_); |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 270 | return ::hpb::RepeatedField<$1>::Proxy(arr, arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 271 | } |
| 272 | )cc", |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 273 | class_name, // $0 |
| 274 | CppConstType(field), // $1 |
| 275 | resolved_field_name, // $2 |
| 276 | MessageName(message), // $3 |
| 277 | upbc_name, // $4 |
| 278 | upb::generator::kRepeatedFieldArrayGetterPostfix, // $5 |
| 279 | upb::generator::kRepeatedFieldMutableArrayGetterPostfix // $6 |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 280 | ); |
| 281 | } |
| 282 | |
| 283 | void WriteRepeatedScalarAccessor(const protobuf::Descriptor* message, |
| 284 | const protobuf::FieldDescriptor* field, |
| 285 | const absl::string_view resolved_field_name, |
| 286 | const absl::string_view class_name, |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 287 | Context& ctx) { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 288 | absl::string_view upbc_name = field->name(); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 289 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 290 | R"cc( |
| 291 | $1 $0::$2(size_t index) const { |
| 292 | size_t len; |
| 293 | auto* ptr = $3_mutable_$4(msg_, &len); |
| 294 | assert(index < len); |
| 295 | return *(ptr + index); |
| 296 | } |
| 297 | )cc", |
| 298 | class_name, CppConstType(field), resolved_field_name, |
| 299 | MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 300 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 301 | R"cc( |
| 302 | bool $0::resize_$1(size_t len) { |
| 303 | return $2_resize_$3(msg_, len, arena_); |
| 304 | } |
| 305 | )cc", |
| 306 | class_name, resolved_field_name, MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 307 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 308 | R"cc( |
| 309 | bool $0::add_$2($1 val) { return $3_add_$4(msg_, val, arena_); } |
| 310 | )cc", |
| 311 | class_name, CppConstType(field), resolved_field_name, |
| 312 | MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 313 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 314 | R"cc( |
| 315 | void $0::set_$2(size_t index, $1 val) { |
| 316 | size_t len; |
| 317 | auto* ptr = $3_mutable_$4(msg_, &len); |
| 318 | assert(index < len); |
| 319 | *(ptr + index) = val; |
| 320 | } |
| 321 | )cc", |
| 322 | class_name, CppConstType(field), resolved_field_name, |
| 323 | MessageName(message), upbc_name); |
Hong Shin | 082cf00 | 2024-10-15 12:29:42 -0700 | [diff] [blame] | 324 | ctx.EmitLegacy( |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 325 | R"cc( |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 326 | const ::hpb::RepeatedField<$1>::CProxy $0::$2() const { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 327 | size_t size; |
| 328 | const upb_Array* arr = _$3_$4_$5(msg_, &size); |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 329 | return ::hpb::RepeatedField<$1>::CProxy(arr, arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 330 | }; |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 331 | ::hpb::Ptr<::hpb::RepeatedField<$1>> $0::mutable_$2() { |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 332 | size_t size; |
| 333 | upb_Array* arr = _$3_$4_$6(msg_, &size, arena_); |
Hong Shin | 090edb7 | 2024-08-06 09:55:47 -0700 | [diff] [blame] | 334 | return ::hpb::RepeatedField<$1>::Proxy(arr, arena_); |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 335 | } |
| 336 | )cc", |
Adam Cozzette | 12c7bb0 | 2023-09-28 12:54:11 -0700 | [diff] [blame] | 337 | class_name, // $0 |
| 338 | CppConstType(field), // $1 |
| 339 | resolved_field_name, // $2 |
| 340 | MessageName(message), // $3 |
| 341 | upbc_name, // $4 |
| 342 | upb::generator::kRepeatedFieldArrayGetterPostfix, // $5 |
| 343 | upb::generator::kRepeatedFieldMutableArrayGetterPostfix // $6 |
Adam Cozzette | 501ecec | 2023-09-26 14:36:20 -0700 | [diff] [blame] | 344 | ); |
| 345 | } |
| 346 | |
Hong Shin | 7a2b37f | 2024-07-09 15:42:34 -0700 | [diff] [blame] | 347 | } // namespace protobuf |
| 348 | } // namespace google::hpb_generator |