Skip to content

Commit dc5d0b5

Browse files
committed
Remove size-dependent helpers and use local templated lambda
1 parent eb5e9ea commit dc5d0b5

File tree

1 file changed

+42
-77
lines changed
  • components/core/src/clp/ffi/ir_stream

1 file changed

+42
-77
lines changed

components/core/src/clp/ffi/ir_stream/utils.hpp

+42-77
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,6 @@ template <typename encoded_variable_t>
8282
template <IntegerType T>
8383
[[nodiscard]] auto get_ones_complement(T int_val) -> T;
8484

85-
/**
86-
* Encodes and serializes a schema tree node ID using the given encoding type.
87-
* @tparam is_auto_generated_node Whether the node is from the auto-generated or the user-generated
88-
* schema tree.
89-
* @tparam length_indicator_tag
90-
* @tparam encoded_node_id_t
91-
* @param node_id
92-
* @param output_buf
93-
*/
94-
template <
95-
bool is_auto_generated_node,
96-
int8_t length_indicator_tag,
97-
SignedIntegerType encoded_node_id_t>
98-
auto size_dependent_encode_and_serialize_schema_tree_node_id(
99-
SchemaTree::Node::id_t node_id,
100-
std::vector<int8_t>& output_buf
101-
) -> void;
102-
10385
/**
10486
* Encodes and serializes a schema tree node ID.
10587
* @tparam is_auto_generated_node Whether the schema tree node ID is from the auto-generated or the
@@ -122,21 +104,6 @@ template <
122104
std::vector<int8_t>& output_buf
123105
) -> bool;
124106

125-
/**
126-
* Deserializes and decodes a schema tree node ID with the given encoding type.
127-
* @tparam encoded_node_id_t The integer type used to encode the node ID.
128-
* @param reader
129-
* @return A result containing a pair or an error code indicating the failure:
130-
* - The pair:
131-
* - Whether the node ID is for an auto-generated node.
132-
* - The decoded node ID.
133-
* - The possible error codes:
134-
* - std::errc::result_out_of_range if the IR stream is truncated.
135-
*/
136-
template <SignedIntegerType encoded_node_id_t>
137-
[[nodiscard]] auto size_dependent_deserialize_and_decode_schema_tree_node_id(ReaderInterface& reader
138-
) -> OUTCOME_V2_NAMESPACE::std_result<std::pair<bool, SchemaTree::Node::id_t>>;
139-
140107
/**
141108
* Deserializes and decodes a schema tree node ID.
142109
* @tparam one_byte_length_indicator_tag Tag for one-byte node ID encoding.
@@ -232,22 +199,6 @@ auto get_ones_complement(T int_val) -> T {
232199
return static_cast<T>(~int_val);
233200
}
234201

235-
template <
236-
bool is_auto_generated_node,
237-
int8_t length_indicator_tag,
238-
SignedIntegerType encoded_node_id_t>
239-
auto size_dependent_encode_and_serialize_schema_tree_node_id(
240-
SchemaTree::Node::id_t node_id,
241-
std::vector<int8_t>& output_buf
242-
) -> void {
243-
output_buf.push_back(length_indicator_tag);
244-
if constexpr (is_auto_generated_node) {
245-
serialize_int(get_ones_complement(static_cast<encoded_node_id_t>(node_id)), output_buf);
246-
} else {
247-
serialize_int(static_cast<encoded_node_id_t>(node_id), output_buf);
248-
}
249-
}
250-
251202
template <
252203
bool is_auto_generated_node,
253204
int8_t one_byte_length_indicator_tag,
@@ -257,40 +208,37 @@ auto encode_and_serialize_schema_tree_node_id(
257208
SchemaTree::Node::id_t node_id,
258209
std::vector<int8_t>& output_buf
259210
) -> bool {
211+
auto size_dependent_encode_and_serialize_schema_tree_node_id
212+
= [&output_buf]<SignedIntegerType encoded_node_id_t>(
213+
int8_t length_indicator_tag,
214+
SchemaTree::Node::id_t node_id
215+
) {
216+
output_buf.push_back(length_indicator_tag);
217+
if constexpr (is_auto_generated_node) {
218+
serialize_int(
219+
get_ones_complement(static_cast<encoded_node_id_t>(node_id)),
220+
output_buf
221+
);
222+
} else {
223+
serialize_int(static_cast<encoded_node_id_t>(node_id), output_buf);
224+
}
225+
};
226+
260227
if (node_id <= static_cast<SchemaTree::Node::id_t>(INT8_MAX)) {
261-
size_dependent_encode_and_serialize_schema_tree_node_id<
262-
is_auto_generated_node,
263-
one_byte_length_indicator_tag,
264-
int8_t>(node_id, output_buf);
228+
size_dependent_encode_and_serialize_schema_tree_node_id.template operator(
229+
)<int8_t>(one_byte_length_indicator_tag, node_id);
265230
} else if (node_id <= static_cast<SchemaTree::Node::id_t>(INT16_MAX)) {
266-
size_dependent_encode_and_serialize_schema_tree_node_id<
267-
is_auto_generated_node,
268-
two_byte_length_indicator_tag,
269-
int16_t>(node_id, output_buf);
231+
size_dependent_encode_and_serialize_schema_tree_node_id.template operator(
232+
)<int16_t>(two_byte_length_indicator_tag, node_id);
270233
} else if (node_id <= static_cast<SchemaTree::Node::id_t>(INT32_MAX)) {
271-
size_dependent_encode_and_serialize_schema_tree_node_id<
272-
is_auto_generated_node,
273-
four_byte_length_indicator_tag,
274-
int32_t>(node_id, output_buf);
234+
size_dependent_encode_and_serialize_schema_tree_node_id.template operator(
235+
)<int32_t>(four_byte_length_indicator_tag, node_id);
275236
} else {
276237
return false;
277238
}
278239
return true;
279240
}
280241

281-
template <SignedIntegerType encoded_node_id_t>
282-
auto size_dependent_deserialize_and_decode_schema_tree_node_id(ReaderInterface& reader
283-
) -> OUTCOME_V2_NAMESPACE::std_result<std::pair<bool, SchemaTree::Node::id_t>> {
284-
encoded_node_id_t encoded_node_id{};
285-
if (false == deserialize_int(reader, encoded_node_id)) {
286-
return std::errc::result_out_of_range;
287-
}
288-
if (0 > encoded_node_id) {
289-
return {true, static_cast<SchemaTree::Node::id_t>(get_ones_complement(encoded_node_id))};
290-
}
291-
return {false, static_cast<SchemaTree::Node::id_t>(encoded_node_id)};
292-
}
293-
294242
template <
295243
int8_t one_byte_length_indicator_tag,
296244
int8_t two_byte_length_indicator_tag,
@@ -299,14 +247,31 @@ auto deserialize_and_decode_schema_tree_node_id(
299247
encoded_tag_t length_indicator_tag,
300248
ReaderInterface& reader
301249
) -> OUTCOME_V2_NAMESPACE::std_result<std::pair<bool, SchemaTree::Node::id_t>> {
250+
auto size_dependent_deserialize_and_decode_schema_tree_node_id
251+
= [&reader]<SignedIntegerType encoded_node_id_t>(
252+
) -> OUTCOME_V2_NAMESPACE::std_result<std::pair<bool, SchemaTree::Node::id_t>> {
253+
encoded_node_id_t encoded_node_id{};
254+
if (false == deserialize_int(reader, encoded_node_id)) {
255+
return std::errc::result_out_of_range;
256+
}
257+
if (0 > encoded_node_id) {
258+
return {true, static_cast<SchemaTree::Node::id_t>(get_ones_complement(encoded_node_id))
259+
};
260+
}
261+
return {false, static_cast<SchemaTree::Node::id_t>(encoded_node_id)};
262+
};
263+
302264
if (one_byte_length_indicator_tag == length_indicator_tag) {
303-
return size_dependent_deserialize_and_decode_schema_tree_node_id<int8_t>(reader);
265+
return size_dependent_deserialize_and_decode_schema_tree_node_id.template operator(
266+
)<int8_t>();
304267
}
305268
if (two_byte_length_indicator_tag == length_indicator_tag) {
306-
return size_dependent_deserialize_and_decode_schema_tree_node_id<int16_t>(reader);
269+
return size_dependent_deserialize_and_decode_schema_tree_node_id.template operator(
270+
)<int16_t>();
307271
}
308272
if (four_byte_length_indicator_tag == length_indicator_tag) {
309-
return size_dependent_deserialize_and_decode_schema_tree_node_id<int32_t>(reader);
273+
return size_dependent_deserialize_and_decode_schema_tree_node_id.template operator(
274+
)<int32_t>();
310275
}
311276
return std::errc::protocol_error;
312277
}

0 commit comments

Comments
 (0)