[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[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> |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 12 | #include "base/base_export.h" |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 13 | |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 14 | namespace base { |
| 15 | |
[email protected] | 36ee032 | 2010-12-23 21:27:12 | [diff] [blame] | 16 | // Version represents a dotted version number, like "1.2.3.4", supporting |
| 17 | // parsing and comparison. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 18 | class BASE_EXPORT Version { |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 19 | public: |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 20 | // The only thing you can legally do to a default constructed |
| 21 | // Version object is assign to it. |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 22 | Version(); |
| 23 | |
[email protected] | 1513bf8 | 2011-06-07 17:43:20 | [diff] [blame] | 24 | ~Version(); |
| 25 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 26 | // Initializes from a decimal dotted version number, like "0.1.1". |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame^] | 27 | // Each component is limited to a uint16_t. Call IsValid() to learn |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 28 | // the outcome. |
| 29 | explicit Version(const std::string& version_str); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 30 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 31 | // Returns true if the object contains a valid version number. |
| 32 | bool IsValid() const; |
| 33 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 34 | // Returns true if the version wildcard string is valid. The version wildcard |
| 35 | // string may end with ".*" (e.g. 1.2.*, 1.*). Any other arrangement with "*" |
| 36 | // is invalid (e.g. 1.*.3 or 1.2.3*). This functions defaults to standard |
| 37 | // Version behavior (IsValid) if no wildcard is present. |
| 38 | static bool IsValidWildcardString(const std::string& wildcard_string); |
| 39 | |
[email protected] | 30c157c | 2011-08-01 17:45:08 | [diff] [blame] | 40 | // Commonly used pattern. Given a valid version object, compare if a |
| 41 | // |version_str| results in a newer version. Returns true if the |
| 42 | // string represents valid version and if the version is greater than |
| 43 | // than the version of this object. |
| 44 | bool IsOlderThan(const std::string& version_str) const; |
| 45 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 46 | bool Equals(const Version& other) const; |
| 47 | |
| 48 | // Returns -1, 0, 1 for <, ==, >. |
| 49 | int CompareTo(const Version& other) const; |
| 50 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame] | 51 | // Given a valid version object, compare if a |wildcard_string| results in a |
| 52 | // newer version. This function will default to CompareTo if the string does |
| 53 | // not end in wildcard sequence ".*". IsValidWildcard(wildcard_string) must be |
| 54 | // true before using this function. |
| 55 | int CompareToWildcardString(const std::string& wildcard_string) const; |
| 56 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 57 | // Return the string representation of this version. |
| 58 | const std::string GetString() const; |
| 59 | |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 60 | const std::vector<uint32_t>& components() const { return components_; } |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 61 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 62 | private: |
wfh | bf68f4d5b | 2015-03-10 01:32:59 | [diff] [blame] | 63 | std::vector<uint32_t> components_; |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 64 | }; |
| 65 | |
[email protected] | 1f04ef4 | 2013-04-22 07:35:50 | [diff] [blame] | 66 | } // namespace base |
| 67 | |
thakis | 37be69c | 2015-08-19 03:26:57 | [diff] [blame] | 68 | // TODO(xhwang) remove this when all users are updated to explicitly use the |
| 69 | // namespace |
| 70 | using base::Version; |
| 71 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 72 | #endif // BASE_VERSION_H_ |