1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_VERSION_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_VERSION_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #include <iosfwd> 11*635a8641SAndroid Build Coastguard Worker #include <string> 12*635a8641SAndroid Build Coastguard Worker #include <vector> 13*635a8641SAndroid Build Coastguard Worker 14*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h" 15*635a8641SAndroid Build Coastguard Worker 16*635a8641SAndroid Build Coastguard Worker namespace base { 17*635a8641SAndroid Build Coastguard Worker 18*635a8641SAndroid Build Coastguard Worker // Version represents a dotted version number, like "1.2.3.4", supporting 19*635a8641SAndroid Build Coastguard Worker // parsing and comparison. 20*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT Version { 21*635a8641SAndroid Build Coastguard Worker public: 22*635a8641SAndroid Build Coastguard Worker // The only thing you can legally do to a default constructed 23*635a8641SAndroid Build Coastguard Worker // Version object is assign to it. 24*635a8641SAndroid Build Coastguard Worker Version(); 25*635a8641SAndroid Build Coastguard Worker 26*635a8641SAndroid Build Coastguard Worker Version(const Version& other); 27*635a8641SAndroid Build Coastguard Worker 28*635a8641SAndroid Build Coastguard Worker // Initializes from a decimal dotted version number, like "0.1.1". 29*635a8641SAndroid Build Coastguard Worker // Each component is limited to a uint16_t. Call IsValid() to learn 30*635a8641SAndroid Build Coastguard Worker // the outcome. 31*635a8641SAndroid Build Coastguard Worker explicit Version(const std::string& version_str); 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker // Initializes from a vector of components, like {1, 2, 3, 4}. Call IsValid() 34*635a8641SAndroid Build Coastguard Worker // to learn the outcome. 35*635a8641SAndroid Build Coastguard Worker explicit Version(std::vector<uint32_t> components); 36*635a8641SAndroid Build Coastguard Worker 37*635a8641SAndroid Build Coastguard Worker ~Version(); 38*635a8641SAndroid Build Coastguard Worker 39*635a8641SAndroid Build Coastguard Worker // Returns true if the object contains a valid version number. 40*635a8641SAndroid Build Coastguard Worker bool IsValid() const; 41*635a8641SAndroid Build Coastguard Worker 42*635a8641SAndroid Build Coastguard Worker // Returns true if the version wildcard string is valid. The version wildcard 43*635a8641SAndroid Build Coastguard Worker // string may end with ".*" (e.g. 1.2.*, 1.*). Any other arrangement with "*" 44*635a8641SAndroid Build Coastguard Worker // is invalid (e.g. 1.*.3 or 1.2.3*). This functions defaults to standard 45*635a8641SAndroid Build Coastguard Worker // Version behavior (IsValid) if no wildcard is present. 46*635a8641SAndroid Build Coastguard Worker static bool IsValidWildcardString(const std::string& wildcard_string); 47*635a8641SAndroid Build Coastguard Worker 48*635a8641SAndroid Build Coastguard Worker // Returns -1, 0, 1 for <, ==, >. 49*635a8641SAndroid Build Coastguard Worker int CompareTo(const Version& other) const; 50*635a8641SAndroid Build Coastguard Worker 51*635a8641SAndroid Build Coastguard Worker // Given a valid version object, compare if a |wildcard_string| results in a 52*635a8641SAndroid Build Coastguard Worker // newer version. This function will default to CompareTo if the string does 53*635a8641SAndroid Build Coastguard Worker // not end in wildcard sequence ".*". IsValidWildcard(wildcard_string) must be 54*635a8641SAndroid Build Coastguard Worker // true before using this function. 55*635a8641SAndroid Build Coastguard Worker int CompareToWildcardString(const std::string& wildcard_string) const; 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker // Return the string representation of this version. 58*635a8641SAndroid Build Coastguard Worker const std::string GetString() const; 59*635a8641SAndroid Build Coastguard Worker components()60*635a8641SAndroid Build Coastguard Worker const std::vector<uint32_t>& components() const { return components_; } 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker private: 63*635a8641SAndroid Build Coastguard Worker std::vector<uint32_t> components_; 64*635a8641SAndroid Build Coastguard Worker }; 65*635a8641SAndroid Build Coastguard Worker 66*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool operator==(const Version& v1, const Version& v2); 67*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool operator!=(const Version& v1, const Version& v2); 68*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool operator<(const Version& v1, const Version& v2); 69*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool operator<=(const Version& v1, const Version& v2); 70*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool operator>(const Version& v1, const Version& v2); 71*635a8641SAndroid Build Coastguard Worker BASE_EXPORT bool operator>=(const Version& v1, const Version& v2); 72*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::ostream& operator<<(std::ostream& stream, const Version& v); 73*635a8641SAndroid Build Coastguard Worker 74*635a8641SAndroid Build Coastguard Worker } // namespace base 75*635a8641SAndroid Build Coastguard Worker 76*635a8641SAndroid Build Coastguard Worker #endif // BASE_VERSION_H_ 77