Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / xpcom / string / nsTDependentString.h
blobc7194a677f82a653fdc4405c650b0e26891d54d9
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 nsTDependentString_h
8 #define nsTDependentString_h
10 #include "nsTString.h"
12 /**
13 * nsTDependentString
15 * Stores a null-terminated, immutable sequence of characters.
17 * Subclass of nsTString that restricts string value to an immutable
18 * character sequence. This class does not own its data, so the creator
19 * of objects of this type must take care to ensure that a
20 * nsTDependentString continues to reference valid memory for the
21 * duration of its use.
23 template <typename T>
24 class nsTDependentString : public nsTString<T> {
25 public:
26 typedef nsTDependentString<T> self_type;
27 typedef nsTString<T> base_string_type;
28 typedef typename base_string_type::string_type string_type;
30 typedef typename base_string_type::fallible_t fallible_t;
32 typedef typename base_string_type::char_type char_type;
33 typedef typename base_string_type::char_traits char_traits;
34 typedef
35 typename base_string_type::incompatible_char_type incompatible_char_type;
37 typedef typename base_string_type::substring_tuple_type substring_tuple_type;
39 typedef typename base_string_type::const_iterator const_iterator;
40 typedef typename base_string_type::iterator iterator;
42 typedef typename base_string_type::comparator_type comparator_type;
44 typedef typename base_string_type::const_char_iterator const_char_iterator;
46 typedef typename base_string_type::string_view string_view;
48 typedef typename base_string_type::index_type index_type;
49 typedef typename base_string_type::size_type size_type;
51 // These are only for internal use within the string classes:
52 typedef typename base_string_type::DataFlags DataFlags;
53 typedef typename base_string_type::ClassFlags ClassFlags;
55 public:
56 /**
57 * constructors
60 nsTDependentString(const char_type* aStart, const char_type* aEnd);
62 nsTDependentString(const char_type* aData, size_type aLength)
63 : string_type(const_cast<char_type*>(aData), aLength,
64 DataFlags::TERMINATED, ClassFlags(0)) {
65 this->AssertValidDependentString();
68 #if defined(MOZ_USE_CHAR16_WRAPPER)
69 template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
70 nsTDependentString(char16ptr_t aData, size_type aLength)
71 : nsTDependentString(static_cast<const char16_t*>(aData), aLength) {}
72 #endif
74 explicit nsTDependentString(const char_type* aData)
75 : string_type(const_cast<char_type*>(aData), char_traits::length(aData),
76 DataFlags::TERMINATED, ClassFlags(0)) {
77 string_type::AssertValidDependentString();
80 #if defined(MOZ_USE_CHAR16_WRAPPER)
81 template <typename Q = T, typename EnableIfChar16 = mozilla::Char16OnlyT<Q>>
82 explicit nsTDependentString(char16ptr_t aData)
83 : nsTDependentString(static_cast<const char16_t*>(aData)) {}
84 #endif
86 nsTDependentString(const string_type& aStr, index_type aStartPos)
87 : string_type() {
88 Rebind(aStr, aStartPos);
91 // Create a nsTDependentSubstring to be bound later
92 nsTDependentString() : string_type() {}
94 // auto-generated destructor OK
96 nsTDependentString(self_type&& aStr) : string_type() {
97 Rebind(aStr, /* aStartPos = */ 0);
98 aStr.SetToEmptyBuffer();
101 explicit nsTDependentString(const self_type& aStr) : string_type() {
102 Rebind(aStr, /* aStartPos = */ 0);
106 * allow this class to be bound to a different string...
109 using nsTString<T>::Rebind;
110 void Rebind(const char_type* aData) {
111 Rebind(aData, char_traits::length(aData));
114 void Rebind(const char_type* aStart, const char_type* aEnd);
115 void Rebind(const string_type&, index_type aStartPos);
117 private:
118 // NOT USED
119 nsTDependentString(const substring_tuple_type&) = delete;
120 self_type& operator=(const self_type& aStr) = delete;
123 extern template class nsTDependentString<char>;
124 extern template class nsTDependentString<char16_t>;
126 #endif