xpcom/glue/nsVersionComparator.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/nsVersionComparator.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,149 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef nsVersionComparator_h__
     1.9 +#define nsVersionComparator_h__
    1.10 +
    1.11 +#include "nscore.h"
    1.12 +#include <stdlib.h>
    1.13 +#include <string.h>
    1.14 +#include <assert.h>
    1.15 +#if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
    1.16 +#include <wchar.h>
    1.17 +#include "nsStringGlue.h"
    1.18 +#endif
    1.19 +
    1.20 +/**
    1.21 + * In order to compare version numbers in Mozilla, you need to use the
    1.22 + * mozilla::Version class.  You can construct an object of this type by passing
    1.23 + * in a string version number to the constructor.  Objects of this type can be
    1.24 + * compared using the standard comparison operators.
    1.25 + *
    1.26 + * For example, let's say that you want to make sure that a given version
    1.27 + * number is not older than 15.a2.  Here's how you would write a function to
    1.28 + * do that.
    1.29 + *
    1.30 + * bool IsVersionValid(const char* version) {
    1.31 + *   return mozilla::Version("15.a2") <= mozilla::Version(version);
    1.32 + * }
    1.33 + *
    1.34 + * Or, since Version's constructor is implicit, you can simplify this code:
    1.35 + *
    1.36 + * bool IsVersionValid(const char* version) {
    1.37 + *   return mozilla::Version("15.a2") <= version;
    1.38 + * }
    1.39 + *
    1.40 + * On Windows, if your version strings are wide characters, you should use the
    1.41 + * mozilla::VersionW variant instead.  The semantics of that class is the same
    1.42 + * as Version.
    1.43 + */
    1.44 +
    1.45 +namespace mozilla {
    1.46 +
    1.47 +int32_t NS_COM_GLUE
    1.48 +CompareVersions(const char *A, const char *B);
    1.49 +
    1.50 +#ifdef XP_WIN
    1.51 +int32_t NS_COM_GLUE
    1.52 +CompareVersions(const char16_t *A, const char16_t *B);
    1.53 +#endif
    1.54 +
    1.55 +struct NS_COM_GLUE Version
    1.56 +{
    1.57 +  Version(const char* versionString)
    1.58 +  {
    1.59 +    versionContent = strdup(versionString);
    1.60 +  }
    1.61 +
    1.62 +  const char* ReadContent() const
    1.63 +  {
    1.64 +    return versionContent;
    1.65 +  }
    1.66 +
    1.67 +  ~Version()
    1.68 +  {
    1.69 +    free(versionContent);
    1.70 +  }
    1.71 +
    1.72 +  bool operator< (const Version& rhs) const
    1.73 +  {
    1.74 +    return CompareVersions(versionContent, rhs.ReadContent()) == -1;
    1.75 +  }
    1.76 +  bool operator<= (const Version& rhs) const
    1.77 +  {
    1.78 +    return CompareVersions(versionContent, rhs.ReadContent()) < 1;
    1.79 +  }
    1.80 +  bool operator> (const Version& rhs) const
    1.81 +  {
    1.82 +    return CompareVersions(versionContent, rhs.ReadContent()) == 1;
    1.83 +  }
    1.84 +  bool operator>= (const Version& rhs) const
    1.85 +  {
    1.86 +    return CompareVersions(versionContent, rhs.ReadContent()) > -1;
    1.87 +  }
    1.88 +  bool operator== (const Version& rhs) const
    1.89 +  {
    1.90 +    return CompareVersions(versionContent, rhs.ReadContent()) == 0;
    1.91 +  }
    1.92 +  bool operator!= (const Version& rhs) const
    1.93 +  {
    1.94 +    return CompareVersions(versionContent, rhs.ReadContent()) != 0;
    1.95 +  }
    1.96 +
    1.97 + private:
    1.98 +  char* versionContent;
    1.99 +};
   1.100 +
   1.101 +#ifdef XP_WIN
   1.102 +struct NS_COM_GLUE VersionW
   1.103 +{
   1.104 +  VersionW(const char16_t *versionStringW)
   1.105 +  {
   1.106 +    versionContentW = reinterpret_cast<char16_t*>(wcsdup(char16ptr_t(versionStringW)));
   1.107 +  }
   1.108 +
   1.109 +  const char16_t* ReadContentW() const
   1.110 +  {
   1.111 +    return versionContentW;
   1.112 +  }
   1.113 +
   1.114 +  ~VersionW()
   1.115 +  {
   1.116 +    free(versionContentW);
   1.117 +  }
   1.118 +
   1.119 +  bool operator< (const VersionW& rhs) const
   1.120 +  {
   1.121 +    return CompareVersions(versionContentW, rhs.ReadContentW()) == -1;
   1.122 +  }
   1.123 +  bool operator<= (const VersionW& rhs) const
   1.124 +  {
   1.125 +    return CompareVersions(versionContentW, rhs.ReadContentW()) < 1;
   1.126 +  }
   1.127 +  bool operator> (const VersionW& rhs) const
   1.128 +  {
   1.129 +    return CompareVersions(versionContentW, rhs.ReadContentW()) == 1;
   1.130 +  }
   1.131 +  bool operator>= (const VersionW& rhs) const
   1.132 +  {
   1.133 +    return CompareVersions(versionContentW, rhs.ReadContentW()) > -1;
   1.134 +  }
   1.135 +  bool operator== (const VersionW& rhs) const
   1.136 +  {
   1.137 +    return CompareVersions(versionContentW, rhs.ReadContentW()) == 0;
   1.138 +  }
   1.139 +  bool operator!= (const VersionW& rhs) const
   1.140 +  {
   1.141 +    return CompareVersions(versionContentW, rhs.ReadContentW()) != 0;
   1.142 +  }
   1.143 +
   1.144 + private:
   1.145 +  char16_t* versionContentW;
   1.146 +};
   1.147 +#endif
   1.148 +
   1.149 +} // namespace mozilla
   1.150 +
   1.151 +#endif // nsVersionComparator_h__
   1.152 +

mercurial