[Libreoffice-commits] .: Branch 'feature/rodatastrings' - sal/inc

Caolán McNamara caolan at kemper.freedesktop.org
Fri Nov 12 13:05:59 PST 2010


 sal/inc/rtl/string.h  |    4 ++--
 sal/inc/rtl/ustring.h |    2 +-
 sal/inc/sal/macros.h  |   26 +++++++++++++++++++++++++-
 3 files changed, 28 insertions(+), 4 deletions(-)

New commits:
commit 6f70608a25595a24115f929eed87809b96457a75
Author: Caolán McNamara <caolanm at redhat.com>
Date:   Fri Nov 12 21:03:13 2010 +0000

    Template to enforce array arg to SAL_N_ELEMENTS and RTL_CONSTASCII_STRINGPARAM

diff --git a/sal/inc/rtl/string.h b/sal/inc/rtl/string.h
index 472b6bf..b81edff 100644
--- a/sal/inc/rtl/string.h
+++ b/sal/inc/rtl/string.h
@@ -1088,7 +1088,7 @@ sal_Int32 SAL_CALL rtl_string_getToken( rtl_String ** newStr , rtl_String * str,
     its value should be 0x00.  Depending on where this macro is used, the nature
     of the supplied expression might be further restricted.
 */
-#define RTL_CONSTASCII_STRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)sizeof(constAsciiStr)-1)
+#define RTL_CONSTASCII_STRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)SAL_N_ELEMENTS(constAsciiStr)-1)
 
 /** Supply the length of an ASCII string literal.
 
@@ -1103,7 +1103,7 @@ sal_Int32 SAL_CALL rtl_string_getToken( rtl_String ** newStr , rtl_String * str,
     its value should be 0x00.  Depending on where this macro is used, the nature
     of the supplied expression might be further restricted.
 */
-#define RTL_CONSTASCII_LENGTH( constAsciiStr ) ((sal_Int32)(sizeof(constAsciiStr)-1))
+#define RTL_CONSTASCII_LENGTH( constAsciiStr ) ((sal_Int32)(SAL_N_ELEMENTS(constAsciiStr)-1))
 
 /* ======================================================================= */
 
diff --git a/sal/inc/rtl/ustring.h b/sal/inc/rtl/ustring.h
index 1d86ba0..5d8ed98 100644
--- a/sal/inc/rtl/ustring.h
+++ b/sal/inc/rtl/ustring.h
@@ -1449,7 +1449,7 @@ sal_Int32 SAL_CALL rtl_uString_getToken( rtl_uString ** newStr , rtl_uString * s
     its value should be 0x00.  Depending on where this macro is used, the nature
     of the supplied expression might be further restricted.
 */
-#define RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)(sizeof(constAsciiStr)-1)), RTL_TEXTENCODING_ASCII_US
+#define RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)(SAL_N_ELEMENTS(constAsciiStr)-1)), RTL_TEXTENCODING_ASCII_US
 
 /* ======================================================================= */
 
diff --git a/sal/inc/sal/macros.h b/sal/inc/sal/macros.h
index 1eeb165..5bebf01 100644
--- a/sal/inc/sal/macros.h
+++ b/sal/inc/sal/macros.h
@@ -29,6 +29,8 @@
 #ifndef _SAL_MACROS_H_
 #define _SAL_MACROS_H_
 
+#include <stddef.h>
+
 #ifndef SAL_MAX
 #    define SAL_MAX(a,b)            (((a) > (b)) ? (a) : (b))
 #endif
@@ -42,7 +44,29 @@
 #endif
 
 #ifndef SAL_N_ELEMENTS
-#    define SAL_N_ELEMENTS(arr)     (sizeof (arr) / sizeof ((arr)[0]))
+#    if defined(__cplusplus) && __GXX_EXPERIMENTAL_CXX0X__
+        /*
+         * Magic template to calculate at compile time the number of elements
+         * in an array. Enforcing that the argument must be a array and not
+         * a pointer, e.g.
+         *  char *pFoo="foo";
+         *  SAL_N_ELEMENTS(pFoo);
+         * fails while
+         *  SAL_N_ELEMENTS("foo");
+         * or
+         *  char aFoo[]="foo";
+         *  SAL_N_ELEMENTS(aFoo);
+         * pass
+         *
+         * Unfortunately if arr is an array of an anonymous class then we need
+         * C++0x, i.e. see
+         * http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#757
+         */
+         template <typename T, size_t S> char (&sal_n_array_size( T(&)[S] ))[S];
+#        define SAL_N_ELEMENTS(arr)     (sizeof(sal_n_array_size(arr)))
+#    else
+#        define SAL_N_ELEMENTS(arr)     (sizeof (arr) / sizeof ((arr)[0]))
+#    endif
 #endif
 
 #ifndef SAL_BOUND


More information about the Libreoffice-commits mailing list