Skip to content

Commit 81b39eb

Browse files
Merge pull request #84 from Fefer-Ivan/use-clone-fix-slicing
use clone to avoid slicing
2 parents a01ec0c + 1970d4a commit 81b39eb

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/mfast/coder/common/dictionary_builder.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ dictionary_builder::clone_instruction(const template_instruction *src_inst) {
104104
<< template_name_info(src_inst->name()));
105105
}
106106

107-
auto dest = new (alloc_) template_instruction(*src_inst);
107+
auto dest = src_inst->clone(alloc_);
108108

109109
const char *ns = src_inst->ns();
110110
if (is_empty_string(ns)) {
@@ -127,21 +127,21 @@ void dictionary_builder::visit(const template_instruction *src_inst,
127127
}
128128
}
129129

130-
void dictionary_builder::visit(const templateref_instruction * /*src_inst*/,
130+
void dictionary_builder::visit(const templateref_instruction * src_inst,
131131
void *dest_inst) {
132132
templateref_instruction *&dest =
133133
*static_cast<templateref_instruction **>(dest_inst);
134134

135135
// this is dynamic templateRef, it can only be binded at decoding time
136-
dest = new (alloc_) templateref_instruction;
136+
dest = src_inst->clone(alloc_);
137137
}
138138

139139
void dictionary_builder::visit(const group_field_instruction *src_inst,
140140
void *dest_inst) {
141141
group_field_instruction *&dest =
142142
*static_cast<group_field_instruction **>(dest_inst);
143143

144-
dest = new (alloc_) group_field_instruction(*src_inst);
144+
dest = src_inst->clone(alloc_);
145145

146146
this->build_group(src_inst, src_inst, dest);
147147
}
@@ -150,7 +150,7 @@ void dictionary_builder::visit(const sequence_field_instruction *src_inst,
150150
void *dest_inst) {
151151
sequence_field_instruction *&dest =
152152
*static_cast<sequence_field_instruction **>(dest_inst);
153-
dest = new (alloc_) sequence_field_instruction(*src_inst);
153+
dest = src_inst->clone(alloc_);
154154

155155
this->build_group(src_inst, src_inst, dest);
156156

@@ -248,7 +248,7 @@ void dictionary_builder::visit(const int32_field_instruction *src_inst,
248248
void *dest_inst) {
249249
int32_field_instruction *&dest =
250250
*static_cast<int32_field_instruction **>(dest_inst);
251-
dest = new (alloc_) int32_field_instruction(*src_inst);
251+
dest = src_inst->clone(alloc_);
252252
dest->prev_value_ =
253253
get_dictionary_storage(dest->name(), dest->ns(), dest->op_context_,
254254
field_type_int32, &dest->prev_storage_, dest);
@@ -258,7 +258,7 @@ void dictionary_builder::visit(const uint32_field_instruction *src_inst,
258258
void *dest_inst) {
259259
uint32_field_instruction *&dest =
260260
*static_cast<uint32_field_instruction **>(dest_inst);
261-
dest = new (alloc_) uint32_field_instruction(*src_inst);
261+
dest = src_inst->clone(alloc_);
262262
dest->prev_value_ =
263263
get_dictionary_storage(dest->name(), dest->ns(), dest->op_context_,
264264
field_type_uint32, &dest->prev_storage_, dest);
@@ -268,7 +268,7 @@ void dictionary_builder::visit(const int64_field_instruction *src_inst,
268268
void *dest_inst) {
269269
int64_field_instruction *&dest =
270270
*static_cast<int64_field_instruction **>(dest_inst);
271-
dest = new (alloc_) int64_field_instruction(*src_inst);
271+
dest = src_inst->clone(alloc_);
272272
dest->prev_value_ =
273273
get_dictionary_storage(dest->name(), dest->ns(), dest->op_context_,
274274
field_type_int64, &dest->prev_storage_, dest);
@@ -278,7 +278,7 @@ void dictionary_builder::visit(const uint64_field_instruction *src_inst,
278278
void *dest_inst) {
279279
uint64_field_instruction *&dest =
280280
*static_cast<uint64_field_instruction **>(dest_inst);
281-
dest = new (alloc_) uint64_field_instruction(*src_inst);
281+
dest = src_inst->clone(alloc_);
282282
dest->prev_value_ =
283283
get_dictionary_storage(dest->name(), dest->ns(), dest->op_context_,
284284
field_type_uint64, &dest->prev_storage_, dest);
@@ -288,7 +288,7 @@ void dictionary_builder::visit(const ascii_field_instruction *src_inst,
288288
void *dest_inst) {
289289
ascii_field_instruction *&dest =
290290
*static_cast<ascii_field_instruction **>(dest_inst);
291-
dest = new (alloc_) ascii_field_instruction(*src_inst);
291+
dest = src_inst->clone(alloc_);
292292
dest->prev_value_ = get_dictionary_storage(
293293
dest->name(), dest->ns(), dest->op_context_, field_type_ascii_string,
294294
&dest->prev_storage_, dest);
@@ -299,7 +299,7 @@ void dictionary_builder::visit(const unicode_field_instruction *src_inst,
299299
void *dest_inst) {
300300
unicode_field_instruction *&dest =
301301
*static_cast<unicode_field_instruction **>(dest_inst);
302-
dest = new (alloc_) unicode_field_instruction(*src_inst);
302+
dest = src_inst->clone(alloc_);
303303
dest->prev_value_ = get_dictionary_storage(
304304
dest->name(), dest->ns(), dest->op_context_, field_type_unicode_string,
305305
&dest->prev_storage_, dest);
@@ -311,7 +311,7 @@ void dictionary_builder::visit(const decimal_field_instruction *src_inst,
311311
void *dest_inst) {
312312
decimal_field_instruction *&dest =
313313
*static_cast<decimal_field_instruction **>(dest_inst);
314-
dest = new (alloc_) decimal_field_instruction(*src_inst);
314+
dest = src_inst->clone(alloc_);
315315

316316
if (src_inst->field_type() == field_type_decimal) {
317317
dest->prev_value_ =
@@ -341,7 +341,7 @@ void dictionary_builder::visit(const byte_vector_field_instruction *src_inst,
341341
void *dest_inst) {
342342
byte_vector_field_instruction *&dest =
343343
*static_cast<byte_vector_field_instruction **>(dest_inst);
344-
dest = new (alloc_) byte_vector_field_instruction(*src_inst);
344+
dest = src_inst->clone(alloc_);
345345
dest->prev_value_ = get_dictionary_storage(
346346
dest->name(), dest->ns(), dest->op_context(), field_type_byte_vector,
347347
&dest->prev_storage_, dest);
@@ -352,35 +352,35 @@ void dictionary_builder::visit(const int32_vector_field_instruction *src_inst,
352352
void *dest_inst) {
353353
int32_vector_field_instruction *&dest =
354354
*static_cast<int32_vector_field_instruction **>(dest_inst);
355-
dest = new (alloc_) int32_vector_field_instruction(*src_inst);
355+
dest = src_inst->clone(alloc_);
356356
}
357357

358358
void dictionary_builder::visit(const uint32_vector_field_instruction *src_inst,
359359
void *dest_inst) {
360360
uint32_vector_field_instruction *&dest =
361361
*static_cast<uint32_vector_field_instruction **>(dest_inst);
362-
dest = new (alloc_) uint32_vector_field_instruction(*src_inst);
362+
dest = src_inst->clone(alloc_);
363363
}
364364

365365
void dictionary_builder::visit(const int64_vector_field_instruction *src_inst,
366366
void *dest_inst) {
367367
int64_vector_field_instruction *&dest =
368368
*static_cast<int64_vector_field_instruction **>(dest_inst);
369-
dest = new (alloc_) int64_vector_field_instruction(*src_inst);
369+
dest = src_inst->clone(alloc_);
370370
}
371371

372372
void dictionary_builder::visit(const uint64_vector_field_instruction *src_inst,
373373
void *dest_inst) {
374374
uint64_vector_field_instruction *&dest =
375375
*static_cast<uint64_vector_field_instruction **>(dest_inst);
376-
dest = new (alloc_) uint64_vector_field_instruction(*src_inst);
376+
dest = src_inst->clone(alloc_);
377377
}
378378

379379
void dictionary_builder::visit(const enum_field_instruction *src_inst,
380380
void *dest_inst) {
381381
enum_field_instruction *&dest =
382382
*static_cast<enum_field_instruction **>(dest_inst);
383-
dest = new (alloc_) enum_field_instruction(*src_inst);
383+
dest = src_inst->clone(alloc_);
384384
dest->prev_value_ =
385385
get_dictionary_storage(dest->name(), dest->ns(), dest->op_context_,
386386
field_type_uint64, &dest->prev_storage_, dest);

src/mfast/coder/fast_decoder_v2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace mfast {
6868
/// the dictionary values essential to decoding subsequent messages. On the
6969
/// other hand, the decoder
7070
/// would
71-
/// always duplicate the dictionay values when NumTokens>0 and thus modifying
71+
/// always duplicate the dictionary values when NumTokens>0 and thus modifying
7272
/// the returned messages
7373
/// is allowed.
7474

0 commit comments

Comments
 (0)