Bug 1329568 - MediaMIMEType construction from literal string - r=jya
☠☠ backed out by 4caa61eee820 ☠ ☠
authorGerald Squelart <gsquelart@mozilla.com>
Sun, 01 Jan 2017 09:52:06 +1100
changeset 374176 662529436cc4dad8be244c0bfe08c0371bbc189e
parent 374175 7c62078beb88dce045784041fe78b1265c1545dc
child 374177 453851b40e3e8982598a179e3a598531571b935e
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1329568
milestone53.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 1329568 - MediaMIMEType construction from literal string - r=jya MediaMIMEType object can now be constructed from string literals by using e.g.: MEDIAMIMETYPE("audio/mp4") -- Note that it's an all-caps macro. The string will be checked for validity at compile time. To help with this, a new class DependentMediaMIMEType can point inside another string (usually a string literal), but can only be constructed for valid strings -- It will fail to compile when using MEDIAMIMETYPE, or it would assert at runtime if directly built. MozReview-Commit-ID: 5T3AKfpGbO4
dom/media/MediaMIMETypes.h
--- a/dom/media/MediaMIMETypes.h
+++ b/dom/media/MediaMIMETypes.h
@@ -7,20 +7,57 @@
 #ifndef MediaMIMETypes_h_
 #define MediaMIMETypes_h_
 
 #include "mozilla/Maybe.h"
 #include "nsString.h"
 
 namespace mozilla {
 
+// Class containing pointing at a media MIME "type/subtype" string literal.
+// See IsMediaMIMEType for restrictions.
+// Mainly used to help construct a MediaMIMEType through the statically-checked
+// MEDIAMIMETYPE macro.
+class DependentMediaMIMEType
+{
+public:
+  // Construction from a literal. Checked in debug builds.
+  // Use MEDIAMIMETYPE macro instead, for static checking.
+  template <size_t N>
+  explicit DependentMediaMIMEType(const char (&aType)[N])
+    : mMIMEType(aType, N - 1)
+  {
+    MOZ_ASSERT(IsMediaMIMEType(aType, N - 1), "Invalid media MIME type");
+  }
+
+  // MIME "type/subtype".
+  const nsDependentCString& AsDependentString() const { return mMIMEType; }
+
+private:
+  nsDependentCString mMIMEType;
+};
+
+// Instantiate a DependentMediaMIMEType from a literal. Statically checked.
+#define MEDIAMIMETYPE(LIT)                                            \
+  static_cast<const DependentMediaMIMEType&>(                         \
+    []() {                                                            \
+      static_assert(IsMediaMIMEType(LIT), "Invalid media MIME type"); \
+      return DependentMediaMIMEType(LIT);                             \
+    }())
+
 // Class containing only pre-parsed lowercase media MIME type/subtype.
 class MediaMIMEType
 {
 public:
+  // Construction from a DependentMediaMIMEType, with its inherent checks.
+  // Implicit so MEDIAMIMETYPE can be used wherever a MediaMIMEType is expected.
+  MOZ_IMPLICIT MediaMIMEType(const DependentMediaMIMEType& aType)
+    : mMIMEType(aType.AsDependentString())
+  {}
+
   // MIME "type/subtype", always lowercase.
   const nsACString& AsString() const { return mMIMEType; }
 
 private:
   friend Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType);
   friend class MediaExtendedMIMEType;
   explicit MediaMIMEType(const nsACString& aType);