Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / xpcom / string / nsTLiteralString.h
blob38ffd32bdbdfaa9b0274afd442d8a9cdba188fa9
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"
12 /**
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.
23 template <typename T>
24 class nsTLiteralString : public mozilla::detail::nsTStringRepr<T> {
25 public:
26 typedef nsTLiteralString<T> self_type;
28 #ifdef __clang__
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;
31 #else
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
35 base_string_type;
36 #endif
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;
43 public:
44 /**
45 * constructor
48 template <size_type N>
49 explicit constexpr nsTLiteralString(const char_type (&aStr)[N])
50 : nsTLiteralString(aStr, N - 1) {}
52 nsTLiteralString(const nsTLiteralString&) = default;
54 /**
55 * For compatibility with existing code that requires const ns[C]String*.
56 * Use sparingly. If possible, rewrite code to use const ns[C]String&
57 * and the implicit cast will just work.
59 const nsTString<T>& AsString() const MOZ_LIFETIME_BOUND {
60 return *reinterpret_cast<const nsTString<T>*>(this);
63 operator const nsTString<T>&() const MOZ_LIFETIME_BOUND { return AsString(); }
65 template <typename N, typename Dummy>
66 struct raw_type {
67 typedef N* type;
70 #ifdef MOZ_USE_CHAR16_WRAPPER
71 template <typename Dummy>
72 struct raw_type<char16_t, Dummy> {
73 typedef char16ptr_t type;
75 #endif
77 /**
78 * Prohibit get() on temporaries as in "x"_ns.get().
79 * These should be written as just "x", using a string literal directly.
81 const typename raw_type<T, int>::type get() const&& = delete;
82 const typename raw_type<T, int>::type get() const& { return this->mData; }
84 // At least older gcc versions do not accept these friend declarations,
85 // complaining about an "invalid argument list" here, but not where the actual
86 // operators are defined or used. We make the supposed-to-be-private constructor
87 // public when building with gcc, relying on the default clang builds to fail if
88 // any non-private use of that constructor would get into the codebase.
89 #if defined(__clang__)
90 private:
91 friend constexpr auto operator"" _ns(const char* aStr, std::size_t aLen);
92 friend constexpr auto operator"" _ns(const char16_t* aStr, std::size_t aLen);
93 #else
94 public:
95 #endif
96 // Only for use by operator""
97 constexpr nsTLiteralString(const char_type* aStr, size_t aLen)
98 : base_string_type(const_cast<char_type*>(aStr), aLen,
99 DataFlags::TERMINATED | DataFlags::LITERAL,
100 ClassFlags::NULL_TERMINATED) {}
102 public:
103 // NOT TO BE IMPLEMENTED
104 template <size_type N>
105 nsTLiteralString(char_type (&aStr)[N]) = delete;
107 nsTLiteralString& operator=(const nsTLiteralString&) = delete;
110 extern template class nsTLiteralString<char>;
111 extern template class nsTLiteralString<char16_t>;
113 #endif