1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsVersionComparator_h__
8 #define nsVersionComparator_h__
13 #if defined(_WIN32) && !defined(UPDATER_NO_STRING_GLUE_STL)
20 * In order to compare version numbers in Mozilla, you need to use the
21 * mozilla::Version class. You can construct an object of this type by passing
22 * in a string version number to the constructor. Objects of this type can be
23 * compared using the standard comparison operators.
25 * For example, let's say that you want to make sure that a given version
26 * number is not older than 15.a2. Here's how you would write a function to
29 * bool IsVersionValid(const char* version) {
30 * return mozilla::Version("15.a2") <= mozilla::Version(version);
33 * Or, since Version's constructor is implicit, you can simplify this code:
35 * bool IsVersionValid(const char* version) {
36 * return mozilla::Version("15.a2") <= version;
39 * On Windows, if your version strings are wide characters, you should use the
40 * mozilla::VersionW variant instead. The semantics of that class is the same
46 int32_t CompareVersions(const char* aStrA
, const char* aStrB
);
49 int32_t CompareVersions(const wchar_t* aStrA
, const wchar_t* aStrB
);
54 explicit Version(const char* aVersionString
)
56 versionContent
= strdup(aVersionString
);
59 const char* ReadContent() const
61 return versionContent
;
69 bool operator<(const Version
& aRhs
) const
71 return CompareVersions(versionContent
, aRhs
.ReadContent()) == -1;
73 bool operator<=(const Version
& aRhs
) const
75 return CompareVersions(versionContent
, aRhs
.ReadContent()) < 1;
77 bool operator>(const Version
& aRhs
) const
79 return CompareVersions(versionContent
, aRhs
.ReadContent()) == 1;
81 bool operator>=(const Version
& aRhs
) const
83 return CompareVersions(versionContent
, aRhs
.ReadContent()) > -1;
85 bool operator==(const Version
& aRhs
) const
87 return CompareVersions(versionContent
, aRhs
.ReadContent()) == 0;
89 bool operator!=(const Version
& aRhs
) const
91 return CompareVersions(versionContent
, aRhs
.ReadContent()) != 0;
93 bool operator<(const char* aRhs
) const
95 return CompareVersions(versionContent
, aRhs
) == -1;
97 bool operator<=(const char* aRhs
) const
99 return CompareVersions(versionContent
, aRhs
) < 1;
101 bool operator>(const char* aRhs
) const
103 return CompareVersions(versionContent
, aRhs
) == 1;
105 bool operator>=(const char* aRhs
) const
107 return CompareVersions(versionContent
, aRhs
) > -1;
109 bool operator==(const char* aRhs
) const
111 return CompareVersions(versionContent
, aRhs
) == 0;
113 bool operator!=(const char* aRhs
) const
115 return CompareVersions(versionContent
, aRhs
) != 0;
119 char* versionContent
;
125 explicit VersionW(const wchar_t* aVersionStringW
)
128 reinterpret_cast<wchar_t*>(wcsdup(aVersionStringW
));
131 const wchar_t* ReadContentW() const
133 return versionContentW
;
138 free(versionContentW
);
141 bool operator<(const VersionW
& aRhs
) const
143 return CompareVersions(versionContentW
, aRhs
.ReadContentW()) == -1;
145 bool operator<=(const VersionW
& aRhs
) const
147 return CompareVersions(versionContentW
, aRhs
.ReadContentW()) < 1;
149 bool operator>(const VersionW
& aRhs
) const
151 return CompareVersions(versionContentW
, aRhs
.ReadContentW()) == 1;
153 bool operator>=(const VersionW
& aRhs
) const
155 return CompareVersions(versionContentW
, aRhs
.ReadContentW()) > -1;
157 bool operator==(const VersionW
& aRhs
) const
159 return CompareVersions(versionContentW
, aRhs
.ReadContentW()) == 0;
161 bool operator!=(const VersionW
& aRhs
) const
163 return CompareVersions(versionContentW
, aRhs
.ReadContentW()) != 0;
167 wchar_t* versionContentW
;
171 } // namespace mozilla
173 #endif // nsVersionComparator_h__