Skip to content

Commit

Permalink
added AbstractConfiguration::getInt16(), getUInt16(), setInt16(), set…
Browse files Browse the repository at this point in the history
…UInt16(), getInt32(), getUInt32(), setInt32(), setUInt32()
  • Loading branch information
obiltschnig committed Sep 30, 2023
1 parent 4e3543a commit 6797e78
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 0 deletions.
125 changes: 125 additions & 0 deletions Util/include/Poco/Util/AbstractConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,78 @@ class Util_API AbstractConfiguration: public Poco::RefCountedObject
/// If the value contains references to other properties (${<property>}), 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 (${<property>}), 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 (${<property>}), 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 (${<property>}), 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 (${<property>}), 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 (${<property>}), 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 (${<property>}), 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 (${<property>}), 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 (${<property>}), these
/// are expanded.

#if defined(POCO_HAVE_INT64)

Int64 getInt64(const std::string& key) const;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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


Expand Down
100 changes: 100 additions & 0 deletions Util/src/AbstractConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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<int>(NumberParser::parseHex(value));
else
intValue = NumberParser::parse(value);
if (intValue >= -32768 && intValue <= 32767)
return static_cast<Poco::Int16>(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<Poco::UInt16>(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))
Expand Down

0 comments on commit 6797e78

Please sign in to comment.