Bug 1329568 - MediaMIMEType construction from literal string - r?jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Sun, 01 Jan 2017 09:52:06 +1100
changeset 460519 e91da7a55bd4b0b1416ead92cdb3d0c9bcd865da
parent 460518 110ce695f7057f217500b21080fdc27fbde9fbad
child 460520 d8bc3b5b84944fed849657878bc4d2a44262bb2e
push id41406
push usergsquelart@mozilla.com
push dateFri, 13 Jan 2017 06:23:59 +0000
reviewersjya
bugs1329568
milestone53.0a1
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);