Index: base/string_number_conversions.cc |
diff --git a/base/string_number_conversions.cc b/base/string_number_conversions.cc |
index e80b6499f2e04c0787a48d08f87b306e9fc3e468..991acde4ee1a0d80694a9bd028ab60442e976811 100644 |
--- a/base/string_number_conversions.cc |
+++ b/base/string_number_conversions.cc |
@@ -346,6 +346,51 @@ bool String16ToIntImpl(const StringPiece16& input, VALUE* output) { |
input.begin(), input.end(), output); |
} |
+// A class template for mapping the size of a pointer to its corresponding |
+// fundamental integer type. |
+template<int PointerSize> |
+class PointerSizeToIntT { |
+}; |
+ |
+// A partial specialization of PointerSizeToIntT for 32-bit pointers. |
+template<> |
+class PointerSizeToIntT<sizeof(uint32)> { |
+ public: |
+ typedef uint32 IntType; |
+}; |
+ |
+// A partial specialization of PointerSizeToIntT for 64-bit pointers. |
+template<> |
+class PointerSizeToIntT<sizeof(uint64)> { |
+ public: |
+ typedef uint64 IntType; |
+}; |
+ |
+// A collection of helper functions for round-tripping pointer values. |
+class PointerHelper { |
+ public: |
+ // The appropriate fundamental integer type for a pointer. |
+ typedef PointerSizeToIntT<sizeof(void*)>::IntType IntType; |
+ |
+ static std::string ToString(void* value) { |
+ return IntToStringT<std::string, IntType, IntType, false>:: |
+ IntToString(reinterpret_cast<IntType>(value)); |
+ } |
+ |
+ static string16 ToString16(void* value) { |
+ return IntToStringT<string16, IntType, IntType, false>:: |
+ IntToString(reinterpret_cast<IntType>(value)); |
+ } |
+ |
+ static bool ToPointer(const base::StringPiece& input, void** output) { |
+ return StringToIntImpl(input, reinterpret_cast<IntType*>(output)); |
+ } |
+ |
+ static bool ToPointer16(const base::StringPiece16& input, void** output) { |
+ return String16ToIntImpl(input, reinterpret_cast<IntType*>(output)); |
+ } |
+}; |
+ |
} // namespace |
std::string IntToString(int value) { |
@@ -387,6 +432,14 @@ string16 Uint64ToString16(uint64 value) { |
IntToString(value); |
} |
+std::string PointerToString(void* value) { |
+ return PointerHelper::ToString(value); |
+} |
+ |
+string16 PointerToString16(void* value) { |
+ return PointerHelper::ToString16(value); |
+} |
+ |
std::string DoubleToString(double value) { |
// According to g_fmt.cc, it is sufficient to declare a buffer of size 32. |
char buffer[32]; |
@@ -426,6 +479,14 @@ bool StringToUint64(const StringPiece16& input, uint64* output) { |
return String16ToIntImpl(input, output); |
} |
+bool StringToPointer(const StringPiece& input, void** output) { |
+ return PointerHelper::ToPointer(input, output); |
+} |
+ |
+bool StringToPointer(const StringPiece16& input, void** output) { |
+ return PointerHelper::ToPointer16(input, output); |
+} |
+ |
bool StringToSizeT(const StringPiece& input, size_t* output) { |
return StringToIntImpl(input, output); |
} |