Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_VERSION_H_ |
| 6 | #define BASE_VERSION_H_ |
| 7 | |
wfh | bf68f4d5 | 2015-03-10 01:32:59 | [diff] [blame] | 8 | #include <stdint.h> |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 9 | |
| 10 | #include <iosfwd> |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 14 | #include "base/base_export.h" |
Greg Thompson | 15a9d17 | 2019-08-06 21:13:55 | [diff] [blame] | 15 | #include "base/strings/string_piece.h" |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 16 | |
xhwang@chromium.org | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 17 | namespace base { |
| 18 | |
evan@chromium.org | 36ee032 | 2010-12-23 21:27:12 | [diff] [blame] | 19 | // Version represents a dotted version number, like "1.2.3.4", supporting |
| 20 | // parsing and comparison. |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 21 | class BASE_EXPORT Version { |
erg@google.com | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 22 | public: |
cpu@chromium.org | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 23 | // The only thing you can legally do to a default constructed |
| 24 | // Version object is assign to it. |
akalin@chromium.org | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 25 | Version(); |
| 26 | |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 27 | Version(const Version& other); |
| 28 | |
cpu@chromium.org | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 29 | // Initializes from a decimal dotted version number, like "0.1.1". |
Christian Flach | bf5fdc4 | 2023-10-24 02:30:18 | [diff] [blame] | 30 | // Each component is limited to a uint32_t. Call IsValid() to learn |
cpu@chromium.org | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 31 | // the outcome. |
Greg Thompson | 15a9d17 | 2019-08-06 21:13:55 | [diff] [blame] | 32 | explicit Version(StringPiece version_str); |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 33 | |
staraz | 8fb3808 | 2016-07-25 18:48:21 | [diff] [blame] | 34 | // Initializes from a vector of components, like {1, 2, 3, 4}. Call IsValid() |
| 35 | // to learn the outcome. |
| 36 | explicit Version(std::vector<uint32_t> components); |
| 37 | |
| 38 | ~Version(); |
| 39 | |
cpu@chromium.org | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 40 | // Returns true if the object contains a valid version number. |
| 41 | bool IsValid() const; |
| 42 | |
mathp@google.com | 810b250 | 2012-07-04 16:22:48 | [diff] [blame] | 43 | // Returns true if the version wildcard string is valid. The version wildcard |
| 44 | // string may end with ".*" (e.g. 1.2.*, 1.*). Any other arrangement with "*" |
| 45 | // is invalid (e.g. 1.*.3 or 1.2.3*). This functions defaults to standard |
| 46 | // Version behavior (IsValid) if no wildcard is present. |
Greg Thompson | 15a9d17 | 2019-08-06 21:13:55 | [diff] [blame] | 47 | static bool IsValidWildcardString(StringPiece wildcard_string); |
mathp@google.com | 810b250 | 2012-07-04 16:22:48 | [diff] [blame] | 48 | |
Chris Fredrickson | 075b495 | 2022-11-21 15:57:30 | [diff] [blame] | 49 | // Returns -1, 0, 1 for <, ==, >. `this` and `other` must both be valid. |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 50 | int CompareTo(const Version& other) const; |
| 51 | |
mathp@google.com | 810b250 | 2012-07-04 16:22:48 | [diff] [blame] | 52 | // Given a valid version object, compare if a |wildcard_string| results in a |
| 53 | // newer version. This function will default to CompareTo if the string does |
| 54 | // not end in wildcard sequence ".*". IsValidWildcard(wildcard_string) must be |
| 55 | // true before using this function. |
Greg Thompson | 15a9d17 | 2019-08-06 21:13:55 | [diff] [blame] | 56 | int CompareToWildcardString(StringPiece wildcard_string) const; |
mathp@google.com | 810b250 | 2012-07-04 16:22:48 | [diff] [blame] | 57 | |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 58 | // Return the string representation of this version. |
Devlin Cronin | fde745d | 2019-09-25 22:21:57 | [diff] [blame] | 59 | std::string GetString() const; |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 60 | |
wfh | bf68f4d5 | 2015-03-10 01:32:59 | [diff] [blame] | 61 | const std::vector<uint32_t>& components() const { return components_; } |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 62 | |
erg@google.com | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 63 | private: |
wfh | bf68f4d5 | 2015-03-10 01:32:59 | [diff] [blame] | 64 | std::vector<uint32_t> components_; |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 65 | }; |
| 66 | |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 67 | BASE_EXPORT bool operator==(const Version& v1, const Version& v2); |
| 68 | BASE_EXPORT bool operator!=(const Version& v1, const Version& v2); |
| 69 | BASE_EXPORT bool operator<(const Version& v1, const Version& v2); |
| 70 | BASE_EXPORT bool operator<=(const Version& v1, const Version& v2); |
| 71 | BASE_EXPORT bool operator>(const Version& v1, const Version& v2); |
| 72 | BASE_EXPORT bool operator>=(const Version& v1, const Version& v2); |
| 73 | BASE_EXPORT std::ostream& operator<<(std::ostream& stream, const Version& v); |
| 74 | |
xhwang@chromium.org | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 75 | } // namespace base |
| 76 | |
erikkay@google.com | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 77 | #endif // BASE_VERSION_H_ |