From 6797e78982cde99d317b08503037fec42cc49814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Obiltschnig?= Date: Sat, 30 Sep 2023 13:15:48 +0200 Subject: [PATCH] added AbstractConfiguration::getInt16(), getUInt16(), setInt16(), setUInt16(), getInt32(), getUInt32(), setInt32(), setUInt32() --- .../include/Poco/Util/AbstractConfiguration.h | 125 ++++++++++++++++++ Util/src/AbstractConfiguration.cpp | 100 ++++++++++++++ 2 files changed, 225 insertions(+) diff --git a/Util/include/Poco/Util/AbstractConfiguration.h b/Util/include/Poco/Util/AbstractConfiguration.h index 30fabc12b1..9f1e48d6e8 100644 --- a/Util/include/Poco/Util/AbstractConfiguration.h +++ b/Util/include/Poco/Util/AbstractConfiguration.h @@ -180,6 +180,78 @@ class Util_API AbstractConfiguration: public Poco::RefCountedObject /// If the value contains references to other properties (${}), these /// are expanded. + Poco::Int32 getInt32(const std::string& key) const; + /// Returns the 32-bit int value of the property with the given name. + /// Throws a NotFoundException if the key does not exist. + /// Throws a SyntaxException if the property can not be converted + /// to an Int32. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + + Poco::UInt32 getUInt32(const std::string& key) const; + /// Returns the 32-bit unsigned int value of the property with the given name. + /// Throws a NotFoundException if the key does not exist. + /// Throws a SyntaxException if the property can not be converted + /// to an UInt32. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + + Poco::Int32 getInt32(const std::string& key, Poco::Int32 defaultValue) const; + /// If a property with the given key exists, returns the property's 32-bit int value, + /// otherwise returns the given default value. + /// Throws a SyntaxException if the property can not be converted + /// to an Int32. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + + Poco::UInt32 getUInt32(const std::string& key, Poco::UInt32 defaultValue) const; + /// If a property with the given key exists, returns the property's 32-bit unsigned int + /// value, otherwise returns the given default value. + /// Throws a SyntaxException if the property can not be converted + /// to an UInt32. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + + Poco::Int16 getInt16(const std::string& key) const; + /// Returns the 16-bit int value of the property with the given name. + /// Throws a NotFoundException if the key does not exist. + /// Throws a SyntaxException or a RangeException if the property can not be converted + /// to an Int16. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + + Poco::UInt16 getUInt16(const std::string& key) const; + /// Returns the unsigned 16-bit int value of the property with the given name. + /// Throws a NotFoundException if the key does not exist. + /// Throws a SyntaxException or a RangeException if the property can not be converted + /// to an UInt16. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + + Poco::Int16 getInt16(const std::string& key, Poco::Int16 defaultValue) const; + /// If a property with the given key exists, returns the property's 16-bit int value, + /// otherwise returns the given default value. + /// Throws a SyntaxException or a RangeException if the property can not be converted + /// to an Int16. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + + Poco::UInt16 getUInt16(const std::string& key, Poco::UInt16 defaultValue) const; + /// If a property with the given key exists, returns the property's unsigned 16-bit int + /// value, otherwise returns the given default value. + /// Throws a SyntaxException or a RangeException if the property can not be converted + /// to an UInt16. + /// Numbers starting with 0x are treated as hexadecimal. + /// If the value contains references to other properties (${}), these + /// are expanded. + #if defined(POCO_HAVE_INT64) Int64 getInt64(const std::string& key) const; @@ -268,6 +340,22 @@ class Util_API AbstractConfiguration: public Poco::RefCountedObject /// Sets the property with the given key to the given value. /// An already existing value for the key is overwritten. + virtual void setInt16(const std::string& key, Poco::Int16 value); + /// Sets the property with the given key to the given value. + /// An already existing value for the key is overwritten. + + virtual void setUInt16(const std::string& key, Poco::UInt16 value); + /// Sets the property with the given key to the given value. + /// An already existing value for the key is overwritten. + + virtual void setInt32(const std::string& key, Poco::Int32 value); + /// Sets the property with the given key to the given value. + /// An already existing value for the key is overwritten. + + virtual void setUInt32(const std::string& key, Poco::UInt32 value); + /// Sets the property with the given key to the given value. + /// An already existing value for the key is overwritten. + #if defined(POCO_HAVE_INT64) virtual void setInt64(const std::string& key, Int64 value); @@ -353,6 +441,14 @@ class Util_API AbstractConfiguration: public Poco::RefCountedObject /// Returns string as unsigned integer. /// Decimal and hexadecimal notation is supported. + static Poco::Int16 parseInt16(const std::string& value); + /// Returns string as signed 16-bit integer. + /// Decimal and hexadecimal notation is supported. + + static Poco::UInt16 parseUInt16(const std::string& value); + /// Returns string as unsigned 16-bit integer. + /// Decimal and hexadecimal notation is supported. + #if defined(POCO_HAVE_INT64) static Int64 parseInt64(const std::string& value); @@ -387,6 +483,35 @@ class Util_API AbstractConfiguration: public Poco::RefCountedObject }; +// +// inlines +// + + +inline Poco::Int32 AbstractConfiguration::getInt32(const std::string& key) const +{ + return getInt(key); +} + + +inline Poco::Int32 AbstractConfiguration::getInt32(const std::string& key, Poco::Int32 defaultValue) const +{ + return getInt(key, defaultValue); +} + + +inline Poco::UInt32 AbstractConfiguration::getUInt32(const std::string& key) const +{ + return getUInt(key); +} + + +inline Poco::UInt32 AbstractConfiguration::getUInt32(const std::string& key, Poco::UInt32 defaultValue) const +{ + return getUInt(key, defaultValue); +} + + } } // namespace Poco::Util diff --git a/Util/src/AbstractConfiguration.cpp b/Util/src/AbstractConfiguration.cpp index 559ea65ded..df17ff4053 100644 --- a/Util/src/AbstractConfiguration.cpp +++ b/Util/src/AbstractConfiguration.cpp @@ -163,6 +163,54 @@ unsigned AbstractConfiguration::getUInt(const std::string& key, unsigned default } +Poco::Int16 AbstractConfiguration::getInt16(const std::string& key) const +{ + Mutex::ScopedLock lock(_mutex); + + std::string value; + if (getRaw(key, value)) + return parseInt16(internalExpand(value)); + else + throw NotFoundException(key); +} + + +Poco::Int16 AbstractConfiguration::getInt16(const std::string& key, Poco::Int16 defaultValue) const +{ + Mutex::ScopedLock lock(_mutex); + + std::string value; + if (getRaw(key, value)) + return parseInt16(internalExpand(value)); + else + return defaultValue; +} + + +Poco::UInt16 AbstractConfiguration::getUInt16(const std::string& key) const +{ + Mutex::ScopedLock lock(_mutex); + + std::string value; + if (getRaw(key, value)) + return parseUInt16(internalExpand(value)); + else + throw NotFoundException(key); +} + + +Poco::UInt16 AbstractConfiguration::getUInt16(const std::string& key, Poco::UInt16 defaultValue) const +{ + Mutex::ScopedLock lock(_mutex); + + std::string value; + if (getRaw(key, value)) + return parseUInt16(internalExpand(value)); + else + return defaultValue; +} + + #if defined(POCO_HAVE_INT64) @@ -283,6 +331,30 @@ void AbstractConfiguration::setUInt(const std::string& key, unsigned int value) } +void AbstractConfiguration::setInt16(const std::string& key, Poco::Int16 value) +{ + setRawWithEvent(key, NumberFormatter::format(value)); +} + + +void AbstractConfiguration::setUInt16(const std::string& key, Poco::UInt16 value) +{ + setRawWithEvent(key, NumberFormatter::format(value)); +} + + +void AbstractConfiguration::setInt32(const std::string& key, Poco::Int32 value) +{ + setRawWithEvent(key, NumberFormatter::format(value)); +} + + +void AbstractConfiguration::setUInt32(const std::string& key, Poco::UInt32 value) +{ + setRawWithEvent(key, NumberFormatter::format(value)); +} + + #if defined(POCO_HAVE_INT64) @@ -475,6 +547,34 @@ unsigned AbstractConfiguration::parseUInt(const std::string& value) } +Poco::Int16 AbstractConfiguration::parseInt16(const std::string& value) +{ + int intValue = 0; + if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0)) + intValue = static_cast(NumberParser::parseHex(value)); + else + intValue = NumberParser::parse(value); + if (intValue >= -32768 && intValue <= 32767) + return static_cast(intValue); + else + throw Poco::RangeException("Not a valid 16-bit integer value", value); +} + + +Poco::UInt16 AbstractConfiguration::parseUInt16(const std::string& value) +{ + unsigned uintValue; + if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0)) + uintValue = NumberParser::parseHex(value); + else + uintValue = NumberParser::parseUnsigned(value); + if (uintValue <= 65535) + return static_cast(uintValue); + else + throw Poco::RangeException("Not a valid unsigned 16-bit integer value", value); +} + + Int64 AbstractConfiguration::parseInt64(const std::string& value) { if ((value.compare(0, 2, "0x") == 0) || (value.compare(0, 2, "0X") == 0))