Bumping manifests a=b2g-bump
[gecko.git] / xpcom / string / nsTPromiseFlatString.h
blob763b2dfdf75244e93123174d9eef208c109dabe6
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/. */
8 /**
9 * NOTE:
11 * Try to avoid flat strings. |PromiseFlat[C]String| will help you as a last
12 * resort, and this may be necessary when dealing with legacy or OS calls,
13 * but in general, requiring a null-terminated array of characters kills many
14 * of the performance wins the string classes offer. Write your own code to
15 * use |nsA[C]String&|s for parameters. Write your string proccessing
16 * algorithms to exploit iterators. If you do this, you will benefit from
17 * being able to chain operations without copying or allocating and your code
18 * will be significantly more efficient. Remember, a function that takes an
19 * |const nsA[C]String&| can always be passed a raw character pointer by
20 * wrapping it (for free) in a |nsDependent[C]String|. But a function that
21 * takes a character pointer always has the potential to force allocation and
22 * copying.
25 * How to use it:
27 * A |nsPromiseFlat[C]String| doesn't necessarily own the characters it
28 * promises. You must never use it to promise characters out of a string
29 * with a shorter lifespan. The typical use will be something like this:
31 * SomeOSFunction( PromiseFlatCString(aCSubstring).get() ); // GOOD
33 * Here's a BAD use:
35 * const char* buffer = PromiseFlatCString(aCSubstring).get();
36 * SomeOSFunction(buffer); // BAD!! |buffer| is a dangling pointer
38 * The only way to make one is with the function |PromiseFlat[C]String|,
39 * which produce a |const| instance. ``What if I need to keep a promise
40 * around for a little while?'' you might ask. In that case, you can keep a
41 * reference, like so
43 * const nsCString& flat = PromiseFlatString(aCSubstring);
44 * // this reference holds the anonymous temporary alive, but remember,
45 * // it must _still_ have a lifetime shorter than that of |aCSubstring|
47 * SomeOSFunction(flat.get());
48 * SomeOtherOSFunction(flat.get());
51 * How does it work?
53 * A |nsPromiseFlat[C]String| is just a wrapper for another string. If you
54 * apply it to a string that happens to be flat, your promise is just a
55 * dependent reference to the string's data. If you apply it to a non-flat
56 * string, then a temporary flat string is created for you, by allocating and
57 * copying. In the event that you end up assigning the result into a sharing
58 * string (e.g., |nsTString|), the right thing happens.
61 class nsTPromiseFlatString_CharT : public nsTString_CharT
63 public:
65 typedef nsTPromiseFlatString_CharT self_type;
67 private:
69 void Init(const substring_type&);
71 // NOT TO BE IMPLEMENTED
72 void operator=(const self_type&) MOZ_DELETE;
74 // NOT TO BE IMPLEMENTED
75 nsTPromiseFlatString_CharT() MOZ_DELETE;
77 // NOT TO BE IMPLEMENTED
78 nsTPromiseFlatString_CharT(const string_type& aStr) MOZ_DELETE;
80 public:
82 explicit
83 nsTPromiseFlatString_CharT(const substring_type& aStr)
84 : string_type()
86 Init(aStr);
89 explicit
90 nsTPromiseFlatString_CharT(const substring_tuple_type& aTuple)
91 : string_type()
93 // nothing else to do here except assign the value of the tuple
94 // into ourselves.
95 Assign(aTuple);
99 // We template this so that the constructor is chosen based on the type of the
100 // parameter. This allows us to reject attempts to promise a flat flat string.
101 template<class T>
102 const nsTPromiseFlatString_CharT
103 TPromiseFlatString_CharT(const T& aString)
105 return nsTPromiseFlatString_CharT(aString);