← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba

 

Paul J. Lucas has proposed merging lp:~paul-lucas/zorba/pjl-misc into lp:zorba.

Commit message:
Fixed to_string() for char* type.

Requested reviews:
  Paul J. Lucas (paul-lucas)

For more details, see:
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/145037

Fixed to_string() for char* type.
-- 
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/145037
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'include/zorba/internal/ztd.h'
--- include/zorba/internal/ztd.h	2012-12-08 00:09:48 +0000
+++ include/zorba/internal/ztd.h	2013-01-25 22:26:24 +0000
@@ -260,16 +260,35 @@
 
 /**
  * \internal
+ * Tests whether a given type \a T is a C string type.
+ *
+ * @tparam T The type to check.
+ */
+template<typename T>
+class is_c_string {
+  typedef typename ZORBA_TR1_NS::remove_pointer<T>::type T_base;
+  typedef typename ZORBA_TR1_NS::add_const<T_base>::type T_base_const;
+public:
+  static bool const value =
+       ZORBA_TR1_NS::is_same<T_base_const*,char const*>::value
+    || ZORBA_TR1_NS::is_same<T_base_const*,unsigned char const*>::value
+    || ZORBA_TR1_NS::is_same<T_base_const*,signed char const*>::value;
+};
+
+/**
+ * \internal
  * Converts an object to its string representation.
  *
  * @tparam T The object type that:
+ *  - is not an array
  *  - is not a pointer
  *  - has an <code>ostream& operator&lt;&lt;(ostream&,T const&)</code> defined
  * @param t The object.
  * @return Returns a string representation of the object.
  */
 template<typename T> inline
-typename std::enable_if<!ZORBA_TR1_NS::is_pointer<T>::value
+typename std::enable_if<!ZORBA_TR1_NS::is_array<T>::value
+                     && !ZORBA_TR1_NS::is_pointer<T>::value
                      && has_insertion_operator<T>::value,
                         std::string>::type
 to_string( T const &t ) {
@@ -345,7 +364,7 @@
 
 /**
  * \internal
- * Specialization of \c to_string() for pointer types.
+ * Specialization of \c to_string() for pointer types other than C strings.
  *
  * @tparam T The pointer type.
  * @param p The pointer.
@@ -353,10 +372,13 @@
  * otherwise returns \c "<null>".
  */
 template<typename T> inline
-typename std::enable_if<ZORBA_TR1_NS::is_pointer<T>::value,std::string>::type
+typename std::enable_if<ZORBA_TR1_NS::is_pointer<T>::value
+                     && !is_c_string<T>::value,
+                        std::string>::type
 to_string( T p ) {
-  typedef typename ZORBA_TR1_NS::remove_pointer<T>::type const* T_const_ptr;
-  return p ? to_string( *static_cast<T_const_ptr>( p ) ) : "<null>";
+  typedef typename ZORBA_TR1_NS::remove_pointer<T>::type T_base;
+  typedef typename ZORBA_TR1_NS::add_const<T_base>::type T_base_const;
+  return p ? to_string( *static_cast<T_base_const*>( p ) ) : "<null>";
 }
 
 /**
@@ -370,6 +392,17 @@
   return s ? s : "<null>";
 }
 
+/**
+ * \internal
+ * Specialization of \c to_string() for C strings.
+ *
+ * @param s The C string.
+ * @return Returns a string representation of the object.
+ */
+inline std::string to_string( unsigned char const *s ) {
+  return s ? reinterpret_cast<char const*>( s ) : "<null>";
+}
+
 ////////// misc ///////////////////////////////////////////////////////////////
 
 /**


Follow ups