#include "common/entity_name.h"
#include "common/ceph_strings.h"
#include "common/Formatter.h"
-#include "msg/msg_types.h"
#include <sstream>
+#include <string>
+#include <string_view>
+#include <vector>
using std::string;
+using namespace std::literals::string_view_literals;
-void EntityName::encode(ceph::buffer::list& bl) const {
+void EntityName::encode(ceph::buffer::list& bl) const
+{
using ceph::encode;
- encode(type, bl);
+ {
+ uint32_t _type = type;
+ encode(_type, bl);
+ }
encode(id, bl);
}
-void EntityName::decode(ceph::buffer::list::const_iterator& bl) {
+void EntityName::decode(ceph::buffer::list::const_iterator& bl)
+{
using ceph::decode;
- uint32_t type_;
std::string id_;
- decode(type_, bl);
+ uint32_t _type;
+ decode(_type, bl);
decode(id_, bl);
- set(type_, id_);
+ set(static_cast<entity_type_t>(_type), id_);
}
-const std::array<EntityName::str_to_entity_type_t, 6> EntityName::STR_TO_ENTITY_TYPE = {{
- { CEPH_ENTITY_TYPE_AUTH, "auth" },
- { CEPH_ENTITY_TYPE_MON, "mon" },
- { CEPH_ENTITY_TYPE_OSD, "osd" },
- { CEPH_ENTITY_TYPE_MDS, "mds" },
- { CEPH_ENTITY_TYPE_MGR, "mgr" },
- { CEPH_ENTITY_TYPE_CLIENT, "client" },
-}};
+using namespace std::string_literals;
+static const std::vector<std::pair<entity_type_t, std::string>> STR_TO_ENTITY_TYPE = {
+ { CEPH_ENTITY_TYPE_AUTH, "auth"s },
+ { CEPH_ENTITY_TYPE_MON, "mon"s },
+ { CEPH_ENTITY_TYPE_OSD, "osd"s },
+ { CEPH_ENTITY_TYPE_MDS, "mds"s },
+ { CEPH_ENTITY_TYPE_MGR, "mgr"s },
+ { CEPH_ENTITY_TYPE_CLIENT, "client"s },
+};
void EntityName::dump(ceph::Formatter *f) const {
f->dump_int("type", type);
return true;
}
-void EntityName::set(uint32_t type_, std::string_view id_) {
+void EntityName::set(entity_type_t type_, std::string_view id_) {
type = type_;
id = id_;
}
int EntityName::set(std::string_view type_, std::string_view id_) {
- uint32_t t = str_to_ceph_entity_type(type_);
+ auto t = str_to_ceph_entity_type(type_);
if (t == CEPH_ENTITY_TYPE_ANY)
return -EINVAL;
set(t, id_);
return 0;
}
-void EntityName::set_type(uint32_t type_) {
+void EntityName::set_type(entity_type_t type_) {
set(type_, id);
}
if (i > 0) {
out << ", ";
}
- out << STR_TO_ENTITY_TYPE[i].str;
+ out << STR_TO_ENTITY_TYPE[i].first;
}
return out.str();
}
-uint32_t EntityName::str_to_ceph_entity_type(std::string_view s)
+entity_type_t EntityName::str_to_ceph_entity_type(std::string_view s)
{
- size_t i;
- for (i = 0; i < STR_TO_ENTITY_TYPE.size(); ++i) {
- if (s == STR_TO_ENTITY_TYPE[i].str)
- return STR_TO_ENTITY_TYPE[i].type;
+ auto l = [s](const auto& p) {
+ return s == p.second;
+ };
+ auto it = std::find_if(STR_TO_ENTITY_TYPE.begin(), STR_TO_ENTITY_TYPE.end(), std::move(l));
+ if (it == STR_TO_ENTITY_TYPE.end()) {
+ return CEPH_ENTITY_TYPE_ANY;
+ } else {
+ return it->first;
+ }
+}
+
+std::string_view EntityName::ceph_entity_type_to_str(entity_type_t type)
+{
+ auto l = [type](const auto& p) {
+ return type == p.first;
+ };
+ auto it = std::find_if(STR_TO_ENTITY_TYPE.begin(), STR_TO_ENTITY_TYPE.end(), std::move(l));
+ if (it == STR_TO_ENTITY_TYPE.end()) {
+ return "???"sv;
+ } else {
+ return it->second;
}
- return CEPH_ENTITY_TYPE_ANY;
}
bool operator<(const EntityName& a, const EntityName& b)
*
*/
-#ifndef CEPH_COMMON_ENTITY_NAME_H
-#define CEPH_COMMON_ENTITY_NAME_H
+#pragma once
#include <array>
#include <cstdint>
class entity_name_t;
namespace ceph { class Formatter; }
+using namespace std::literals::string_view_literals;
+
+
/* Represents a Ceph entity name.
*
* For example, mds.0 is the name of the first metadata server.
* client
*/
+
struct EntityName
{
+ EntityName() = default;
+ explicit EntityName(entity_type_t t) : type(t), id("*"sv) {}
+
void encode(ceph::buffer::list& bl) const;
void decode(ceph::buffer::list::const_iterator& bl);
void dump(ceph::Formatter *f) const;
const std::string& to_str() const;
const char *to_cstr() const;
bool from_str(std::string_view s);
- void set(uint32_t type_, std::string_view id_);
+ void set(entity_type_t type_, std::string_view id_);
int set(std::string_view type_, std::string_view id_);
- void set_type(uint32_t type_);
+ void set_type(entity_type_t type_);
int set_type(std::string_view type);
void set_id(std::string_view id_);
void set_name(entity_name_t n);
const char* get_type_str() const;
- uint32_t get_type() const { return type; }
+ entity_type_t get_type() const { return type; }
bool is_osd() const { return get_type() == CEPH_ENTITY_TYPE_OSD; }
bool is_mgr() const { return get_type() == CEPH_ENTITY_TYPE_MGR; }
bool is_mds() const { return get_type() == CEPH_ENTITY_TYPE_MDS; }
bool has_default_id() const;
static std::string get_valid_types_as_str();
- static uint32_t str_to_ceph_entity_type(std::string_view);
+ static entity_type_t str_to_ceph_entity_type(std::string_view);
+ static std::string_view ceph_entity_type_to_str(entity_type_t type);
friend bool operator<(const EntityName& a, const EntityName& b);
friend std::ostream& operator<<(std::ostream& out, const EntityName& n);
}
private:
- struct str_to_entity_type_t {
- uint32_t type;
- const char *str;
- };
- static const std::array<str_to_entity_type_t, 6> STR_TO_ENTITY_TYPE;
-
- uint32_t type = 0;
+ entity_type_t type = 0;
std::string id;
std::string type_id;
};
WRITE_CLASS_ENCODER(EntityName)
-
-#endif