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