]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
byteorder: refactor ceph_le{16,32,64} using template 15012/head
authorKefu Chai <kchai@redhat.com>
Tue, 9 May 2017 08:08:49 +0000 (16:08 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 10 May 2017 15:12:30 +0000 (23:12 +0800)
to improve the readablity and easier for debugging.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/byteorder.h

index bbc8a2ef444393603b290525bfb945cd134a0b6a..8674860166864936da9aa7db9bfc8b293a1630a0 100644 (file)
@@ -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<typename T>
+struct ceph_le {
+  T v;
+  ceph_le<T>& 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<typename T>
+inline bool operator==(ceph_le<T> a, ceph_le<T> 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);