[Libreoffice-commits] .: sal/inc
Caolán McNamara
caolan at kemper.freedesktop.org
Wed Dec 8 04:22:26 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 202268247f1eb7add816fdd0856acb62f2265c66
Author: Caolán McNamara <caolanm at redhat.com>
Date: Wed Dec 8 10:30:09 2010 +0000
Use c++0x features to detect misuse of SAL_N_ELEMENTS and CONSTASCII
diff --git a/sal/inc/rtl/string.h b/sal/inc/rtl/string.h
index 8acdc03..2a7a2d9 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 f01b5c6..723117c 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..e4c3513 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) && defined(__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