]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/cmdparse.h
update source to 12.2.11
[ceph.git] / ceph / src / common / cmdparse.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 #ifndef CEPH_COMMON_CMDPARSE_H
4 #define CEPH_COMMON_CMDPARSE_H
5
6 #include <vector>
7 #include <stdexcept>
8 #include <ostream>
9 #include <boost/variant.hpp>
10 #include "include/assert.h" // boost clobbers this
11 #include "common/Formatter.h"
12 #include "common/BackTrace.h"
13
14 class CephContext;
15
16 /* this is handy; can't believe it's not standard */
17 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
18
19 typedef boost::variant<std::string,
20 bool,
21 int64_t,
22 double,
23 std::vector<std::string>,
24 std::vector<int64_t>,
25 std::vector<double>> cmd_vartype;
26 typedef std::map<std::string, cmd_vartype> cmdmap_t;
27
28 std::string cmddesc_get_prefix(const std::string &cmddesc);
29 void dump_cmd_to_json(ceph::Formatter *f, const std::string& cmd);
30 void dump_cmd_and_help_to_json(ceph::Formatter *f,
31 const std::string& secname,
32 const std::string& cmd,
33 const std::string& helptext);
34 void dump_cmddesc_to_json(ceph::Formatter *jf,
35 const std::string& secname,
36 const std::string& cmdsig,
37 const std::string& helptext,
38 const std::string& module,
39 const std::string& perm,
40 const std::string& avail,
41 uint64_t flags);
42 bool cmdmap_from_json(std::vector<std::string> cmd, cmdmap_t *mapp,
43 std::stringstream &ss);
44 void cmdmap_dump(const cmdmap_t &cmdmap, ceph::Formatter *f);
45 void handle_bad_get(CephContext *cct, const std::string& k, const char *name);
46
47 std::string cmd_vartype_stringify(const cmd_vartype& v);
48
49 struct bad_cmd_get : public std::exception {
50 std::string desc;
51 bad_cmd_get(const std::string& f, const cmdmap_t& cmdmap) {
52 desc = "bad or missing field '" + f + "'";
53 }
54 const char *what() const throw() override {
55 return desc.c_str();
56 }
57 };
58
59 template <typename T>
60 bool cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, const std::string& k,
61 T& val)
62 {
63 if (cmdmap.count(k)) {
64 try {
65 val = boost::get<T>(cmdmap.find(k)->second);
66 return true;
67 } catch (boost::bad_get&) {
68 handle_bad_get(cct, k, typeid(T).name());
69 }
70 }
71 return false;
72 }
73
74 template <typename T>
75 bool cmd_getval_throws(CephContext *cct, const cmdmap_t& cmdmap,
76 const std::string& k, T& val)
77 {
78 if (cmdmap.count(k)) {
79 try {
80 val = boost::get<T>(cmdmap.find(k)->second);
81 return true;
82 } catch (boost::bad_get&) {
83 throw bad_cmd_get(k, cmdmap);
84 }
85 }
86 return false;
87 }
88
89 // with default
90
91 template <typename T>
92 void cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, const std::string& k,
93 T& val, const T& defval)
94 {
95 if (!cmd_getval(cct, cmdmap, k, val))
96 val = defval;
97 }
98
99 template <typename T>
100 bool cmd_getval_throws(
101 CephContext *cct, const cmdmap_t& cmdmap, const std::string& k,
102 T& val, const T& defval)
103 {
104 if (cmdmap.count(k)) {
105 try {
106 val = boost::get<T>(cmdmap.find(k)->second);
107 return true;
108 } catch (boost::bad_get&) {
109 throw bad_cmd_get(k, cmdmap);
110 }
111 } else {
112 val = defval;
113 return true;
114 }
115 }
116
117 template <typename T>
118 void
119 cmd_putval(CephContext *cct, cmdmap_t& cmdmap, const std::string& k, const T& val)
120 {
121 cmdmap[k] = val;
122 }
123
124 extern int parse_osd_id(const char *s, std::ostream *pss);
125 extern long parse_pos_long(const char *s, std::ostream *pss = NULL);
126
127 #endif