[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_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 8 | |
| 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 | #include "base/basictypes.h" |
| 14 | |
[email protected] | 36ee032 | 2010-12-23 21:27:12 | [diff] [blame] | 15 | // Version represents a dotted version number, like "1.2.3.4", supporting |
| 16 | // parsing and comparison. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 17 | class BASE_EXPORT Version { |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 18 | public: |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 19 | // The only thing you can legally do to a default constructed |
| 20 | // Version object is assign to it. |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 21 | Version(); |
| 22 | |
[email protected] | 1513bf8 | 2011-06-07 17:43:20 | [diff] [blame] | 23 | ~Version(); |
| 24 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 25 | // Initializes from a decimal dotted version number, like "0.1.1". |
| 26 | // Each component is limited to a uint16. Call IsValid() to learn |
| 27 | // the outcome. |
| 28 | explicit Version(const std::string& version_str); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 29 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 30 | // Returns true if the object contains a valid version number. |
| 31 | bool IsValid() const; |
| 32 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame^] | 33 | // Returns true if the version wildcard string is valid. The version wildcard |
| 34 | // string may end with ".*" (e.g. 1.2.*, 1.*). Any other arrangement with "*" |
| 35 | // is invalid (e.g. 1.*.3 or 1.2.3*). This functions defaults to standard |
| 36 | // Version behavior (IsValid) if no wildcard is present. |
| 37 | static bool IsValidWildcardString(const std::string& wildcard_string); |
| 38 | |
[email protected] | 30c157c | 2011-08-01 17:45:08 | [diff] [blame] | 39 | // Commonly used pattern. Given a valid version object, compare if a |
| 40 | // |version_str| results in a newer version. Returns true if the |
| 41 | // string represents valid version and if the version is greater than |
| 42 | // than the version of this object. |
| 43 | bool IsOlderThan(const std::string& version_str) const; |
| 44 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 45 | // Returns NULL if the string is not in the proper format. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 46 | // Caller is responsible for freeing the Version object once done. |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 47 | // DO NOT USE FOR NEWER CODE. |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 48 | static Version* GetVersionFromString(const std::string& version_str); |
| 49 | |
[email protected] | 76002478 | 2011-06-07 17:21:30 | [diff] [blame] | 50 | // Creates a copy of this version object. Caller takes ownership. |
| 51 | // DO NOT USE FOR NEWER CODE. |
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 52 | Version* Clone() const; |
| 53 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 54 | bool Equals(const Version& other) const; |
| 55 | |
| 56 | // Returns -1, 0, 1 for <, ==, >. |
| 57 | int CompareTo(const Version& other) const; |
| 58 | |
[email protected] | 810b2508 | 2012-07-04 16:22:48 | [diff] [blame^] | 59 | // Given a valid version object, compare if a |wildcard_string| results in a |
| 60 | // newer version. This function will default to CompareTo if the string does |
| 61 | // not end in wildcard sequence ".*". IsValidWildcard(wildcard_string) must be |
| 62 | // true before using this function. |
| 63 | int CompareToWildcardString(const std::string& wildcard_string) const; |
| 64 | |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 65 | // Return the string representation of this version. |
| 66 | const std::string GetString() const; |
| 67 | |
| 68 | const std::vector<uint16>& components() const { return components_; } |
| 69 | |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 70 | private: |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 71 | std::vector<uint16> components_; |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | #endif // BASE_VERSION_H_ |