Bug 1344629 - Part 8: Make nsTLiteralString inherit from nsTStringRepr. r=dbaron
authorDavid Major <dmajor@mozilla.com>
Tue, 14 Mar 2017 15:26:36 +1300
changeset 347459 c3873cde0276917f09ed5872aeeb712067a7a481
parent 347458 5504d47acea7095b93f9e89d5238b0b62e73a048
child 347460 a06239255301c9f6e1200d82d7bfe0459be37f6a
push id31496
push usercbook@mozilla.com
push dateTue, 14 Mar 2017 13:21:57 +0000
treeherdermozilla-central@9a26ed658fdc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdbaron
bugs1344629
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1344629 - Part 8: Make nsTLiteralString inherit from nsTStringRepr. r=dbaron This is the patch that all the others have been leading up to: by inheriting from nsTStringRepr, nsTLiteralString loses its destructor (previously suppplied by nsTSubstring). I included an AsString() cast as an escape hatch for a few pieces of code that couldn't easily accommodate the new hierarchy, but I'm pretty pleased with how little it was necessary. MozReview-Commit-ID: 2zGkaw1sMUp
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/HTMLStyleEditor.cpp
editor/libeditor/TextEditRules.cpp
xpcom/string/nsTLiteralString.h
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -3459,17 +3459,18 @@ nsresult
 HTMLEditRules::WillMakeDefListItem(Selection* aSelection,
                                    const nsAString *aItemType,
                                    bool aEntireList,
                                    bool* aCancel,
                                    bool* aHandled)
 {
   // for now we let WillMakeList handle this
   NS_NAMED_LITERAL_STRING(listType, "dl");
-  return WillMakeList(aSelection, &listType, aEntireList, nullptr, aCancel, aHandled, aItemType);
+  return WillMakeList(aSelection, &listType.AsString(), aEntireList, nullptr,
+                      aCancel, aHandled, aItemType);
 }
 
 nsresult
 HTMLEditRules::WillMakeBasicBlock(Selection& aSelection,
                                   const nsAString& aBlockType,
                                   bool* aCancel,
                                   bool* aHandled)
 {
--- a/editor/libeditor/HTMLStyleEditor.cpp
+++ b/editor/libeditor/HTMLStyleEditor.cpp
@@ -1628,18 +1628,18 @@ HTMLEditor::GetFontFaceState(bool* aMixe
   NS_ENSURE_TRUE(aMixed, NS_ERROR_FAILURE);
   *aMixed = true;
   outFace.Truncate();
 
   bool first, any, all;
 
   NS_NAMED_LITERAL_STRING(attr, "face");
   nsresult rv =
-    GetInlinePropertyBase(*nsGkAtoms::font, &attr, nullptr, &first, &any,
-                          &all, &outFace);
+    GetInlinePropertyBase(*nsGkAtoms::font, &attr.AsString(), nullptr, &first,
+                          &any, &all, &outFace);
   NS_ENSURE_SUCCESS(rv, rv);
   if (any && !all) {
     return NS_OK; // mixed
   }
   if (all) {
     *aMixed = false;
     return NS_OK;
   }
@@ -1671,18 +1671,18 @@ HTMLEditor::GetFontColorState(bool* aMix
   NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
   *aMixed = true;
   aOutColor.Truncate();
 
   NS_NAMED_LITERAL_STRING(colorStr, "color");
   bool first, any, all;
 
   nsresult rv =
-    GetInlinePropertyBase(*nsGkAtoms::font, &colorStr, nullptr, &first,
-                          &any, &all, &aOutColor);
+    GetInlinePropertyBase(*nsGkAtoms::font, &colorStr.AsString(), nullptr,
+                          &first, &any, &all, &aOutColor);
   NS_ENSURE_SUCCESS(rv, rv);
   if (any && !all) {
     return NS_OK; // mixed
   }
   if (all) {
     *aMixed = false;
     return NS_OK;
   }
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -380,18 +380,19 @@ TextEditRules::WillInsertBreak(Selection
   if (IsSingleLineEditor()) {
     *aCancel = true;
   } else {
     // handle docs with a max length
     // NOTE, this function copies inString into outString for us.
     NS_NAMED_LITERAL_STRING(inString, "\n");
     nsAutoString outString;
     bool didTruncate;
-    nsresult rv = TruncateInsertionIfNeeded(aSelection, &inString, &outString,
-                                            aMaxLength, &didTruncate);
+    nsresult rv = TruncateInsertionIfNeeded(aSelection, &inString.AsString(),
+                                            &outString, aMaxLength,
+                                            &didTruncate);
     NS_ENSURE_SUCCESS(rv, rv);
     if (didTruncate) {
       *aCancel = true;
       return NS_OK;
     }
 
     *aCancel = false;
 
--- a/xpcom/string/nsTLiteralString.h
+++ b/xpcom/string/nsTLiteralString.h
@@ -1,44 +1,59 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
-
 /**
  * nsTLiteralString_CharT
  *
  * Stores a null-terminated, immutable sequence of characters.
  *
- * Subclass of nsTString that restricts string value to a literal
- * character sequence.  This class does not own its data. The data is
- * assumed to be permanent. In practice this is true because this code
- * is only usable by and for libxul.
+ * nsTString-lookalike that restricts its string value to a literal character
+ * sequence. Can be implicitly cast to const nsTString& (the const is
+ * essential, since this class's data are not writable). The data are assumed
+ * to be static (permanent) and therefore, as an optimization, this class
+ * does not have a destructor.
  */
-class nsTLiteralString_CharT : public nsTString_CharT
+class nsTLiteralString_CharT : public mozilla::detail::nsTStringRepr_CharT
 {
 public:
 
   typedef nsTLiteralString_CharT self_type;
 
 public:
 
   /**
    * constructor
    */
 
   template<size_type N>
   explicit nsTLiteralString_CharT(const char_type (&aStr)[N])
-    : string_type(const_cast<char_type*>(aStr), N - 1, F_TERMINATED | F_LITERAL)
+    : base_string_type(const_cast<char_type*>(aStr), N - 1, F_TERMINATED | F_LITERAL)
   {
   }
 
   /**
+   * For compatibility with existing code that requires const ns[C]String*.
+   * Use sparingly. If possible, rewrite code to use const ns[C]String&
+   * and the implicit cast will just work.
+   */
+  const nsTString_CharT& AsString() const
+  {
+    return *reinterpret_cast<const nsTString_CharT*>(this);
+  }
+
+  operator const nsTString_CharT&() const
+  {
+    return AsString();
+  }
+
+  /**
    * Prohibit get() on temporaries as in nsLiteralCString("x").get().
    * These should be written as just "x", using a string literal directly.
    */
 #if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
   char16ptr_t get() const && = delete;
   char16ptr_t get() const &
 #else
   const char_type* get() const && = delete;
@@ -48,9 +63,15 @@ public:
     return mData;
   }
 
 private:
 
   // NOT TO BE IMPLEMENTED
   template<size_type N>
   nsTLiteralString_CharT(char_type (&aStr)[N]) = delete;
+
+  self_type& operator=(const self_type&) = delete;
 };
+
+static_assert(sizeof(nsTLiteralString_CharT) == sizeof(nsTString_CharT),
+              "nsTLiteralString_CharT can masquerade as nsTString_CharT, "
+              "so they must have identical layout");