From: Remi Gacogne Date: Tue, 24 Dec 2024 14:20:51 +0000 (+0100) Subject: QClass: Construct from a string X-Git-Tag: dnsdist-2.0.0-alpha1~160^2~34 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=02196ee00327ed717219aa5f47bc8970f45e3027;p=thirdparty%2Fpdns.git QClass: Construct from a string --- diff --git a/pdns/qtype.cc b/pdns/qtype.cc index a0d5732cd1..710421f977 100644 --- a/pdns/qtype.cc +++ b/pdns/qtype.cc @@ -166,7 +166,23 @@ QType &QType::operator=(const string &s) return *this; } -const std::string QClass::toString() const +static const std::map 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: diff --git a/pdns/qtype.hh b/pdns/qtype.hh index f5a879bef8..dd2761a65f 100644 --- a/pdns/qtype.hh +++ b/pdns/qtype.hh @@ -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; @@ -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;