ctkVersion.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKVERSION_H
  16. #define CTKVERSION_H
  17. #include <QString>
  18. #include <QRegExp>
  19. #include "CTKPluginFrameworkExport.h"
  20. namespace ctk {
  21. /**
  22. * Version identifier for plug-ins and packages.
  23. *
  24. * <p>
  25. * Version identifiers have four components.
  26. * <ol>
  27. * <li>Major version. A non-negative integer.</li>
  28. * <li>Minor version. A non-negative integer.</li>
  29. * <li>Micro version. A non-negative integer.</li>
  30. * <li>Qualifier. A text string. See <code>Version(const QString&)</code> for the
  31. * format of the qualifier string.</li>
  32. * </ol>
  33. *
  34. * <p>
  35. * <code>Version</code> objects are immutable.
  36. *
  37. * @Immutable
  38. */
  39. class CTK_PLUGINFW_EXPORT Version {
  40. private:
  41. friend class PluginPrivate;
  42. friend class VersionRange;
  43. unsigned int majorVersion;
  44. unsigned int minorVersion;
  45. unsigned int microVersion;
  46. QString qualifier;
  47. static const QString SEPARATOR; // = "."
  48. static const QRegExp RegExp;
  49. /**
  50. * Called by the Version constructors to validate the version components.
  51. *
  52. * @return <code>true</code> if the validation was successfull, <code>false</code> otherwise.
  53. */
  54. void validate();
  55. Version& operator=(const Version& v);
  56. Version();
  57. public:
  58. /**
  59. * The empty version "0.0.0".
  60. */
  61. static const Version& emptyVersion();
  62. /**
  63. * Creates a version identifier from the specified numerical components.
  64. *
  65. * <p>
  66. * The qualifier is set to the empty string.
  67. *
  68. * @param majorVersion Major component of the version identifier.
  69. * @param minorVersion Minor component of the version identifier.
  70. * @param microVersion Micro component of the version identifier.
  71. *
  72. */
  73. Version(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion);
  74. /**
  75. * Creates a version identifier from the specified components.
  76. *
  77. * @param majorVersion Major component of the version identifier.
  78. * @param minorVersion Minor component of the version identifier.
  79. * @param microVersion Micro component of the version identifier.
  80. * @param qualifier Qualifier component of the version identifier.
  81. */
  82. Version(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const QString& qualifier);
  83. /**
  84. * Created a version identifier from the specified string.
  85. *
  86. * <p>
  87. * Here is the grammar for version strings.
  88. *
  89. * <pre>
  90. * version ::= majorVersion('.'minorVersion('.'microVersion('.'qualifier)?)?)?
  91. * majorVersion ::= digit+
  92. * minorVersion ::= digit+
  93. * microVersion ::= digit+
  94. * qualifier ::= (alpha|digit|'_'|'-')+
  95. * digit ::= [0..9]
  96. * alpha ::= [a..zA..Z]
  97. * </pre>
  98. *
  99. * There must be no whitespace in version.
  100. *
  101. * @param version string representation of the version identifier.
  102. */
  103. Version(const QString& version);
  104. /**
  105. * Create a version identifier from another.
  106. *
  107. * @param version Another version identifier
  108. */
  109. Version(const Version& version);
  110. /**
  111. * Parses a version identifier from the specified string.
  112. *
  113. * <p>
  114. * See <code>Version(const QString&)</code> for the format of the version string.
  115. *
  116. * @param version string representation of the version identifier. Leading
  117. * and trailing whitespace will be ignored.
  118. * @return A <code>Version</code> object representing the version
  119. * identifier. If <code>version</code> is the empty string
  120. * then <code>emptyVersion</code> will be
  121. * returned.
  122. */
  123. static Version parseVersion(const QString& version);
  124. /**
  125. * Returns the majorVersion component of this version identifier.
  126. *
  127. * @return The majorVersion component.
  128. */
  129. unsigned int getMajor() const;
  130. /**
  131. * Returns the minorVersion component of this version identifier.
  132. *
  133. * @return The minorVersion component.
  134. */
  135. unsigned int getMinor() const;
  136. /**
  137. * Returns the microVersion component of this version identifier.
  138. *
  139. * @return The microVersion component.
  140. */
  141. unsigned int getMicro() const;
  142. /**
  143. * Returns the qualifier component of this version identifier.
  144. *
  145. * @return The qualifier component.
  146. */
  147. QString getQualifier() const;
  148. /**
  149. * Returns the string representation of this version identifier.
  150. *
  151. * <p>
  152. * The format of the version string will be <code>majorVersion.minorVersion.microVersion</code>
  153. * if qualifier is the empty string or
  154. * <code>majorVersion.minorVersion.microVersion.qualifier</code> otherwise.
  155. *
  156. * @return The string representation of this version identifier.
  157. */
  158. QString toString() const;
  159. /**
  160. * Compares this <code>Version</code> object to another object.
  161. *
  162. * <p>
  163. * A version is considered to be <b>equal to </b> another version if the
  164. * majorVersion, minorVersion and microVersion components are equal and the qualifier component
  165. * is equal.
  166. *
  167. * @param object The <code>Version</code> object to be compared.
  168. * @return <code>true</code> if <code>object</code> is a
  169. * <code>Version</code> and is equal to this object;
  170. * <code>false</code> otherwise.
  171. */
  172. bool operator==(const Version& object) const;
  173. /**
  174. * Compares this <code>Version</code> object to another object.
  175. *
  176. * <p>
  177. * A version is considered to be <b>less than </b> another version if its
  178. * majorVersion component is less than the other version's majorVersion component, or the
  179. * majorVersion components are equal and its minorVersion component is less than the other
  180. * version's minorVersion component, or the majorVersion and minorVersion components are equal
  181. * and its microVersion component is less than the other version's microVersion component,
  182. * or the majorVersion, minorVersion and microVersion components are equal and it's qualifier
  183. * component is less than the other version's qualifier component (using
  184. * <code>std::string::compare</code>).
  185. *
  186. * <p>
  187. * A version is considered to be <b>equal to</b> another version if the
  188. * majorVersion, minorVersion and microVersion components are equal and the qualifier component
  189. * is equal.
  190. *
  191. * @param object The <code>Version</code> object to be compared.
  192. * @return A negative integer, zero, or a positive integer if this object is
  193. * less than, equal to, or greater than the specified
  194. * <code>Version</code> object.
  195. */
  196. int compare(const Version& object) const;
  197. };
  198. }
  199. CTK_PLUGINFW_EXPORT QDebug operator<<(QDebug dbg, const ctk::Version& v);
  200. #endif // CTKVERSION_H