blob: b3012eb921b1e62a803b681f03954fbb17d14cea [file] [log] [blame]
mathp@google.com1088cfd2012-07-05 01:22:48 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
erikkay@google.come05e3c62009-01-30 04:18:57 +09002// 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 <string>
9#include <vector>
10
darin@chromium.orge585bed2011-08-06 00:34:00 +090011#include "base/base_export.h"
erikkay@google.come05e3c62009-01-30 04:18:57 +090012#include "base/basictypes.h"
13
xhwang@chromium.org993e5062013-04-22 16:35:50 +090014namespace base {
15
evan@chromium.org055f7672010-12-24 06:27:12 +090016// Version represents a dotted version number, like "1.2.3.4", supporting
17// parsing and comparison.
darin@chromium.orge585bed2011-08-06 00:34:00 +090018class BASE_EXPORT Version {
erg@google.combf6ce9f2010-01-27 08:08:02 +090019 public:
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090020 // The only thing you can legally do to a default constructed
21 // Version object is assign to it.
akalin@chromium.org2659ef12010-03-26 07:19:04 +090022 Version();
23
cpu@chromium.org331e27b2011-06-08 02:43:20 +090024 ~Version();
25
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090026 // Initializes from a decimal dotted version number, like "0.1.1".
27 // Each component is limited to a uint16. Call IsValid() to learn
28 // the outcome.
29 explicit Version(const std::string& version_str);
erikkay@google.come05e3c62009-01-30 04:18:57 +090030
cpu@chromium.orgfa761a32011-06-08 02:21:30 +090031 // Returns true if the object contains a valid version number.
32 bool IsValid() const;
33
mathp@google.com1088cfd2012-07-05 01:22:48 +090034 // Returns true if the version wildcard string is valid. The version wildcard
35 // string may end with ".*" (e.g. 1.2.*, 1.*). Any other arrangement with "*"
36 // is invalid (e.g. 1.*.3 or 1.2.3*). This functions defaults to standard
37 // Version behavior (IsValid) if no wildcard is present.
38 static bool IsValidWildcardString(const std::string& wildcard_string);
39
cpu@chromium.org9008a612011-08-02 02:45:08 +090040 // Commonly used pattern. Given a valid version object, compare if a
41 // |version_str| results in a newer version. Returns true if the
42 // string represents valid version and if the version is greater than
43 // than the version of this object.
44 bool IsOlderThan(const std::string& version_str) const;
45
erikkay@google.come05e3c62009-01-30 04:18:57 +090046 bool Equals(const Version& other) const;
47
48 // Returns -1, 0, 1 for <, ==, >.
49 int CompareTo(const Version& other) const;
50
mathp@google.com1088cfd2012-07-05 01:22:48 +090051 // Given a valid version object, compare if a |wildcard_string| results in a
52 // newer version. This function will default to CompareTo if the string does
53 // not end in wildcard sequence ".*". IsValidWildcard(wildcard_string) must be
54 // true before using this function.
55 int CompareToWildcardString(const std::string& wildcard_string) const;
56
erikkay@google.come05e3c62009-01-30 04:18:57 +090057 // Return the string representation of this version.
58 const std::string GetString() const;
59
60 const std::vector<uint16>& components() const { return components_; }
61
erg@google.combf6ce9f2010-01-27 08:08:02 +090062 private:
erikkay@google.come05e3c62009-01-30 04:18:57 +090063 std::vector<uint16> components_;
erikkay@google.come05e3c62009-01-30 04:18:57 +090064};
65
xhwang@chromium.org993e5062013-04-22 16:35:50 +090066} // namespace base
67
avi@chromium.orgf88221a2014-01-04 01:06:13 +090068// TODO(xhwang) remove this when all users are updated to explicitly use the
69// namespace
70using base::Version;
71
erikkay@google.come05e3c62009-01-30 04:18:57 +090072#endif // BASE_VERSION_H_