From 5e3f83722239314179df337d059188ab223ae25d Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 9 May 2017 16:08:49 +0800 Subject: [PATCH] byteorder: refactor ceph_le{16,32,64} using template to improve the readablity and easier for debugging. Signed-off-by: Kefu Chai --- src/include/byteorder.h | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/include/byteorder.h b/src/include/byteorder.h index bbc8a2ef4443..867486016686 100644 --- a/src/include/byteorder.h +++ b/src/include/byteorder.h @@ -64,23 +64,24 @@ inline T mswab(T val) { } #endif -#define MAKE_LE_CLASS(bits) \ - struct ceph_le##bits { \ - __u##bits v; \ - ceph_le##bits &operator=(__u##bits nv) { \ - v = mswab(nv); \ - return *this; \ - } \ - operator __u##bits() const { return mswab(v); } \ - } __attribute__ ((packed)); \ - static inline bool operator==(ceph_le##bits a, ceph_le##bits b) { \ - return a.v == b.v; \ +template +struct ceph_le { + T v; + ceph_le& operator=(T nv) { + v = mswab(nv); + return *this; } - -MAKE_LE_CLASS(64) -MAKE_LE_CLASS(32) -MAKE_LE_CLASS(16) -#undef MAKE_LE_CLASS + operator T() const { return mswab(v); } +} __attribute__ ((packed)); + +template +inline bool operator==(ceph_le a, ceph_le b) { + return a.v == b.v; +} + +using ceph_le64 = ceph_le<__u64>; +using ceph_le32 = ceph_le<__u32>; +using ceph_le16 = ceph_le<__u16>; inline __u64 init_le64(__u64 x) { return mswab<__u64>(x); -- 2.47.3