version.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_VERSION_H_
  5. #define BASE_VERSION_H_
  6. #include <stdint.h>
  7. #include <iosfwd>
  8. #include <string>
  9. #include <vector>
  10. #include "base/base_export.h"
  11. #include "base/strings/string_piece.h"
  12. namespace base {
  13. // Version represents a dotted version number, like "1.2.3.4", supporting
  14. // parsing and comparison.
  15. class BASE_EXPORT Version {
  16. public:
  17. // The only thing you can legally do to a default constructed
  18. // Version object is assign to it.
  19. Version();
  20. Version(const Version& other);
  21. // Initializes from a decimal dotted version number, like "0.1.1".
  22. // Each component is limited to a uint16_t. Call IsValid() to learn
  23. // the outcome.
  24. explicit Version(StringPiece version_str);
  25. // Initializes from a vector of components, like {1, 2, 3, 4}. Call IsValid()
  26. // to learn the outcome.
  27. explicit Version(std::vector<uint32_t> components);
  28. ~Version();
  29. // Returns true if the object contains a valid version number.
  30. bool IsValid() const;
  31. // Returns true if the version wildcard string is valid. The version wildcard
  32. // string may end with ".*" (e.g. 1.2.*, 1.*). Any other arrangement with "*"
  33. // is invalid (e.g. 1.*.3 or 1.2.3*). This functions defaults to standard
  34. // Version behavior (IsValid) if no wildcard is present.
  35. static bool IsValidWildcardString(StringPiece wildcard_string);
  36. // Returns -1, 0, 1 for <, ==, >.
  37. int CompareTo(const Version& other) const;
  38. // Given a valid version object, compare if a |wildcard_string| results in a
  39. // newer version. This function will default to CompareTo if the string does
  40. // not end in wildcard sequence ".*". IsValidWildcard(wildcard_string) must be
  41. // true before using this function.
  42. int CompareToWildcardString(StringPiece wildcard_string) const;
  43. // Return the string representation of this version.
  44. std::string GetString() const;
  45. const std::vector<uint32_t>& components() const { return components_; }
  46. private:
  47. std::vector<uint32_t> components_;
  48. };
  49. BASE_EXPORT bool operator==(const Version& v1, const Version& v2);
  50. BASE_EXPORT bool operator!=(const Version& v1, const Version& v2);
  51. BASE_EXPORT bool operator<(const Version& v1, const Version& v2);
  52. BASE_EXPORT bool operator<=(const Version& v1, const Version& v2);
  53. BASE_EXPORT bool operator>(const Version& v1, const Version& v2);
  54. BASE_EXPORT bool operator>=(const Version& v1, const Version& v2);
  55. BASE_EXPORT std::ostream& operator<<(std::ostream& stream, const Version& v);
  56. } // namespace base
  57. #endif // BASE_VERSION_H_