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