]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/cmdparse.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / common / cmdparse.h
CommitLineData
7c673cae
FG
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 <string>
7#include <sstream>
8#include <map>
9#include <boost/variant.hpp>
10#include <vector>
11#include <stdexcept>
12#include "common/Formatter.h"
13#include "common/BackTrace.h"
14
15class CephContext;
16
17/* this is handy; can't believe it's not standard */
18#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a))
19
20typedef boost::variant<std::string,
21 bool,
22 int64_t,
23 double,
24 std::vector<std::string>,
25 std::vector<int64_t>> cmd_vartype;
26typedef std::map<std::string, cmd_vartype> cmdmap_t;
27
28std::string cmddesc_get_prefix(const std::string &cmddesc);
29void dump_cmd_to_json(ceph::Formatter *f, const std::string& cmd);
30void dump_cmd_and_help_to_json(ceph::Formatter *f,
31 const std::string& secname,
32 const std::string& cmd,
33 const std::string& helptext);
34void 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);
42bool cmdmap_from_json(std::vector<std::string> cmd, cmdmap_t *mapp,
43 std::stringstream &ss);
44void cmdmap_dump(const cmdmap_t &cmdmap, ceph::Formatter *f);
45void handle_bad_get(CephContext *cct, std::string k, const char *name);
46
47std::string cmd_vartype_stringify(const cmd_vartype& v);
48
49template <typename T>
50bool
51cmd_getval(CephContext *cct, const cmdmap_t& cmdmap, std::string k, T& val)
52{
53 if (cmdmap.count(k)) {
54 try {
55 val = boost::get<T>(cmdmap.find(k)->second);
56 return true;
57 } catch (boost::bad_get) {
58 handle_bad_get(cct, k, typeid(T).name());
59 }
60 }
61 return false;
62}
63
64// with default
65
66template <typename T>
67void
68cmd_getval(CephContext *cct, cmdmap_t& cmdmap, std::string k, T& val, T defval)
69{
70 if (!cmd_getval(cct, cmdmap, k, val))
71 val = defval;
72}
73
74template <typename T>
75void
76cmd_putval(CephContext *cct, cmdmap_t& cmdmap, std::string k, T val)
77{
78 cmdmap[k] = val;
79}
80#endif