Bug 1329568 - MediaMIMEType comparisons against others - r?jya
`==` and `!=` against other MediaMIMEType objects, and against MEDIAMIMETYPE
checked literals.
This will allow simple (and compile-time-checked!) tests like:
if (contentType.Type() == MEDIAMIMETYPE("audio/mp4")) { ...
MozReview-Commit-ID: 5yMua5krOKD
--- a/dom/media/MediaMIMETypes.h
+++ b/dom/media/MediaMIMETypes.h
@@ -10,17 +10,17 @@
#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.
+// MEDIAMIMETYPE macro, or to compare a MediaMIMEType to a literal.
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)
@@ -51,16 +51,36 @@ public:
// 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; }
+ // Comparison with DependentMediaMIMEType.
+ // Useful to compare to MEDIAMIMETYPE literals.
+ bool operator==(const DependentMediaMIMEType& aOther) const
+ {
+ return mMIMEType.Equals(aOther.AsDependentString());
+ }
+ bool operator!=(const DependentMediaMIMEType& aOther) const
+ {
+ return !mMIMEType.Equals(aOther.AsDependentString());
+ }
+
+ bool operator==(const MediaMIMEType& aOther) const
+ {
+ return mMIMEType.Equals(aOther.mMIMEType);
+ }
+ bool operator!=(const MediaMIMEType& aOther) const
+ {
+ return !mMIMEType.Equals(aOther.mMIMEType);
+ }
+
private:
friend Maybe<MediaMIMEType> MakeMediaMIMEType(const nsAString& aType);
friend class MediaExtendedMIMEType;
explicit MediaMIMEType(const nsACString& aType);
nsCString mMIMEType; // UTF8 MIME "type/subtype".
};