ctkVersion.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #ifndef CTKVERSION_H
  2. #define CTKVERSION_H
  3. #include <QString>
  4. #include <QRegExp>
  5. namespace ctk {
  6. /**
  7. * Version identifier for plug-ins and packages.
  8. *
  9. * <p>
  10. * Version identifiers have four components.
  11. * <ol>
  12. * <li>Major version. A non-negative integer.</li>
  13. * <li>Minor version. A non-negative integer.</li>
  14. * <li>Micro version. A non-negative integer.</li>
  15. * <li>Qualifier. A text string. See <code>Version(const QString&)</code> for the
  16. * format of the qualifier string.</li>
  17. * </ol>
  18. *
  19. * <p>
  20. * <code>Version</code> objects are immutable.
  21. *
  22. * @Immutable
  23. */
  24. class Version {
  25. private:
  26. bool valid;
  27. unsigned int major;
  28. unsigned int minor;
  29. unsigned int micro;
  30. QString qualifier;
  31. static const QString SEPARATOR; // = "."
  32. static const QRegExp RegExp;
  33. /**
  34. * Called by the Version constructors to validate the version components.
  35. *
  36. * @return <code>true</code> if the validation was successfull, <code>false</code> otherwise.
  37. */
  38. void validate();
  39. public:
  40. /**
  41. * The empty version "0.0.0".
  42. */
  43. static const Version emptyVersion;
  44. /**
  45. * Creates a version identifier from the specified numerical components.
  46. *
  47. * <p>
  48. * The qualifier is set to the empty string.
  49. *
  50. * @param major Major component of the version identifier.
  51. * @param minor Minor component of the version identifier.
  52. * @param micro Micro component of the version identifier.
  53. *
  54. */
  55. Version(unsigned int major, unsigned int minor, unsigned int micro);
  56. /**
  57. * Creates a version identifier from the specified components.
  58. *
  59. * @param major Major component of the version identifier.
  60. * @param minor Minor component of the version identifier.
  61. * @param micro Micro component of the version identifier.
  62. * @param qualifier Qualifier component of the version identifier.
  63. */
  64. Version(unsigned int major, unsigned int minor, unsigned int micro, const QString& qualifier);
  65. /**
  66. * Created a version identifier from the specified string.
  67. *
  68. * <p>
  69. * Here is the grammar for version strings.
  70. *
  71. * <pre>
  72. * version ::= major('.'minor('.'micro('.'qualifier)?)?)?
  73. * major ::= digit+
  74. * minor ::= digit+
  75. * micro ::= digit+
  76. * qualifier ::= (alpha|digit|'_'|'-')+
  77. * digit ::= [0..9]
  78. * alpha ::= [a..zA..Z]
  79. * </pre>
  80. *
  81. * There must be no whitespace in version.
  82. *
  83. * @param version string representation of the version identifier.
  84. */
  85. Version(const QString& version);
  86. /**
  87. * Create a version identifier from another.
  88. *
  89. * @param version Another version identifier
  90. */
  91. Version(const Version& version);
  92. /**
  93. * Parses a version identifier from the specified string.
  94. *
  95. * <p>
  96. * See <code>Version(const QString&)</code> for the format of the version string.
  97. *
  98. * @param version string representation of the version identifier. Leading
  99. * and trailing whitespace will be ignored.
  100. * @return A <code>Version</code> object representing the version
  101. * identifier. If <code>version</code> is the empty string
  102. * then <code>emptyVersion</code> will be
  103. * returned.
  104. */
  105. static Version parseVersion(const QString& version);
  106. /**
  107. * Returns if the version is valid.
  108. */
  109. bool isValid() const;
  110. /**
  111. * Returns the major component of this version identifier.
  112. *
  113. * @return The major component.
  114. */
  115. unsigned int getMajor() const;
  116. /**
  117. * Returns the minor component of this version identifier.
  118. *
  119. * @return The minor component.
  120. */
  121. unsigned int getMinor() const;
  122. /**
  123. * Returns the micro component of this version identifier.
  124. *
  125. * @return The micro component.
  126. */
  127. unsigned int getMicro() const;
  128. /**
  129. * Returns the qualifier component of this version identifier.
  130. *
  131. * @return The qualifier component.
  132. */
  133. QString getQualifier() const;
  134. /**
  135. * Returns the string representation of this version identifier.
  136. *
  137. * <p>
  138. * The format of the version string will be <code>major.minor.micro</code>
  139. * if qualifier is the empty string or
  140. * <code>major.minor.micro.qualifier</code> otherwise.
  141. *
  142. * @return The string representation of this version identifier.
  143. */
  144. QString toString() const;
  145. /**
  146. * Compares this <code>Version</code> object to another object.
  147. *
  148. * <p>
  149. * A version is considered to be <b>equal to </b> another version if the
  150. * major, minor and micro components are equal and the qualifier component
  151. * is equal.
  152. *
  153. * @param object The <code>Version</code> object to be compared.
  154. * @return <code>true</code> if <code>object</code> is a
  155. * <code>Version</code> and is equal to this object;
  156. * <code>false</code> otherwise.
  157. */
  158. bool operator==(const Version& object) const;
  159. /**
  160. * Compares this <code>Version</code> object to another object.
  161. *
  162. * <p>
  163. * A version is considered to be <b>less than </b> another version if its
  164. * major component is less than the other version's major component, or the
  165. * major components are equal and its minor component is less than the other
  166. * version's minor component, or the major and minor components are equal
  167. * and its micro component is less than the other version's micro component,
  168. * or the major, minor and micro components are equal and it's qualifier
  169. * component is less than the other version's qualifier component (using
  170. * <code>std::string::compare</code>).
  171. *
  172. * <p>
  173. * A version is considered to be <b>equal to</b> another version if the
  174. * major, minor and micro components are equal and the qualifier component
  175. * is equal.
  176. *
  177. * @param object The <code>Version</code> object to be compared.
  178. * @return A negative integer, zero, or a positive integer if this object is
  179. * less than, equal to, or greater than the specified
  180. * <code>Version</code> object.
  181. */
  182. int compare(const Version& object) const;
  183. };
  184. }
  185. #endif // CTKVERSION_H