From: Mikko Rasa Date: Wed, 1 Aug 2012 15:01:10 +0000 (+0300) Subject: Provide a way to get an integer type matching the size of another type X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=commitdiff_plain;h=c29459b08eca95418fbdee714a1c59f3becdb2ce Provide a way to get an integer type matching the size of another type --- diff --git a/source/core/inttypes.h b/source/core/inttypes.h index 3bdac35..58debd0 100644 --- a/source/core/inttypes.h +++ b/source/core/inttypes.h @@ -46,6 +46,11 @@ struct Int typedef typename TypeChooser::Type UnsignedType; }; +/** A helper for choosing an integer that's the same size as another type. */ +template +struct MatchingInt: Int +{ }; + // Finally define convenient shorthands for the actual integer types typedef Int<8>::SignedType Int8; typedef Int<8>::UnsignedType UInt8; @@ -55,6 +60,7 @@ typedef Int<32>::SignedType Int32; typedef Int<32>::UnsignedType UInt32; typedef Int<64>::SignedType Int64; typedef Int<64>::UnsignedType UInt64; +typedef MatchingInt::UnsignedType IntPtr; } // namespace Msp diff --git a/source/strings/lexicalcast.cpp b/source/strings/lexicalcast.cpp index 71e346b..6a226e1 100644 --- a/source/strings/lexicalcast.cpp +++ b/source/strings/lexicalcast.cpp @@ -14,16 +14,6 @@ template struct IsSigned { enum { result = !(static_cast(-1)>0) }; }; -templatesizeof(unsigned long))> -struct Temporary -{ typedef unsigned long Type; }; - -template -struct Temporary -{ - typedef UInt64 Type; -}; - /* Helper to avoid warnings about an unsigned type never being < 0 */ template::result> struct IsNegative @@ -69,7 +59,7 @@ char *int_to_str(T v, const Fmt &f, char *end) const char *digits = (f.get_uppercase() ? udigits : ldigits); if(v) { - typename Temporary::Type w = Absolute::eval(v); + typename MatchingInt::UnsignedType w = Absolute::eval(v); while(w) { *--ptr = digits[w%base]; @@ -544,7 +534,7 @@ void operator<<(LexicalConverter &c, const char *s) { c.result(str_to_str(s, c.get_fmt())); } void operator<<(LexicalConverter &c, const void *p) -{ c.result(int_to_str(reinterpret_cast(p), c.get_fmt())); } +{ c.result(int_to_str(reinterpret_cast(p), c.get_fmt())); } /*** operator>> ***/