Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 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 | bf68f4d5b | 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> |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 11 | #include <string> |
Helmut Januschka | 6bfb01f5 | 2024-05-28 15:47:45 | [diff] [blame] | 12 | #include <string_view> |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 15 | #include "base/base_export.h" |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 16 | |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 17 | namespace base { |
| 18 | |
[email protected] | 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. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 21 | class BASE_EXPORT Version { |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 22 | public: |
[email protected] | 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. |
[email protected] | 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); |
David Bertoni | d8a8c00 | 2024-07-18 19:30:46 | [diff] [blame] | 28 | Version(Version&& other); |
| 29 | |
| 30 | Version& operator=(const Version& other) = default; |
| 31 | Version& operator=(Version&& other) = default; |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 32 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 33 | // Initializes from a decimal dotted version number, like "0.1.1". |
Christian Flach | bf5fdc4 | 2023-10-24 02:30:18 | [diff] [blame] | 34 | // Each component is limited to a uint32_t. Call IsValid() to learn |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 35 | // the outcome. |
Helmut Januschka | 6bfb01f5 | 2024-05-28 15:47:45 | [diff] [blame] | 36 | explicit Version(std::string_view version_str); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 37 | |
staraz | 8fb3808 | 2016-07-25 18:48:21 | [diff] [blame] | 38 | // Initializes from a vector of components, like {1, 2, 3, 4}. Call IsValid() |
| 39 | // to learn the outcome. |
| 40 | explicit Version(std::vector<uint32_t> components); |
| 41 | |
| 42 | ~Version(); |
| 43 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 44 | // Returns true if the object contains a valid version number. |
| 45 | bool IsValid() const; |
| 46 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 47 | // Returns true if the version wildcard string is valid. The version wildcard |
| 48 | // string may end with ".*" (e.g. 1.2.*, 1.*). Any other arrangement with "*" |
| 49 | // is invalid (e.g. 1.*.3 or 1.2.3*). This functions defaults to standard |
| 50 | // Version behavior (IsValid) if no wildcard is present. |
Helmut Januschka | 6bfb01f5 | 2024-05-28 15:47:45 | [diff] [blame] | 51 | static bool IsValidWildcardString(std::string_view wildcard_string); |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 52 | |
Chris Fredrickson | 075b495 | 2022-11-21 15:57:30 | [diff] [blame] | 53 | // Returns -1, 0, 1 for <, ==, >. `this` and `other` must both be valid. |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 54 | int CompareTo(const Version& other) const; |
| 55 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 56 | // Given a valid version object, compare if a |wildcard_string| results in a |
| 57 | // newer version. This function will default to CompareTo if the string does |
| 58 | // not end in wildcard sequence ".*". IsValidWildcard(wildcard_string) must be |
| 59 | // true before using this function. |
Helmut Januschka | 6bfb01f5 | 2024-05-28 15:47:45 | [diff] [blame] | 60 | int CompareToWildcardString(std::string_view wildcard_string) const; |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 61 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 62 | // Return the string representation of this version. |
Devlin Cronin | fde745d | 2019-09-25 22:21:57 | [diff] [blame] | 63 | std::string GetString() const; |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 64 | |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 65 | const std::vector<uint32_t>& components() const { return components_; } |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 66 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 67 | private: |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 68 | std::vector<uint32_t> components_; |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 69 | }; |
| 70 | |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 71 | BASE_EXPORT bool operator==(const Version& v1, const Version& v2); |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 72 | BASE_EXPORT bool operator<(const Version& v1, const Version& v2); |
| 73 | BASE_EXPORT bool operator<=(const Version& v1, const Version& v2); |
| 74 | BASE_EXPORT bool operator>(const Version& v1, const Version& v2); |
| 75 | BASE_EXPORT bool operator>=(const Version& v1, const Version& v2); |
| 76 | BASE_EXPORT std::ostream& operator<<(std::ostream& stream, const Version& v); |
| 77 | |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 78 | } // namespace base |
| 79 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 80 | #endif // BASE_VERSION_H_ |