diff options
author | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-07 17:21:30 +0000 |
---|---|---|
committer | cpu@chromium.org <cpu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-07 17:21:30 +0000 |
commit | 76002478cf1d6ddf9677401f73198fb7a564fdf7 (patch) | |
tree | 7782f6b6b2e7b06bb7f66da7081790b3fc019675 /base/version.cc | |
parent | 0985614e49db6b4a94c551fda1107462b53a9aac (diff) | |
download | chromium_src-76002478cf1d6ddf9677401f73198fb7a564fdf7.zip chromium_src-76002478cf1d6ddf9677401f73198fb7a564fdf7.tar.gz chromium_src-76002478cf1d6ddf9677401f73198fb7a564fdf7.tar.bz2 |
Clean up base/Version
It turns out base/Version is really a value object but probably because of some serious accident or by
the machinations of a super villain, forgot his identity and now it thinks is a reference object,
only creatable in the heap and that could only spawn offsprings via cloning.
But fear not 'cause I've seen Version true nature; At its core is just a good 'ol vector<uint16>, which has
very respectable value semantics. Also I have removed the is_valid_ parasite as much as I could.
The old interface (GetVersionFromString and Clone) is kept so existing callers would not need to be modified.
BUG=none
TEST=included
Review URL: http://codereview.chromium.org/7105008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@88143 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/version.cc')
-rw-r--r-- | base/version.cc | 85 |
1 files changed, 41 insertions, 44 deletions
diff --git a/base/version.cc b/base/version.cc index 571672c..74c570c 100644 --- a/base/version.cc +++ b/base/version.cc @@ -10,40 +10,63 @@ #include "base/string_number_conversions.h" #include "base/string_split.h" #include "base/string_util.h" -#include "base/utf_string_conversions.h" -Version::Version() : is_valid_(false) {} +Version::Version() { +} + +Version::Version(const std::string& version_str) { + std::vector<std::string> numbers; + base::SplitString(version_str, '.', &numbers); + if (numbers.empty()) + return; + std::vector<uint16> parsed; + for (std::vector<std::string>::iterator i = numbers.begin(); + i != numbers.end(); ++i) { + int num; + if (!base::StringToInt(*i, &num)) + return; + if (num < 0) + return; + const uint16 max = 0xFFFF; + if (num > max) + return; + // This throws out things like +3, or 032. + if (base::IntToString(num) != *i) + return; + parsed.push_back(static_cast<uint16>(num)); + } + components_.swap(parsed); +} -Version::~Version() {} +bool Version::IsValid() const { + return (!components_.empty()); +} -// static +// TODO(cpu): remove this method. Version* Version::GetVersionFromString(const std::string& version_str) { - Version* vers = new Version(); - if (vers->InitFromString(version_str)) { - DCHECK(vers->is_valid_); + Version* vers = new Version(version_str); + if (vers->IsValid()) { return vers; } delete vers; return NULL; } +// TODO(cpu): remove this method. Version* Version::Clone() const { - DCHECK(is_valid_); - Version* copy = new Version(); - copy->components_ = components_; - copy->is_valid_ = true; - return copy; + DCHECK(IsValid()); + return new Version(*this); } bool Version::Equals(const Version& that) const { - DCHECK(is_valid_); - DCHECK(that.is_valid_); - return CompareTo(that) == 0; + DCHECK(IsValid()); + DCHECK(that.IsValid()); + return (CompareTo(that) == 0); } int Version::CompareTo(const Version& other) const { - DCHECK(is_valid_); - DCHECK(other.is_valid_); + DCHECK(IsValid()); + DCHECK(other.IsValid()); size_t count = std::min(components_.size(), other.components_.size()); for (size_t i = 0; i < count; ++i) { if (components_[i] > other.components_[i]) @@ -64,7 +87,7 @@ int Version::CompareTo(const Version& other) const { } const std::string Version::GetString() const { - DCHECK(is_valid_); + DCHECK(IsValid()); std::string version_str; size_t count = components_.size(); for (size_t i = 0; i < count - 1; ++i) { @@ -74,29 +97,3 @@ const std::string Version::GetString() const { version_str.append(base::IntToString(components_[count - 1])); return version_str; } - -bool Version::InitFromString(const std::string& version_str) { - DCHECK(!is_valid_); - std::vector<std::string> numbers; - base::SplitString(version_str, '.', &numbers); - if (numbers.empty()) - return false; - for (std::vector<std::string>::iterator i = numbers.begin(); - i != numbers.end(); ++i) { - int num; - if (!base::StringToInt(*i, &num)) - return false; - if (num < 0) - return false; - const uint16 max = 0xFFFF; - if (num > max) - return false; - // This throws out things like +3, or 032. - if (base::IntToString(num) != *i) - return false; - uint16 component = static_cast<uint16>(num); - components_.push_back(component); - } - is_valid_ = true; - return true; -} |