Skip to content

Commit

Permalink
QClass: Construct from a string
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Jan 16, 2025
1 parent a316e48 commit 02196ee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 17 additions & 1 deletion pdns/qtype.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,23 @@ QType &QType::operator=(const string &s)
return *this;
}

const std::string QClass::toString() const
static const std::map<const std::string, uint16_t> s_classMap = {
{"IN", QClass::IN},
{"CHAOS", QClass::CHAOS},
{"NONE", QClass::NONE},
{"ANY", QClass::ANY},
};

QClass::QClass(const std::string& code)
{
auto mapIt = s_classMap.find(code);
if (mapIt == s_classMap.end()) {
throw std::runtime_error("Invalid QClass '" + code + "'");
}
qclass = mapIt->second;
}

std::string QClass::toString() const
{
switch (qclass) {
case IN:
Expand Down
3 changes: 2 additions & 1 deletion pdns/qtype.hh
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ inline size_t hash_value(const QType qtype) {
struct QClass
{
constexpr QClass(uint16_t code = 0) : qclass(code) {}
explicit QClass(const std::string& code);

constexpr operator uint16_t() const {
return qclass;
Expand All @@ -186,7 +187,7 @@ struct QClass
{
return qclass;
}
const std::string toString() const;
std::string toString() const;

static const QClass IN;
static const QClass CHAOS;
Expand Down

0 comments on commit 02196ee

Please sign in to comment.