philippeVerney commented on code in PR #3545:
URL: https://github.com/apache/avro/pull/3545#discussion_r2522513150
##########
lang/c++/include/avro/Specific.hh:
##########
@@ -165,6 +166,44 @@ struct codec_traits<double> {
}
};
+/**
+* codec_traits for Avro optional.
+*/
+template<typename T>
+struct codec_traits<std::optional<T>> {
+ /**
+ * Encodes a given value.
+ */
+ static void encode(Encoder &e, const std::optional<T> &b) {
+ if (b) {
+ e.encodeUnionIndex(1);
+ avro::encode(e, b.value());
+ } else {
+ e.encodeUnionIndex(0);
+ e.encodeNull();
+ }
+ }
+
+ /**
+ * Decodes into a given value.
+ */
+ static void decode(Decoder &d, std::optional<T> &s) {
+ size_t n = d.decodeUnionIndex();
+ if (n >= 2) { throw avro::Exception("Union index too big"); }
+ switch (n) {
+ case 0: {
+ d.decodeNull();
+ s = std::nullopt;
+ } break;
+ case 1: {
+ T t;
+ avro::decode(d, t);
+ s.emplace(t);
Review Comment:
I actually even preferred in place construction. Hoping it is fine with you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]