]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
QClass: Construct from a string
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 24 Dec 2024 14:20:51 +0000 (15:20 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 16 Jan 2025 08:50:26 +0000 (09:50 +0100)
pdns/qtype.cc
pdns/qtype.hh

index a0d5732cd10ea4363c198d55974ea06e546d31e2..710421f977701de4efb1f4d9c1336ee65571434b 100644 (file)
@@ -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:
index f5a879bef8f56c60223c3538347d17c7df39dbfb..dd2761a65f296fecbb21a85c56cd8e59da4a4a12 100644 (file)
@@ -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;