]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
include/encoding: add encoder helpers for sized ints
authorPatrick Donnelly <pdonnell@ibm.com>
Tue, 24 Jun 2025 02:37:16 +0000 (22:37 -0400)
committerPatrick Donnelly <pdonnell@ibm.com>
Mon, 26 Jan 2026 15:27:21 +0000 (10:27 -0500)
When the raw type may not match the required encoded size, this helper makes
intent clear and avoids a common verbose pattern:

    intX_t t = val;
    encode(t, bl);

and

    intX_t t;
    decode(t, p);
    val = t;

Signed-off-by: Patrick Donnelly <pdonnell@ibm.com>
src/include/encoding.h

index d073c1e7c627a50558274c6184cb849dec727e73..ac5a0f41658895d8185b909f761102c0ed4e7c5c 100644 (file)
@@ -23,6 +23,7 @@
 #include <string>
 #include <string_view>
 #include <tuple>
+#include <type_traits>
 #include <optional>
 #include <unordered_map>
 #include <unordered_set>
@@ -208,6 +209,20 @@ WRITE_FLTTYPE_ENCODER(double, uint64_t, le64)
     ENCODE_DUMP_PRE(); c.encode(bl, features); ENCODE_DUMP_POST(cl); } \
   inline void decode(cl &c, ::ceph::bufferlist::const_iterator &p) { c.decode(p); }
 
+template<typename Int>
+inline void encode_assign(const auto& t, ::ceph::bufferlist& bl, uint64_t features=0)
+{
+  Int i = static_cast<Int>(t);
+  ::encode(i, bl);
+}
+
+template<typename Int>
+inline void decode_assign(auto& t, ::ceph::bufferlist::const_iterator& p)
+{
+  Int i;
+  ::decode(i, p);
+  t = static_cast<std::remove_reference_t<decltype(t)>>(i);
+}
 
 // string
 inline void encode(std::string_view s, bufferlist& bl, uint64_t features=0)