]> git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_basic_types.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rgw / rgw_basic_types.h
1 #ifndef CEPH_RGW_BASIC_TYPES_H
2 #define CEPH_RGW_BASIC_TYPES_H
3
4 #include <string>
5
6 #include "include/types.h"
7
8 struct rgw_user {
9 std::string tenant;
10 std::string id;
11
12 rgw_user() {}
13 // cppcheck-suppress noExplicitConstructor
14 rgw_user(const std::string& s) {
15 from_str(s);
16 }
17 rgw_user(const std::string& tenant, const std::string& id)
18 : tenant(tenant),
19 id(id) {
20 }
21
22 void encode(bufferlist& bl) const {
23 ENCODE_START(1, 1, bl);
24 ::encode(tenant, bl);
25 ::encode(id, bl);
26 ENCODE_FINISH(bl);
27 }
28 void decode(bufferlist::iterator& bl) {
29 DECODE_START(1, bl);
30 ::decode(tenant, bl);
31 ::decode(id, bl);
32 DECODE_FINISH(bl);
33 }
34
35 void to_str(std::string& str) const {
36 if (!tenant.empty()) {
37 str = tenant + '$' + id;
38 } else {
39 str = id;
40 }
41 }
42
43 void clear() {
44 tenant.clear();
45 id.clear();
46 }
47
48 bool empty() const {
49 return id.empty();
50 }
51
52 string to_str() const {
53 string s;
54 to_str(s);
55 return s;
56 }
57
58 void from_str(const std::string& str) {
59 size_t pos = str.find('$');
60 if (pos != std::string::npos) {
61 tenant = str.substr(0, pos);
62 id = str.substr(pos + 1);
63 } else {
64 tenant.clear();
65 id = str;
66 }
67 }
68
69 rgw_user& operator=(const string& str) {
70 from_str(str);
71 return *this;
72 }
73
74 int compare(const rgw_user& u) const {
75 int r = tenant.compare(u.tenant);
76 if (r != 0)
77 return r;
78
79 return id.compare(u.id);
80 }
81 int compare(const string& str) const {
82 rgw_user u(str);
83 return compare(u);
84 }
85
86 bool operator!=(const rgw_user& rhs) const {
87 return (compare(rhs) != 0);
88 }
89 bool operator==(const rgw_user& rhs) const {
90 return (compare(rhs) == 0);
91 }
92 bool operator<(const rgw_user& rhs) const {
93 if (tenant < rhs.tenant) {
94 return true;
95 } else if (tenant > rhs.tenant) {
96 return false;
97 }
98 return (id < rhs.id);
99 }
100 };
101 WRITE_CLASS_ENCODER(rgw_user)
102
103
104 class JSONObj;
105
106 void decode_json_obj(rgw_user& val, JSONObj *obj);
107 void encode_json(const char *name, const rgw_user& val, Formatter *f);
108
109 inline ostream& operator<<(ostream& out, const rgw_user &u) {
110 string s;
111 u.to_str(s);
112 return out << s;
113 }
114
115
116 #endif