diff options
author | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-01 17:45:08 +0000 |
---|---|---|
committer | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-01 17:45:08 +0000 |
commit | 30c157c0312dd978500c5ede8cc06849b4c13f77 (patch) | |
tree | 6eecfd36dcb7bc33f71b60103a6f2d8877c5ad96 /base | |
parent | f5fe361a09af8005f0553557d91274339bc20755 (diff) | |
download | chromium_src-30c157c0312dd978500c5ede8cc06849b4c13f77.zip chromium_src-30c157c0312dd978500c5ede8cc06849b4c13f77.tar.gz chromium_src-30c157c0312dd978500c5ede8cc06849b4c13f77.tar.bz2 |
Add a convenience function to Version
Came up as in item in a component updater review. It is a common pattern used in extension updater and component updater.
TEST=included
BUG=none
Review URL: http://codereview.chromium.org/7541015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@94928 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/version.cc | 7 | ||||
-rw-r--r-- | base/version.h | 6 | ||||
-rw-r--r-- | base/version_unittest.cc | 2 |
3 files changed, 15 insertions, 0 deletions
diff --git a/base/version.cc b/base/version.cc index cdd15a0..1f9bd20 100644 --- a/base/version.cc +++ b/base/version.cc @@ -45,6 +45,13 @@ bool Version::IsValid() const { return (!components_.empty()); } +bool Version::IsOlderThan(const std::string& version_str) const { + Version proposed_ver(version_str); + if (!proposed_ver.IsValid()) + return false; + return (CompareTo(proposed_ver) < 0); +} + // TODO(cpu): remove this method. Version* Version::GetVersionFromString(const std::string& version_str) { Version* vers = new Version(version_str); diff --git a/base/version.h b/base/version.h index e03b01a..b61cbb5 100644 --- a/base/version.h +++ b/base/version.h @@ -31,6 +31,12 @@ class BASE_API Version { // Returns true if the object contains a valid version number. bool IsValid() const; + // Commonly used pattern. Given a valid version object, compare if a + // |version_str| results in a newer version. Returns true if the + // string represents valid version and if the version is greater than + // than the version of this object. + bool IsOlderThan(const std::string& version_str) const; + // Returns NULL if the string is not in the proper format. // Caller is responsible for freeing the Version object once done. // DO NOT USE FOR NEWER CODE. diff --git a/base/version_unittest.cc b/base/version_unittest.cc index a3b1ac6..342d0d6 100644 --- a/base/version_unittest.cc +++ b/base/version_unittest.cc @@ -85,5 +85,7 @@ TEST_F(VersionTest, Compare) { scoped_ptr<Version> rhs(Version::GetVersionFromString(cases[i].rhs)); EXPECT_EQ(lhs->CompareTo(*rhs), cases[i].expected) << cases[i].lhs << " ? " << cases[i].rhs; + + EXPECT_EQ(lhs->IsOlderThan(cases[i].rhs), (cases[i].expected == -1)); } } |