From 1d1f048769b68ebc16bdc007db5f97be6b1d11e6 Mon Sep 17 00:00:00 2001 From: noloader Date: Tue, 7 Jul 2015 23:40:46 +0000 Subject: Added GetAsUIntValue{WithDefault} to help manage signed/unsigned warnings. The value is still written as an signed int; but its retrieved, range checked and then returned as an unsigned int git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@573 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- cryptlib.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/cryptlib.h b/cryptlib.h index 4068722..906d498 100644 --- a/cryptlib.h +++ b/cryptlib.h @@ -217,7 +217,7 @@ struct CRYPTOPP_DLL DecodingResult \note To obtain an object that implements NameValuePairs for the purpose of parameter passing, use the MakeParameters() function. \note To get a value from NameValuePairs, you need to know the name and the type of the value. - Call GetValueNames() on a NameValuePairs object to obtain a list of value names that it supports. + Call GetValueNames() on a NameValuePairs object to obtain a list of value names that it contains. Then look at the Name namespace documentation to see what the type of each value is, or alternatively, call GetIntValue() with the value name, and if the type is not int, a ValueTypeMismatch exception will be thrown and you can get the actual type from the exception object. @@ -282,10 +282,31 @@ public: CRYPTOPP_DLL bool GetIntValue(const char *name, int &value) const {return GetValue(name, value);} + //! get a named value as an unsigned int, but written as an int. + /*! used to avoid polluting library code with casts. If the underlying type + is not int, then a type mismatch exception will be thrown. */ + CRYPTOPP_DLL bool GetAsUIntValue(const char *name, unsigned int &value) const + { + int v; + if(!GetValue(name, v) || v < 0) return false; + value = static_cast(v); + return true; + } + //! get a named value with type int, with default CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const {return GetValueWithDefault(name, defaultValue);} + //! get a named value as an unsigned int, but written as an int. + /*! used to avoid polluting library code with casts. If the underlying type + is not int, then a type mismatch exception will be thrown. */ + CRYPTOPP_DLL unsigned int GetAsUIntValueWithDefault(const char *name, unsigned int defaultValue) const + { + int v; + if(!GetValue(name, v)) return defaultValue; + return static_cast(v); + } + //! used by derived classes to check for type mismatch CRYPTOPP_DLL static void CRYPTOPP_API ThrowIfTypeMismatch(const char *name, const std::type_info &stored, const std::type_info &retrieving) {if (stored != retrieving) throw ValueTypeMismatch(name, stored, retrieving);} -- cgit v1.2.1