[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |||||
12 | #include "base/basictypes.h" | ||||
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 13 | #include "base/gtest_prod_util.h" |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 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. | ||||
17 | // Each component is limited to a uint16. | ||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 18 | class Version { |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 19 | public: |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 20 | // Exposed only so that a Version can be stored in STL containers; |
21 | // any call to the methods below on a default-constructed Version | ||||
22 | // will DCHECK. | ||||
23 | Version(); | ||||
24 | |||||
[email protected] | 2858bbf | 2010-10-05 23:46:02 | [diff] [blame] | 25 | ~Version(); |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 26 | |
[email protected] | a502bbe7 | 2011-01-07 18:06:45 | [diff] [blame] | 27 | // The version string must be made up of 1 or more uint16's separated |
28 | // by '.'. Returns NULL if string is not in this format. | ||||
29 | // Caller is responsible for freeing the Version object once done. | ||||
30 | static Version* GetVersionFromString(const std::string& version_str); | ||||
31 | |||||
[email protected] | b566c11 | 2010-12-21 08:27:25 | [diff] [blame] | 32 | // Creates a copy of this version. Caller takes ownership. |
33 | Version* Clone() const; | ||||
34 | |||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 35 | bool Equals(const Version& other) const; |
36 | |||||
37 | // Returns -1, 0, 1 for <, ==, >. | ||||
38 | int CompareTo(const Version& other) const; | ||||
39 | |||||
40 | // Return the string representation of this version. | ||||
41 | const std::string GetString() const; | ||||
42 | |||||
43 | const std::vector<uint16>& components() const { return components_; } | ||||
44 | |||||
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 45 | private: |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 46 | bool InitFromString(const std::string& version_str); |
47 | |||||
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 48 | bool is_valid_; |
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 49 | std::vector<uint16> components_; |
[email protected] | 26931bc | 2010-03-25 22:19:04 | [diff] [blame] | 50 | |
[email protected] | a918f87 | 2010-06-01 14:30:51 | [diff] [blame] | 51 | FRIEND_TEST_ALL_PREFIXES(VersionTest, DefaultConstructor); |
52 | FRIEND_TEST_ALL_PREFIXES(VersionTest, GetVersionFromString); | ||||
53 | FRIEND_TEST_ALL_PREFIXES(VersionTest, Compare); | ||||
[email protected] | 19b8d82f | 2009-01-29 19:18:57 | [diff] [blame] | 54 | }; |
55 | |||||
56 | #endif // BASE_VERSION_H_ |