michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * NOTE: michael@0: * michael@0: * Try to avoid flat strings. |PromiseFlat[C]String| will help you as a last michael@0: * resort, and this may be necessary when dealing with legacy or OS calls, michael@0: * but in general, requiring a null-terminated array of characters kills many michael@0: * of the performance wins the string classes offer. Write your own code to michael@0: * use |nsA[C]String&|s for parameters. Write your string proccessing michael@0: * algorithms to exploit iterators. If you do this, you will benefit from michael@0: * being able to chain operations without copying or allocating and your code michael@0: * will be significantly more efficient. Remember, a function that takes an michael@0: * |const nsA[C]String&| can always be passed a raw character pointer by michael@0: * wrapping it (for free) in a |nsDependent[C]String|. But a function that michael@0: * takes a character pointer always has the potential to force allocation and michael@0: * copying. michael@0: * michael@0: * michael@0: * How to use it: michael@0: * michael@0: * A |nsPromiseFlat[C]String| doesn't necessarily own the characters it michael@0: * promises. You must never use it to promise characters out of a string michael@0: * with a shorter lifespan. The typical use will be something like this: michael@0: * michael@0: * SomeOSFunction( PromiseFlatCString(aCSubstring).get() ); // GOOD michael@0: * michael@0: * Here's a BAD use: michael@0: * michael@0: * const char* buffer = PromiseFlatCString(aCSubstring).get(); michael@0: * SomeOSFunction(buffer); // BAD!! |buffer| is a dangling pointer michael@0: * michael@0: * The only way to make one is with the function |PromiseFlat[C]String|, michael@0: * which produce a |const| instance. ``What if I need to keep a promise michael@0: * around for a little while?'' you might ask. In that case, you can keep a michael@0: * reference, like so michael@0: * michael@0: * const nsCString& flat = PromiseFlatString(aCSubstring); michael@0: * // this reference holds the anonymous temporary alive, but remember, michael@0: * // it must _still_ have a lifetime shorter than that of |aCSubstring| michael@0: * michael@0: * SomeOSFunction(flat.get()); michael@0: * SomeOtherOSFunction(flat.get()); michael@0: * michael@0: * michael@0: * How does it work? michael@0: * michael@0: * A |nsPromiseFlat[C]String| is just a wrapper for another string. If you michael@0: * apply it to a string that happens to be flat, your promise is just a michael@0: * dependent reference to the string's data. If you apply it to a non-flat michael@0: * string, then a temporary flat string is created for you, by allocating and michael@0: * copying. In the event that you end up assigning the result into a sharing michael@0: * string (e.g., |nsTString|), the right thing happens. michael@0: */ michael@0: michael@0: class nsTPromiseFlatString_CharT : public nsTString_CharT michael@0: { michael@0: public: michael@0: michael@0: typedef nsTPromiseFlatString_CharT self_type; michael@0: michael@0: private: michael@0: michael@0: void Init( const substring_type& ); michael@0: michael@0: // NOT TO BE IMPLEMENTED michael@0: void operator=( const self_type& ) MOZ_DELETE; michael@0: michael@0: // NOT TO BE IMPLEMENTED michael@0: nsTPromiseFlatString_CharT() MOZ_DELETE; michael@0: michael@0: // NOT TO BE IMPLEMENTED michael@0: nsTPromiseFlatString_CharT( const string_type& str ) MOZ_DELETE; michael@0: michael@0: public: michael@0: michael@0: explicit michael@0: nsTPromiseFlatString_CharT( const substring_type& str ) michael@0: : string_type() michael@0: { michael@0: Init(str); michael@0: } michael@0: michael@0: explicit michael@0: nsTPromiseFlatString_CharT( const substring_tuple_type& tuple ) michael@0: : string_type() michael@0: { michael@0: // nothing else to do here except assign the value of the tuple michael@0: // into ourselves. michael@0: Assign(tuple); michael@0: } michael@0: }; michael@0: michael@0: // We template this so that the constructor is chosen based on the type of the michael@0: // parameter. This allows us to reject attempts to promise a flat flat string. michael@0: template michael@0: const nsTPromiseFlatString_CharT michael@0: TPromiseFlatString_CharT( const T& string ) michael@0: { michael@0: return nsTPromiseFlatString_CharT(string); michael@0: }