]> git.tdb.fi Git - libs/core.git/commitdiff
Provide a way to get an integer type matching the size of another type
authorMikko Rasa <tdb@tdb.fi>
Wed, 1 Aug 2012 15:01:10 +0000 (18:01 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 1 Aug 2012 15:01:10 +0000 (18:01 +0300)
source/core/inttypes.h
source/strings/lexicalcast.cpp

index 3bdac35c160925aae2a0b3aef5c6b45a66cdefa0..58debd00f67542ed751bacc4f320789948f83c83 100644 (file)
@@ -46,6 +46,11 @@ struct Int
        typedef typename TypeChooser<PlatformUnsignedTypes, Predicate>::Type UnsignedType;
 };
 
        typedef typename TypeChooser<PlatformUnsignedTypes, Predicate>::Type UnsignedType;
 };
 
+/** A helper for choosing an integer that's the same size as another type. */
+template<typename T>
+struct MatchingInt: Int<sizeof(T)*CHAR_BIT>
+{ };
+
 // Finally define convenient shorthands for the actual integer types
 typedef Int<8>::SignedType Int8;
 typedef Int<8>::UnsignedType UInt8;
 // 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 Int<32>::UnsignedType UInt32;
 typedef Int<64>::SignedType Int64;
 typedef Int<64>::UnsignedType UInt64;
+typedef MatchingInt<void *>::UnsignedType IntPtr;
 
 } // namespace Msp
 
 
 } // namespace Msp
 
index 71e346b7651818f9c91006fdfacaa8dce05613ed..6a226e1d27ac5ff356589c4e59f06c65085ebde4 100644 (file)
@@ -14,16 +14,6 @@ template<typename T>
 struct IsSigned
 { enum { result = !(static_cast<T>(-1)>0) }; };
 
 struct IsSigned
 { enum { result = !(static_cast<T>(-1)>0) }; };
 
-template<typename T, bool f = (sizeof(T)>sizeof(unsigned long))>
-struct Temporary
-{ typedef unsigned long Type; };
-
-template<typename T>
-struct Temporary<T, true>
-{
-       typedef UInt64 Type;
-};
-
 /* Helper to avoid warnings about an unsigned type never being < 0 */
 template<typename T, bool f = IsSigned<T>::result>
 struct IsNegative
 /* Helper to avoid warnings about an unsigned type never being < 0 */
 template<typename T, bool f = IsSigned<T>::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)
        {
        const char *digits = (f.get_uppercase() ? udigits : ldigits);
        if(v)
        {
-               typename Temporary<T>::Type w = Absolute<T>::eval(v);
+               typename MatchingInt<T>::UnsignedType w = Absolute<T>::eval(v);
                while(w)
                {
                        *--ptr = digits[w%base];
                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(str_to_str(s, c.get_fmt())); }
 
 void operator<<(LexicalConverter &c, const void *p)
-{ c.result(int_to_str(reinterpret_cast<unsigned long>(p), c.get_fmt())); }
+{ c.result(int_to_str(reinterpret_cast<IntPtr>(p), c.get_fmt())); }
 
 
 /*** operator>> ***/
 
 
 /*** operator>> ***/