1. object_t
这个object_t实际上就是只有一个name在起作用。
struct object_t { string name; object_t() {} object_t(const char *s) : name(s) {} object_t(const string& s) : name(s) {} void swap(object_t& o) { name.swap(o.name); } void clear() { name.clear(); } void encode(bufferlist &bl) const { using ceph::encode; encode(name, bl); } void decode(bufferlist::const_iterator &bl) { using ceph::decode; decode(name, bl); } }; WRITE_CLASS_ENCODER(object_t)
|
尾部的这个宏,实际上没有什么特别的。这里可以把这个宏展开。
WRITE_CLASS_ENCODER
#define WRITE_CLASS_ENCODER(cl) \ inline void encode(const cl &c, ::ceph::bufferlist &bl, uint64_t features=0) { \ ENCODE_DUMP_PRE(); c.encode(bl); ENCODE_DUMP_POST(cl); } \ inline void decode(cl &c, ::ceph::bufferlist::const_iterator &p) { c.decode(p); }
|
展开之后的代码就是
inline void encode(const cl &c, ::ceph::bufferlist &bl, uint64_t features=0) { ENCODE_DUMP_PRE(); c.encode(bl); ENCODE_DUMP_POST(cl); } inline void decode(cl &c, ::ceph::bufferlist::const_iterator &p) { c.decode(p); }
|
所以这两个宏的作用就是直接调用类的encode和decode函数。
问题
object_t
只提供了name
并没有提供其他任何存储空间,那么一个object
的内容在哪里呢?
重载运算符函数
针对于object_t
,Ceph
重载了很多运算符,看起来是比较繁锁的,就是不知道有没有更加简便的方法。
inline bool operator==(const object_t& l, const object_t& r) { return l.name == r.name; } inline bool operator!=(const object_t& l, const object_t& r) { return l.name != r.name; } inline bool operator>(const object_t& l, const object_t& r) { return l.name > r.name; } inline bool operator<(const object_t& l, const object_t& r) { return l.name < r.name; } inline bool operator>=(const object_t& l, const object_t& r) { return l.name >= r.name; } inline bool operator<=(const object_t& l, const object_t& r) { return l.name <= r.name; } inline ostream& operator<<(ostream& out, const object_t& o) { return out << o.name; }
|
如是只是为了支持hash
函数,那么只需要==
被重载就可以了。
如何自定义hash支持
namespace std { template<> struct hash<object_t> { size_t operator()(const object_t& r) const { return ceph_str_hash_linux(r.name.c_str(), r.name.length()); } }; }
|