1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsTLiteralString_h
8 #define nsTLiteralString_h
10 #include "nsTStringRepr.h"
13 * nsTLiteralString_CharT
15 * Stores a null-terminated, immutable sequence of characters.
17 * nsTString-lookalike that restricts its string value to a literal character
18 * sequence. Can be implicitly cast to const nsTString& (the const is
19 * essential, since this class's data are not writable). The data are assumed
20 * to be static (permanent) and therefore, as an optimization, this class
21 * does not have a destructor.
24 class nsTLiteralString
: public mozilla::detail::nsTStringRepr
<T
> {
26 typedef nsTLiteralString
<T
> self_type
;
29 // bindgen w/ clang 3.9 at least chokes on a typedef, but using is okay.
30 using typename
mozilla::detail::nsTStringRepr
<T
>::base_string_type
;
32 // On the other hand msvc chokes on the using statement. It seems others
33 // don't care either way so we lump them in here.
34 typedef typename
mozilla::detail::nsTStringRepr
<T
>::base_string_type
38 typedef typename
base_string_type::char_type char_type
;
39 typedef typename
base_string_type::size_type size_type
;
40 typedef typename
base_string_type::DataFlags DataFlags
;
41 typedef typename
base_string_type::ClassFlags ClassFlags
;
48 template <size_type N
>
49 explicit constexpr nsTLiteralString(const char_type (&aStr
)[N
])
50 : nsTLiteralString(aStr
, N
- 1) {}
53 * For compatibility with existing code that requires const ns[C]String*.
54 * Use sparingly. If possible, rewrite code to use const ns[C]String&
55 * and the implicit cast will just work.
57 MOZ_LIFETIME_BOUND
const nsTString
<T
>& AsString() const {
58 return *reinterpret_cast<const nsTString
<T
>*>(this);
61 MOZ_LIFETIME_BOUND
operator const nsTString
<T
>&() const { return AsString(); }
63 template <typename N
, typename Dummy
>
68 #ifdef MOZ_USE_CHAR16_WRAPPER
69 template <typename Dummy
>
70 struct raw_type
<char16_t
, Dummy
> {
71 typedef char16ptr_t type
;
76 * Prohibit get() on temporaries as in "x"_ns.get().
77 * These should be written as just "x", using a string literal directly.
79 const typename raw_type
<T
, int>::type
get() const&& = delete;
80 const typename raw_type
<T
, int>::type
get() const& { return this->mData
; }
82 // At least older gcc versions do not accept these friend declarations,
83 // complaining about an "invalid argument list" here, but not where the actual
84 // operators are defined or used. We make the supposed-to-be-private constructor
85 // public when building with gcc, relying on the default clang builds to fail if
86 // any non-private use of that constructor would get into the codebase.
87 #if defined(__clang__)
89 friend constexpr auto operator"" _ns(const char* aStr
, std::size_t aLen
);
90 friend constexpr auto operator"" _ns(const char16_t
* aStr
, std::size_t aLen
);
94 // Only for use by operator""
95 constexpr nsTLiteralString(const char_type
* aStr
, size_t aLen
)
96 : base_string_type(const_cast<char_type
*>(aStr
), aLen
,
97 DataFlags::TERMINATED
| DataFlags::LITERAL
,
98 ClassFlags::NULL_TERMINATED
) {}
101 // NOT TO BE IMPLEMENTED
102 template <size_type N
>
103 nsTLiteralString(char_type (&aStr
)[N
]) = delete;
105 self_type
& operator=(const self_type
&) = delete;
108 extern template class nsTLiteralString
<char>;
109 extern template class nsTLiteralString
<char16_t
>;