Line data Source code
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/. */
6 :
7 : /**
8 : * nsTLiteralString_CharT
9 : *
10 : * Stores a null-terminated, immutable sequence of characters.
11 : *
12 : * nsTString-lookalike that restricts its string value to a literal character
13 : * sequence. Can be implicitly cast to const nsTString& (the const is
14 : * essential, since this class's data are not writable). The data are assumed
15 : * to be static (permanent) and therefore, as an optimization, this class
16 : * does not have a destructor.
17 : */
18 : class nsTLiteralString_CharT : public mozilla::detail::nsTStringRepr_CharT
19 : {
20 : public:
21 :
22 : typedef nsTLiteralString_CharT self_type;
23 :
24 : public:
25 :
26 : /**
27 : * constructor
28 : */
29 :
30 : template<size_type N>
31 30237 : explicit nsTLiteralString_CharT(const char_type (&aStr)[N])
32 : : base_string_type(const_cast<char_type*>(aStr), N - 1,
33 30237 : DataFlags::TERMINATED | DataFlags::LITERAL, ClassFlags(0))
34 : {
35 30237 : }
36 :
37 : /**
38 : * For compatibility with existing code that requires const ns[C]String*.
39 : * Use sparingly. If possible, rewrite code to use const ns[C]String&
40 : * and the implicit cast will just work.
41 : */
42 22786 : const nsTString_CharT& AsString() const
43 : {
44 22786 : return *reinterpret_cast<const nsTString_CharT*>(this);
45 : }
46 :
47 22786 : operator const nsTString_CharT&() const
48 : {
49 22786 : return AsString();
50 : }
51 :
52 : /**
53 : * Prohibit get() on temporaries as in nsLiteralCString("x").get().
54 : * These should be written as just "x", using a string literal directly.
55 : */
56 : #if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
57 : char16ptr_t get() const && = delete;
58 : char16ptr_t get() const &
59 : #else
60 : const char_type* get() const && = delete;
61 0 : const char_type* get() const &
62 : #endif
63 : {
64 0 : return mData;
65 : }
66 :
67 : private:
68 :
69 : // NOT TO BE IMPLEMENTED
70 : template<size_type N>
71 : nsTLiteralString_CharT(char_type (&aStr)[N]) = delete;
72 :
73 : self_type& operator=(const self_type&) = delete;
74 : };
75 :
76 : static_assert(sizeof(nsTLiteralString_CharT) == sizeof(nsTString_CharT),
77 : "nsTLiteralString_CharT can masquerade as nsTString_CharT, "
78 : "so they must have identical layout");
|