summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoloader <noloader@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2015-07-07 23:40:46 +0000
committernoloader <noloader@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2015-07-07 23:40:46 +0000
commit1d1f048769b68ebc16bdc007db5f97be6b1d11e6 (patch)
tree166ca035b8dc67e303c075789ced8b6785d6d22f
parent5fae159248b8053f57f9900ac83772b974a71001 (diff)
downloadcryptopp-1d1f048769b68ebc16bdc007db5f97be6b1d11e6.tar.gz
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
-rw-r--r--cryptlib.h23
1 files changed, 22 insertions, 1 deletions
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<unsigned int>(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<unsigned int>(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);}