Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / xpcom / string / nsTPromiseFlatString.h
blob0033d074ce5c5dad30297883e99d49e2d96a503e
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 nsTPromiseFlatString_h
8 #define nsTPromiseFlatString_h
10 #include "mozilla/Attributes.h"
11 #include "nsTString.h"
13 /**
14 * NOTE:
16 * Try to avoid flat strings. |PromiseFlat[C]String| will help you as a last
17 * resort, and this may be necessary when dealing with legacy or OS calls,
18 * but in general, requiring a null-terminated array of characters kills many
19 * of the performance wins the string classes offer. Write your own code to
20 * use |nsA[C]String&|s for parameters. Write your string proccessing
21 * algorithms to exploit iterators. If you do this, you will benefit from
22 * being able to chain operations without copying or allocating and your code
23 * will be significantly more efficient. Remember, a function that takes an
24 * |const nsA[C]String&| can always be passed a raw character pointer by
25 * wrapping it (for free) in a |nsDependent[C]String|. But a function that
26 * takes a character pointer always has the potential to force allocation and
27 * copying.
30 * How to use it:
32 * A |nsPromiseFlat[C]String| doesn't necessarily own the characters it
33 * promises. You must never use it to promise characters out of a string
34 * with a shorter lifespan. The typical use will be something like this:
36 * SomeOSFunction( PromiseFlatCString(aCSubstring).get() ); // GOOD
38 * Here's a BAD use:
40 * const char* buffer = PromiseFlatCString(aCSubstring).get();
41 * SomeOSFunction(buffer); // BAD!! |buffer| is a dangling pointer
43 * The only way to make one is with the function |PromiseFlat[C]String|,
44 * which produce a |const| instance. ``What if I need to keep a promise
45 * around for a little while?'' you might ask. In that case, you can keep a
46 * reference, like so:
48 * const nsCString& flat = PromiseFlatString(aCSubstring);
49 * // Temporaries usually die after the full expression containing the
50 * // expression that created the temporary is evaluated. But when a
51 * // temporary is assigned to a local reference, the temporary's lifetime
52 * // is extended to the reference's lifetime (C++11 [class.temporary]p5).
53 * //
54 * // This reference holds the anonymous temporary alive. But remember: it
55 * // must _still_ have a lifetime shorter than that of |aCSubstring|, and
56 * // |aCSubstring| must not be changed while the PromiseFlatString lives.
58 * SomeOSFunction(flat.get());
59 * SomeOtherOSFunction(flat.get());
62 * How does it work?
64 * A |nsPromiseFlat[C]String| is just a wrapper for another string. If you
65 * apply it to a string that happens to be flat, your promise is just a
66 * dependent reference to the string's data. If you apply it to a non-flat
67 * string, then a temporary flat string is created for you, by allocating and
68 * copying. In the event that you end up assigning the result into a sharing
69 * string (e.g., |nsTString|), the right thing happens.
72 template <typename T>
73 class MOZ_STACK_CLASS nsTPromiseFlatString : public nsTString<T> {
74 public:
75 typedef nsTPromiseFlatString<T> self_type;
76 typedef nsTString<T> base_string_type;
77 typedef typename base_string_type::substring_type substring_type;
78 typedef typename base_string_type::string_type string_type;
79 typedef typename base_string_type::substring_tuple_type substring_tuple_type;
80 typedef typename base_string_type::char_type char_type;
81 typedef typename base_string_type::size_type size_type;
83 // These are only for internal use within the string classes:
84 typedef typename base_string_type::DataFlags DataFlags;
85 typedef typename base_string_type::ClassFlags ClassFlags;
87 private:
88 void Init(const substring_type&);
90 // NOT TO BE IMPLEMENTED
91 void operator=(const self_type&) = delete;
93 // NOT TO BE IMPLEMENTED
94 nsTPromiseFlatString(const self_type&) = delete;
96 // NOT TO BE IMPLEMENTED
97 nsTPromiseFlatString() = delete;
99 // NOT TO BE IMPLEMENTED
100 nsTPromiseFlatString(const string_type& aStr) = delete;
102 public:
103 explicit nsTPromiseFlatString(const substring_type& aStr) : string_type() {
104 Init(aStr);
107 explicit nsTPromiseFlatString(const substring_tuple_type& aTuple)
108 : string_type() {
109 // nothing else to do here except assign the value of the tuple
110 // into ourselves.
111 this->Assign(aTuple);
115 extern template class nsTPromiseFlatString<char>;
116 extern template class nsTPromiseFlatString<char16_t>;
118 // We template this so that the constructor is chosen based on the type of the
119 // parameter. This allows us to reject attempts to promise a flat flat string.
120 template <class T>
121 const nsTPromiseFlatString<T> TPromiseFlatString(
122 const typename nsTPromiseFlatString<T>::substring_type& aString) {
123 return nsTPromiseFlatString<T>(aString);
126 template <class T>
127 const nsTPromiseFlatString<T> TPromiseFlatString(
128 const typename nsTPromiseFlatString<T>::substring_tuple_type& aString) {
129 return nsTPromiseFlatString<T>(aString);
132 #ifndef PromiseFlatCString
133 # define PromiseFlatCString TPromiseFlatString<char>
134 #endif
136 #ifndef PromiseFlatString
137 # define PromiseFlatString TPromiseFlatString<char16_t>
138 #endif
140 #endif