From ffe3d2d38301b1ba430909bb8dc6f358dcdafd4f Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Wed, 5 Apr 2023 16:07:38 +0300 Subject: [PATCH] [#549] not strictly related: return enum from Element::getType() So you don't have to cast it each time it's used. --- src/lib/cc/data.cc | 6 +++--- src/lib/cc/data.h | 16 ++++++++-------- src/lib/cc/stamped_value.cc | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib/cc/data.cc b/src/lib/cc/data.cc index 20c2015484..2df39abf48 100644 --- a/src/lib/cc/data.cc +++ b/src/lib/cc/data.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2010-2022 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2010-2023 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -229,9 +229,9 @@ operator<(Element const& a, Element const& b) { return b.boolValue() || !a.boolValue(); case Element::string: return std::strcmp(a.stringValue().c_str(), b.stringValue().c_str()) < 0; + default: + isc_throw(BadValue, "cannot compare Elements of type " << to_string(a.getType())); } - isc_throw(BadValue, "cannot compare Elements of type " << - std::to_string(a.getType())); } // diff --git a/src/lib/cc/data.h b/src/lib/cc/data.h index 7cb2380713..916ad3f106 100644 --- a/src/lib/cc/data.h +++ b/src/lib/cc/data.h @@ -1,4 +1,4 @@ -// Copyright (C) 2010-2022 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2010-2023 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -126,11 +126,15 @@ public: return (position); } + // any is a special type used in list specifications, specifying + // that the elements can be of any type + enum types { integer, real, boolean, null, string, list, map, any }; + private: // technically the type could be omitted; is it useful? // should we remove it or replace it with a pure virtual // function getType? - int type_; + types type_; /// @brief Position of the element in the configuration string. Position position_; @@ -143,21 +147,17 @@ protected: /// @param pos Structure holding position of the value of the data element. /// It comprises the line number and the position within this line. The values /// held in this structure are used for error logging purposes. - Element(int t, const Position& pos = ZERO_POSITION()) + Element(types t, const Position& pos = ZERO_POSITION()) : type_(t), position_(pos) { } public: - - // any is a special type used in list specifications, specifying - // that the elements can be of any type - enum types { integer, real, boolean, null, string, list, map, any }; // base class; make dtor virtual virtual ~Element() {}; /// @return the type of this element - int getType() const { return (type_); } + types getType() const { return (type_); } /// @brief Returns position where the data element's value starts in a /// configuration string. diff --git a/src/lib/cc/stamped_value.cc b/src/lib/cc/stamped_value.cc index b6554ea829..077c688584 100644 --- a/src/lib/cc/stamped_value.cc +++ b/src/lib/cc/stamped_value.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2018-2023 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -169,7 +169,7 @@ StampedValue::validateConstruct() const { (value_->getType() != Element::real)) { isc_throw(TypeError, "StampedValue: provided value of the '" << name_ << "' parameter has invalid type: " - << Element::typeToName(static_cast(value_->getType()))); + << Element::typeToName(value_->getType())); } } @@ -184,7 +184,7 @@ StampedValue::validateAccess(Element::types type) const { isc_throw(TypeError, "StampedValue: attempt to access a '" << name_ << "' parameter as " << Element::typeToName(type) << ", but this parameter has " - << Element::typeToName(static_cast(value_->getType())) + << Element::typeToName(value_->getType()) << " type"); } } -- 2.47.2