]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/cmdparse.h
import ceph quincy 17.2.6
[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 <optional>
9 #include <ostream>
10 #include <boost/variant.hpp>
11 #include "include/ceph_assert.h" // boost clobbers this
12 #include "include/common_fwd.h"
13 #include "common/Formatter.h"
14 #include "common/BackTrace.h"
15
16 typedef boost::variant<std::string,
17 bool,
18 int64_t,
19 double,
20 std::vector<std::string>,
21 std::vector<int64_t>,
22 std::vector<double>> cmd_vartype;
23 typedef std::map<std::string, cmd_vartype, std::less<>> cmdmap_t;
24
25 namespace TOPNSPC::common {
26 std::string cmddesc_get_prefix(const std::string_view &cmddesc);
27 std::string cmddesc_get_prenautilus_compat(const std::string &cmddesc);
28 void dump_cmd_to_json(ceph::Formatter *f, uint64_t features,
29 const std::string& cmd);
30 void dump_cmd_and_help_to_json(ceph::Formatter *f,
31 uint64_t features,
32 const std::string& secname,
33 const std::string& cmd,
34 const std::string& helptext);
35 void dump_cmddesc_to_json(ceph::Formatter *jf,
36 uint64_t features,
37 const std::string& secname,
38 const std::string& cmdsig,
39 const std::string& helptext,
40 const std::string& module,
41 const std::string& perm,
42 uint64_t flags);
43 bool cmdmap_from_json(const std::vector<std::string>& cmd, cmdmap_t *mapp,
44 std::ostream& ss);
45 void cmdmap_dump(const cmdmap_t &cmdmap, ceph::Formatter *f);
46 void handle_bad_get(CephContext *cct, const std::string& k, const char *name);
47
48 std::string cmd_vartype_stringify(const cmd_vartype& v);
49
50 struct bad_cmd_get : public std::exception {
51 std::string desc;
52 bad_cmd_get(std::string_view f, const cmdmap_t& cmdmap) {
53 desc += "bad or missing field '";
54 desc += f;
55 desc += "'";
56 }
57 const char *what() const throw() override {
58 return desc.c_str();
59 }
60 };
61
62 bool cmd_getval(const cmdmap_t& cmdmap,
63 std::string_view k, bool& val);
64
65 bool cmd_getval_compat_cephbool(
66 const cmdmap_t& cmdmap,
67 const std::string& k, bool& val);
68
69 template <typename T>
70 bool cmd_getval(const cmdmap_t& cmdmap,
71 std::string_view k, T& val)
72 {
73 auto found = cmdmap.find(k);
74 if (found == cmdmap.end()) {
75 return false;
76 }
77 try {
78 val = boost::get<T>(found->second);
79 return true;
80 } catch (boost::bad_get&) {
81 throw bad_cmd_get(k, cmdmap);
82 }
83 }
84
85 template <typename T>
86 std::optional<T> cmd_getval(const cmdmap_t& cmdmap,
87 std::string_view k)
88 {
89 T ret;
90 if (const bool found = cmd_getval(cmdmap, k, ret); found) {
91 return std::make_optional(std::move(ret));
92 } else {
93 return std::nullopt;
94 }
95 }
96
97 // with default
98
99 template <typename T, typename V>
100 T cmd_getval_or(const cmdmap_t& cmdmap, std::string_view k,
101 const V& defval)
102 {
103 auto found = cmdmap.find(k);
104 if (found == cmdmap.end()) {
105 return T(defval);
106 }
107 try {
108 return boost::get<T>(cmdmap.find(k)->second);
109 } catch (boost::bad_get&) {
110 throw bad_cmd_get(k, cmdmap);
111 }
112 }
113
114 template <typename T>
115 void
116 cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string_view k, const T& val)
117 {
118 cmdmap.insert_or_assign(std::string{k}, val);
119 }
120
121 bool validate_cmd(CephContext* cct,
122 const std::string& desc,
123 const cmdmap_t& cmdmap,
124 std::ostream& os);
125 extern int parse_osd_id(const char *s, std::ostream *pss);
126 extern long parse_pos_long(const char *s, std::ostream *pss = NULL);
127
128 }
129 #endif