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