|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
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 /** |
|
9 * NOTE: |
|
10 * |
|
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. |
|
23 * |
|
24 * |
|
25 * How to use it: |
|
26 * |
|
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: |
|
30 * |
|
31 * SomeOSFunction( PromiseFlatCString(aCSubstring).get() ); // GOOD |
|
32 * |
|
33 * Here's a BAD use: |
|
34 * |
|
35 * const char* buffer = PromiseFlatCString(aCSubstring).get(); |
|
36 * SomeOSFunction(buffer); // BAD!! |buffer| is a dangling pointer |
|
37 * |
|
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 |
|
42 * |
|
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| |
|
46 * |
|
47 * SomeOSFunction(flat.get()); |
|
48 * SomeOtherOSFunction(flat.get()); |
|
49 * |
|
50 * |
|
51 * How does it work? |
|
52 * |
|
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. |
|
59 */ |
|
60 |
|
61 class nsTPromiseFlatString_CharT : public nsTString_CharT |
|
62 { |
|
63 public: |
|
64 |
|
65 typedef nsTPromiseFlatString_CharT self_type; |
|
66 |
|
67 private: |
|
68 |
|
69 void Init( const substring_type& ); |
|
70 |
|
71 // NOT TO BE IMPLEMENTED |
|
72 void operator=( const self_type& ) MOZ_DELETE; |
|
73 |
|
74 // NOT TO BE IMPLEMENTED |
|
75 nsTPromiseFlatString_CharT() MOZ_DELETE; |
|
76 |
|
77 // NOT TO BE IMPLEMENTED |
|
78 nsTPromiseFlatString_CharT( const string_type& str ) MOZ_DELETE; |
|
79 |
|
80 public: |
|
81 |
|
82 explicit |
|
83 nsTPromiseFlatString_CharT( const substring_type& str ) |
|
84 : string_type() |
|
85 { |
|
86 Init(str); |
|
87 } |
|
88 |
|
89 explicit |
|
90 nsTPromiseFlatString_CharT( const substring_tuple_type& tuple ) |
|
91 : string_type() |
|
92 { |
|
93 // nothing else to do here except assign the value of the tuple |
|
94 // into ourselves. |
|
95 Assign(tuple); |
|
96 } |
|
97 }; |
|
98 |
|
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& string ) |
|
104 { |
|
105 return nsTPromiseFlatString_CharT(string); |
|
106 } |