Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | |
michael@0 | 8 | /** |
michael@0 | 9 | * NOTE: |
michael@0 | 10 | * |
michael@0 | 11 | * Try to avoid flat strings. |PromiseFlat[C]String| will help you as a last |
michael@0 | 12 | * resort, and this may be necessary when dealing with legacy or OS calls, |
michael@0 | 13 | * but in general, requiring a null-terminated array of characters kills many |
michael@0 | 14 | * of the performance wins the string classes offer. Write your own code to |
michael@0 | 15 | * use |nsA[C]String&|s for parameters. Write your string proccessing |
michael@0 | 16 | * algorithms to exploit iterators. If you do this, you will benefit from |
michael@0 | 17 | * being able to chain operations without copying or allocating and your code |
michael@0 | 18 | * will be significantly more efficient. Remember, a function that takes an |
michael@0 | 19 | * |const nsA[C]String&| can always be passed a raw character pointer by |
michael@0 | 20 | * wrapping it (for free) in a |nsDependent[C]String|. But a function that |
michael@0 | 21 | * takes a character pointer always has the potential to force allocation and |
michael@0 | 22 | * copying. |
michael@0 | 23 | * |
michael@0 | 24 | * |
michael@0 | 25 | * How to use it: |
michael@0 | 26 | * |
michael@0 | 27 | * A |nsPromiseFlat[C]String| doesn't necessarily own the characters it |
michael@0 | 28 | * promises. You must never use it to promise characters out of a string |
michael@0 | 29 | * with a shorter lifespan. The typical use will be something like this: |
michael@0 | 30 | * |
michael@0 | 31 | * SomeOSFunction( PromiseFlatCString(aCSubstring).get() ); // GOOD |
michael@0 | 32 | * |
michael@0 | 33 | * Here's a BAD use: |
michael@0 | 34 | * |
michael@0 | 35 | * const char* buffer = PromiseFlatCString(aCSubstring).get(); |
michael@0 | 36 | * SomeOSFunction(buffer); // BAD!! |buffer| is a dangling pointer |
michael@0 | 37 | * |
michael@0 | 38 | * The only way to make one is with the function |PromiseFlat[C]String|, |
michael@0 | 39 | * which produce a |const| instance. ``What if I need to keep a promise |
michael@0 | 40 | * around for a little while?'' you might ask. In that case, you can keep a |
michael@0 | 41 | * reference, like so |
michael@0 | 42 | * |
michael@0 | 43 | * const nsCString& flat = PromiseFlatString(aCSubstring); |
michael@0 | 44 | * // this reference holds the anonymous temporary alive, but remember, |
michael@0 | 45 | * // it must _still_ have a lifetime shorter than that of |aCSubstring| |
michael@0 | 46 | * |
michael@0 | 47 | * SomeOSFunction(flat.get()); |
michael@0 | 48 | * SomeOtherOSFunction(flat.get()); |
michael@0 | 49 | * |
michael@0 | 50 | * |
michael@0 | 51 | * How does it work? |
michael@0 | 52 | * |
michael@0 | 53 | * A |nsPromiseFlat[C]String| is just a wrapper for another string. If you |
michael@0 | 54 | * apply it to a string that happens to be flat, your promise is just a |
michael@0 | 55 | * dependent reference to the string's data. If you apply it to a non-flat |
michael@0 | 56 | * string, then a temporary flat string is created for you, by allocating and |
michael@0 | 57 | * copying. In the event that you end up assigning the result into a sharing |
michael@0 | 58 | * string (e.g., |nsTString|), the right thing happens. |
michael@0 | 59 | */ |
michael@0 | 60 | |
michael@0 | 61 | class nsTPromiseFlatString_CharT : public nsTString_CharT |
michael@0 | 62 | { |
michael@0 | 63 | public: |
michael@0 | 64 | |
michael@0 | 65 | typedef nsTPromiseFlatString_CharT self_type; |
michael@0 | 66 | |
michael@0 | 67 | private: |
michael@0 | 68 | |
michael@0 | 69 | void Init( const substring_type& ); |
michael@0 | 70 | |
michael@0 | 71 | // NOT TO BE IMPLEMENTED |
michael@0 | 72 | void operator=( const self_type& ) MOZ_DELETE; |
michael@0 | 73 | |
michael@0 | 74 | // NOT TO BE IMPLEMENTED |
michael@0 | 75 | nsTPromiseFlatString_CharT() MOZ_DELETE; |
michael@0 | 76 | |
michael@0 | 77 | // NOT TO BE IMPLEMENTED |
michael@0 | 78 | nsTPromiseFlatString_CharT( const string_type& str ) MOZ_DELETE; |
michael@0 | 79 | |
michael@0 | 80 | public: |
michael@0 | 81 | |
michael@0 | 82 | explicit |
michael@0 | 83 | nsTPromiseFlatString_CharT( const substring_type& str ) |
michael@0 | 84 | : string_type() |
michael@0 | 85 | { |
michael@0 | 86 | Init(str); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | explicit |
michael@0 | 90 | nsTPromiseFlatString_CharT( const substring_tuple_type& tuple ) |
michael@0 | 91 | : string_type() |
michael@0 | 92 | { |
michael@0 | 93 | // nothing else to do here except assign the value of the tuple |
michael@0 | 94 | // into ourselves. |
michael@0 | 95 | Assign(tuple); |
michael@0 | 96 | } |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | // We template this so that the constructor is chosen based on the type of the |
michael@0 | 100 | // parameter. This allows us to reject attempts to promise a flat flat string. |
michael@0 | 101 | template<class T> |
michael@0 | 102 | const nsTPromiseFlatString_CharT |
michael@0 | 103 | TPromiseFlatString_CharT( const T& string ) |
michael@0 | 104 | { |
michael@0 | 105 | return nsTPromiseFlatString_CharT(string); |
michael@0 | 106 | } |