Bug 796948 - Create Char16.h header file for char16_t type. r=waldo
authorChris Peterson <cpeterson@mozilla.com>
Wed, 12 Dec 2012 00:34:20 -0800
changeset 117338 793c5c49bd9b8b2a9a6a9a40283bbcc04d252a8c
parent 117337 a6a26c14f5e45d61662ea8f29d04b24506546798
child 117339 001b109e68c46353d6c7f4b943225088eb593eac
push id24097
push userryanvm@gmail.com
push dateThu, 03 Jan 2013 02:02:24 +0000
treeherdermozilla-central@4e18ac9b51e2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs796948
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 796948 - Create Char16.h header file for char16_t type. r=waldo
mfbt/Char16.h
mfbt/exported_headers.mk
new file mode 100644
--- /dev/null
+++ b/mfbt/Char16.h
@@ -0,0 +1,46 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* Implements a UTF-16 character type. */
+
+#ifndef mozilla_Char16_h_
+#define mozilla_Char16_h_
+
+#include "mozilla/Assertions.h"
+
+/*
+ * C11 and C++11 introduce a char16_t type and support for UTF-16 string and
+ * character literals. C++11's char16_t is a distinct builtin type. C11's
+ * char16_t is a typedef for uint_least16_t. Technically, char16_t is a 16-bit
+ * code unit of a Unicode code point, not a "character".
+ *
+ * For now, Char16.h only supports C++ because we don't want mix different C
+ * and C++ definitions of char16_t in the same code base.
+ */
+
+#ifdef _MSC_VER
+   /*
+    * C++11 says char16_t is a distinct builtin type, but Windows's yvals.h
+    * typedefs char16_t as an unsigned short. We would like to alias char16_t
+    * to Windows's 16-bit wchar_t so we can declare UTF-16 literals as constant
+    * expressions (and pass char16_t pointers to Windows APIs). We #define our
+    * char16_t as a macro to override yval.h's typedef of the same name.
+    */
+#  define MOZ_UTF16(s) L##s
+#  include <yvals.h>
+#  define char16_t wchar_t
+#elif defined(__cplusplus) && \
+      (__cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__))
+   /* C++11 has a builtin char16_t type. */
+#  define MOZ_UTF16(s) u##s
+#else
+#  error "Char16.h requires C++11 (or something like it) for UTF-16 support."
+#endif
+
+MOZ_STATIC_ASSERT(sizeof(char16_t) == 2, "Is char16_t type 16 bits?");
+MOZ_STATIC_ASSERT(sizeof(MOZ_UTF16('A')) == 2, "Is char literal 16 bits?");
+MOZ_STATIC_ASSERT(sizeof(MOZ_UTF16("")[0]) == 2, "Is string char 16 bits?");
+
+#endif /* mozilla_Char16_h_ */
--- a/mfbt/exported_headers.mk
+++ b/mfbt/exported_headers.mk
@@ -7,16 +7,17 @@
 # mfbt's exported headers itself.
 
 EXPORTS_NAMESPACES += mozilla
 
 EXPORTS_mozilla += \
   Assertions.h \
   Attributes.h \
   BloomFilter.h \
+  Char16.h \
   CheckedInt.h \
   Constants.h \
   DebugOnly.h \
   EnumSet.h \
   FloatingPoint.h \
   GuardObjects.h \
   HashFunctions.h \
   Likely.h \