From: Patrick Donnelly Date: Tue, 24 Jun 2025 02:37:16 +0000 (-0400) Subject: include/encoding: add encoder helpers for sized ints X-Git-Tag: testing/wip-pdonnell-testing-20260126.152838~48 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=af953bcb67eb9ffb74cec4764a9f66234dd837ad;p=ceph-ci.git include/encoding: add encoder helpers for sized ints 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 --- diff --git a/src/include/encoding.h b/src/include/encoding.h index d073c1e7c62..ac5a0f41658 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -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 +inline void encode_assign(const auto& t, ::ceph::bufferlist& bl, uint64_t features=0) +{ + Int i = static_cast(t); + ::encode(i, bl); +} + +template +inline void decode_assign(auto& t, ::ceph::bufferlist::const_iterator& p) +{ + Int i; + ::decode(i, p); + t = static_cast>(i); +} // string inline void encode(std::string_view s, bufferlist& bl, uint64_t features=0)