michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Implements a UTF-16 character type. */ michael@0: michael@0: #ifndef mozilla_Char16_h michael@0: #define mozilla_Char16_h michael@0: michael@0: #ifdef __cplusplus michael@0: michael@0: /* michael@0: * C++11 introduces a char16_t type and support for UTF-16 string and character michael@0: * literals. C++11's char16_t is a distinct builtin type. Technically, char16_t michael@0: * is a 16-bit code unit of a Unicode code point, not a "character". michael@0: */ michael@0: michael@0: #ifdef _MSC_VER michael@0: /* michael@0: * C++11 says char16_t is a distinct builtin type, but Windows's yvals.h michael@0: * typedefs char16_t as an unsigned short. We would like to alias char16_t michael@0: * to Windows's 16-bit wchar_t so we can declare UTF-16 literals as constant michael@0: * expressions (and pass char16_t pointers to Windows APIs). We #define michael@0: * _CHAR16T here in order to prevent yvals.h from overriding our char16_t michael@0: * typedefs, which we set to wchar_t for C++ code. michael@0: * michael@0: * In addition, #defining _CHAR16T will prevent yvals.h from defining a michael@0: * char32_t type, so we have to undo that damage here and provide our own, michael@0: * which is identical to the yvals.h type. michael@0: */ michael@0: # define MOZ_UTF16_HELPER(s) L##s michael@0: # define _CHAR16T michael@0: typedef wchar_t char16_t; michael@0: typedef unsigned int char32_t; michael@0: #else michael@0: /* C++11 has a builtin char16_t type. */ michael@0: # define MOZ_UTF16_HELPER(s) u##s michael@0: /** michael@0: * This macro is used to distinguish when char16_t would be a distinct michael@0: * typedef from wchar_t. michael@0: */ michael@0: # define MOZ_CHAR16_IS_NOT_WCHAR michael@0: # ifdef WIN32 michael@0: # define MOZ_USE_CHAR16_WRAPPER michael@0: # endif michael@0: #endif michael@0: michael@0: #ifdef MOZ_USE_CHAR16_WRAPPER michael@0: # include michael@0: /** michael@0: * Win32 API extensively uses wchar_t, which is represented by a separated michael@0: * builtin type than char16_t per spec. It's not the case for MSVC, but GCC michael@0: * follows the spec. We want to mix wchar_t and char16_t on Windows builds. michael@0: * This class is supposed to make it easier. It stores char16_t const pointer, michael@0: * but provides implicit casts for wchar_t as well. On other platforms, we michael@0: * simply use |typedef const char16_t* char16ptr_t|. Here, we want to make michael@0: * the class as similar to this typedef, including providing some casts that michael@0: * are allowed by the typedef. michael@0: */ michael@0: class char16ptr_t michael@0: { michael@0: private: michael@0: const char16_t* ptr; michael@0: static_assert(sizeof(char16_t) == sizeof(wchar_t), "char16_t and wchar_t sizes differ"); michael@0: michael@0: public: michael@0: char16ptr_t(const char16_t* p) : ptr(p) {} michael@0: char16ptr_t(const wchar_t* p) : ptr(reinterpret_cast(p)) {} michael@0: michael@0: /* Without this, nullptr assignment would be ambiguous. */ michael@0: constexpr char16ptr_t(decltype(nullptr)) : ptr(nullptr) {} michael@0: michael@0: operator const char16_t*() const { michael@0: return ptr; michael@0: } michael@0: operator const wchar_t*() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: operator const void*() const { michael@0: return ptr; michael@0: } michael@0: operator bool() const { michael@0: return ptr != nullptr; michael@0: } michael@0: operator std::wstring() const { michael@0: return std::wstring(static_cast(*this)); michael@0: } michael@0: michael@0: /* Explicit cast operators to allow things like (char16_t*)str. */ michael@0: explicit operator char16_t*() const { michael@0: return const_cast(ptr); michael@0: } michael@0: explicit operator wchar_t*() const { michael@0: return const_cast(static_cast(*this)); michael@0: } michael@0: explicit operator int() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: explicit operator unsigned int() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: explicit operator long() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: explicit operator unsigned long() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: explicit operator long long() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: explicit operator unsigned long long() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: michael@0: /** michael@0: * Some Windows API calls accept BYTE* but require that data actually be WCHAR*. michael@0: * Supporting this requires explicit operators to support the requisite explicit michael@0: * casts. michael@0: */ michael@0: explicit operator const char*() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: explicit operator const unsigned char*() const { michael@0: return reinterpret_cast(ptr); michael@0: } michael@0: explicit operator unsigned char*() const { michael@0: return const_cast(reinterpret_cast(ptr)); michael@0: } michael@0: explicit operator void*() const { michael@0: return const_cast(ptr); michael@0: } michael@0: michael@0: /* Some operators used on pointers. */ michael@0: char16_t operator[](size_t i) const { michael@0: return ptr[i]; michael@0: } michael@0: bool operator==(const char16ptr_t &x) const { michael@0: return ptr == x.ptr; michael@0: } michael@0: bool operator==(decltype(nullptr)) const { michael@0: return ptr == nullptr; michael@0: } michael@0: bool operator!=(const char16ptr_t &x) const { michael@0: return ptr != x.ptr; michael@0: } michael@0: bool operator!=(decltype(nullptr)) const { michael@0: return ptr != nullptr; michael@0: } michael@0: char16ptr_t operator+(int aValue) const { michael@0: return char16ptr_t(ptr + aValue); michael@0: } michael@0: char16ptr_t operator+(unsigned int aValue) const { michael@0: return char16ptr_t(ptr + aValue); michael@0: } michael@0: char16ptr_t operator+(long aValue) const { michael@0: return char16ptr_t(ptr + aValue); michael@0: } michael@0: char16ptr_t operator+(unsigned long aValue) const { michael@0: return char16ptr_t(ptr + aValue); michael@0: } michael@0: char16ptr_t operator+(long long aValue) const { michael@0: return char16ptr_t(ptr + aValue); michael@0: } michael@0: char16ptr_t operator+(unsigned long long aValue) const { michael@0: return char16ptr_t(ptr + aValue); michael@0: } michael@0: ptrdiff_t operator-(const char16ptr_t &other) const { michael@0: return ptr - other.ptr; michael@0: } michael@0: }; michael@0: michael@0: inline decltype((char*)0-(char*)0) michael@0: operator-(const char16_t* x, const char16ptr_t y) { michael@0: return x - static_cast(y); michael@0: } michael@0: michael@0: #else michael@0: michael@0: typedef const char16_t* char16ptr_t; michael@0: michael@0: #endif michael@0: michael@0: /* michael@0: * Macro arguments used in concatenation or stringification won't be expanded. michael@0: * Therefore, in order for |MOZ_UTF16(FOO)| to work as expected (which is to michael@0: * expand |FOO| before doing whatever |MOZ_UTF16| needs to do to it) a helper michael@0: * macro, |MOZ_UTF16_HELPER| needs to be inserted in between to allow the macro michael@0: * argument to expand. See "3.10.6 Separate Expansion of Macro Arguments" of the michael@0: * CPP manual for a more accurate and precise explanation. michael@0: */ michael@0: #define MOZ_UTF16(s) MOZ_UTF16_HELPER(s) michael@0: michael@0: static_assert(sizeof(char16_t) == 2, "Is char16_t type 16 bits?"); michael@0: static_assert(char16_t(-1) > char16_t(0), "Is char16_t type unsigned?"); michael@0: static_assert(sizeof(MOZ_UTF16('A')) == 2, "Is char literal 16 bits?"); michael@0: static_assert(sizeof(MOZ_UTF16("")[0]) == 2, "Is string char 16 bits?"); michael@0: michael@0: #endif michael@0: michael@0: #endif /* mozilla_Char16_h */