blob: dc1e3c73d4e4b0ecd196c8fa98a7692e450532c5 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]19b8d82f2009-01-29 19:18:572// 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
wfhbf68f4d5b2015-03-10 01:32:598#include <stdint.h>
robpercivaldcd8b102016-01-25 19:39:009
10#include <iosfwd>
[email protected]19b8d82f2009-01-29 19:18:5711#include <string>
Helmut Januschka6bfb01f52024-05-28 15:47:4512#include <string_view>
[email protected]19b8d82f2009-01-29 19:18:5713#include <vector>
14
[email protected]0bea7252011-08-05 15:34:0015#include "base/base_export.h"
[email protected]19b8d82f2009-01-29 19:18:5716
[email protected]1f04ef42013-04-22 07:35:5017namespace base {
18
[email protected]36ee0322010-12-23 21:27:1219// Version represents a dotted version number, like "1.2.3.4", supporting
20// parsing and comparison.
[email protected]0bea7252011-08-05 15:34:0021class BASE_EXPORT Version {
[email protected]2fdc86a2010-01-26 23:08:0222 public:
[email protected]760024782011-06-07 17:21:3023 // The only thing you can legally do to a default constructed
24 // Version object is assign to it.
[email protected]26931bc2010-03-25 22:19:0425 Version();
26
vmpstre65942b2016-02-25 00:50:3127 Version(const Version& other);
David Bertonid8a8c002024-07-18 19:30:4628 Version(Version&& other);
29
30 Version& operator=(const Version& other) = default;
31 Version& operator=(Version&& other) = default;
vmpstre65942b2016-02-25 00:50:3132
[email protected]760024782011-06-07 17:21:3033 // Initializes from a decimal dotted version number, like "0.1.1".
Christian Flachbf5fdc42023-10-24 02:30:1834 // Each component is limited to a uint32_t. Call IsValid() to learn
[email protected]760024782011-06-07 17:21:3035 // the outcome.
Helmut Januschka6bfb01f52024-05-28 15:47:4536 explicit Version(std::string_view version_str);
[email protected]19b8d82f2009-01-29 19:18:5737
staraz8fb38082016-07-25 18:48:2138 // 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
[email protected]760024782011-06-07 17:21:3044 // Returns true if the object contains a valid version number.
45 bool IsValid() const;
46
[email protected]810b25082012-07-04 16:22:4847 // 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.
Helmut Januschka6bfb01f52024-05-28 15:47:4551 static bool IsValidWildcardString(std::string_view wildcard_string);
[email protected]810b25082012-07-04 16:22:4852
Chris Fredrickson075b4952022-11-21 15:57:3053 // Returns -1, 0, 1 for <, ==, >. `this` and `other` must both be valid.
[email protected]19b8d82f2009-01-29 19:18:5754 int CompareTo(const Version& other) const;
55
[email protected]810b25082012-07-04 16:22:4856 // 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.
Helmut Januschka6bfb01f52024-05-28 15:47:4560 int CompareToWildcardString(std::string_view wildcard_string) const;
[email protected]810b25082012-07-04 16:22:4861
[email protected]19b8d82f2009-01-29 19:18:5762 // Return the string representation of this version.
Devlin Croninfde745d2019-09-25 22:21:5763 std::string GetString() const;
[email protected]19b8d82f2009-01-29 19:18:5764
wfhbf68f4d5b2015-03-10 01:32:5965 const std::vector<uint32_t>& components() const { return components_; }
[email protected]19b8d82f2009-01-29 19:18:5766
[email protected]2fdc86a2010-01-26 23:08:0267 private:
wfhbf68f4d5b2015-03-10 01:32:5968 std::vector<uint32_t> components_;
[email protected]19b8d82f2009-01-29 19:18:5769};
70
robpercivaldcd8b102016-01-25 19:39:0071BASE_EXPORT bool operator==(const Version& v1, const Version& v2);
robpercivaldcd8b102016-01-25 19:39:0072BASE_EXPORT bool operator<(const Version& v1, const Version& v2);
73BASE_EXPORT bool operator<=(const Version& v1, const Version& v2);
74BASE_EXPORT bool operator>(const Version& v1, const Version& v2);
75BASE_EXPORT bool operator>=(const Version& v1, const Version& v2);
76BASE_EXPORT std::ostream& operator<<(std::ostream& stream, const Version& v);
77
[email protected]1f04ef42013-04-22 07:35:5078} // namespace base
79
[email protected]19b8d82f2009-01-29 19:18:5780#endif // BASE_VERSION_H_