Spinning Topp Logo BlackTopp Studios
inc
version.h
1 // © Copyright 2010 - 2015 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 
41 #ifndef _version_h
42 #define _version_h
43 
44 #include "datatypes.h"
45 
46 namespace Mezzanine
47 {
48  ///////////////////////////////////////////////////////////////////////////////
49  /// @class SimpleVersion
50  /// @brief A very basic class for expressing an API or syntax version.
51  /// @details
52  ///////////////////////////////////////
54  {
55  protected:
56  /// @brief Convenience method for converting text to numbers.
57  /// @remarks Other methods in the mezzanine to do this exist, but creating a basic implementation here to avoid a dependency for one small simple thing.
58  /// @param ToConvert The String to be converted.
59  /// @param Var The UInt16 to populate from the provided String.
60  void ConvertToUInt16(const String& ToConvert, UInt16& Var)
61  {
62  StringStream NumConverter;
63  NumConverter << ToConvert;
64  NumConverter >> Var;
65  }
66  public:
67  /// @brief The Major component of the version to be expressed.
69  /// @brief The Minor component of the version to be expressed.
71 
72  ///////////////////////////////////////////////////////////////////////////////
73  // Construction and Destruction
74 
75  /// @brief Blank Constructor.
77  Major(1),
78  Minor(0)
79  { }
80 
81  /// @brief Copy Constructor.
82  /// @param Other The other SimpleVersion to be copied.
83  SimpleVersion(const SimpleVersion& Other) :
84  Major(Other.Major),
85  Minor(Other.Minor)
86  { }
87 
88  /// @brief String Constructor.
89  /// @param ToParse The String to be parsed.
90  SimpleVersion(const String& ToParse)
91  { this->FromString(ToParse); }
92 
93  /// @brief Number Constructor.
94  /// @param VerMajor The Major component of the version to be expressed.
95  /// @param VerMinor The Minor component of the version to be expressed.
96  SimpleVersion(const UInt16 VerMajor, const UInt16 VerMinor) :
97  Major(VerMajor),
98  Minor(VerMinor)
99  { }
100 
101  ///////////////////////////////////////////////////////////////////////////////
102  // String Conversions
103 
104  /// @brief Parses a String expressing a simple version.
105  /// @remarks This method expects a short character sequence where the middle character is the Delimiter separating the two components.
106  /// @param ToParse The String to be parsed.
107  /// @param Delim The delimiter to be used for separating the version components.
108  /// @return Returns true if the String was successfully parsed, false if there was a failure.
109  Boole FromString(const String& ToParse, const Char8 Delim = '.')
110  {
111  StringStream NumConverter;
112  size_t DelimPos = ToParse.find(Delim);
113  if( DelimPos != String::npos ) {
114  ConvertToUInt16(ToParse.substr(0,DelimPos),this->Major);
115  ConvertToUInt16(ToParse.substr(DelimPos + 1),this->Minor);
116  return true;
117  }
118  return false;
119  }
120  /// @brief Converts this version into a String.
121  /// @param Delim The delimiter to be used for separating the version components.
122  /// @return Returns a String expressing this version.
123  String ToString(const Char8 Delim = '.') const
124  {
125  StringStream ConvertStream;
126  ConvertStream << this->Major << Delim << this->Minor;
127  return ConvertStream.str();
128  }
129 
130  ///////////////////////////////////////////////////////////////////////////////
131  // Operators
132 
133  /// @brief Assignment operator.
134  /// @param Other The other SimpleVersion to assign to this.
135  void operator=(const SimpleVersion& Other)
136  { this->Major = Other.Major; this->Minor = Other.Minor; }
137 
138  ///////////////////////////////////////////////////////////////////////////////
139  // Comparison Operators
140 
141  /// @brief Less than comparison operator.
142  /// @param Other The other SimpleVersion to be compared to.
143  /// @return Returns true if this compares less than the other SimpleVersion.
144  Boole operator<(const SimpleVersion& Other) const
145  {
146  if( this->Major < Other.Major ) return true;
147  else if( this->Major > Other.Major ) return false;
148  else return ( this->Minor < Other.Minor );
149  }
150  /// @brief Less than or equal to comparison operator.
151  /// @param Other The other SimpleVersion to be compared to.
152  /// @return Returns true if this compares less than or equal to the other SimpleVersion.
153  Boole operator<=(const SimpleVersion& Other) const
154  {
155  if( this->Major < Other.Major ) return true;
156  else if( this->Major > Other.Major ) return false;
157  else return ( this->Minor <= Other.Minor );
158  }
159  /// @brief Greater than comparison operator.
160  /// @param Other The other SimpleVersion to be compared to.
161  /// @return Returns true if this compares greater than the other SimpleVersion.
162  Boole operator>(const SimpleVersion& Other) const
163  {
164  if( this->Major > Other.Major ) return true;
165  else if( this->Major < Other.Major ) return false;
166  else return ( this->Minor > Other.Minor );
167  }
168  /// @brief Greater than or equal to comparison operator.
169  /// @param Other The other SimpleVersion to be compared to.
170  /// @return Returns true if this compares greater than or equal to the other SimpleVersion.
171  Boole operator>=(const SimpleVersion& Other) const
172  {
173  if( this->Major > Other.Major ) return true;
174  else if( this->Major < Other.Major ) return false;
175  else return ( this->Minor >= Other.Minor );
176  }
177 
178  /// @brief Equality comparison operator.
179  /// @param Other The other SimpleVersion to be compared to.
180  /// @return Returns true if the two SimpleVersions are equal.
181  Boole operator==(const SimpleVersion& Other) const
182  { return ( this->Major == Other.Major && this->Minor == Other.Minor ); }
183  /// @brief Inequality comparison operator.
184  /// @param Other The other SimpleVersion to be compared to.
185  /// @return Returns true if the two SimpleVersions are inequal.
186  Boole operator!=(const SimpleVersion& Other) const
187  { return ( this->Major != Other.Major || this->Minor != Other.Minor ); }
188  };//SimpleVersion
189 
190  ///////////////////////////////////////////////////////////////////////////////
191  /// @class SemanticVersion
192  /// @brief A Version class matching the Semantic Version specification.
193  /// @details
194  ///////////////////////////////////////
196  {
197  protected:
198  /// @brief Convenience method for converting text to numbers.
199  /// @remarks Other methods in the mezzanine to do this exist, but creating a basic implementation here to avoid a dependency for one small simple thing.
200  /// @param ToConvert The String to be converted.
201  /// @param Var The UInt16 to populate from the provided String.
202  void ConvertToUInt16(const String& ToConvert, UInt16& Var)
203  {
204  StringStream NumConverter;
205  NumConverter << ToConvert;
206  NumConverter >> Var;
207  }
208  public:
209  /// @brief The Major component of the version to be expressed.
211  /// @brief The Minor component of the version to be expressed.
213  /// @brief The Patch component of the version to be expressed.
215  /// @brief An optional pre-release string providing additional context for the version.
217  /// @brief An optional component for build meta data associated with the version.
219 
220  ///////////////////////////////////////////////////////////////////////////////
221  // Construction and Destruction
222 
223  /// @brief Blank Constructor.
225  Major(1),
226  Minor(0),
227  Patch(0)
228  { }
229  /// @brief Copy Constructor.
230  /// @param Other The other SemanticVersion to be copied.
232  Major(Other.Major),
233  Minor(Other.Minor),
234  Patch(Other.Patch),
235  PreRelease(Other.PreRelease),
236  MetaData(Other.MetaData)
237  { }
238  /// @brief Parse Constructor.
239  /// @param ToParse The String to be parsed.
240  SemanticVersion(const String& ToParse)
241  { this->FromString(ToParse); }
242  /// @brief Number Component Constructor.
243  /// @param VerMajor The Major component of the version to be expressed.
244  /// @param VerMinor The Minor component of the version to be expressed.
245  /// @param VerPatch The Patch component of the version to be expressed.
246  SemanticVersion(const UInt16 VerMajor, const UInt16 VerMinor, const UInt16 VerPatch) :
247  Major(VerMajor),
248  Minor(VerMinor),
249  Patch(VerPatch)
250  { }
251  /// @brief Descriptive Constructor.
252  /// @param VerMajor The Major component of the version to be expressed.
253  /// @param VerMinor The Minor component of the version to be expressed.
254  /// @param VerPatch The Patch component of the version to be expressed.
255  /// @param VerDesc An optional description string providing additional context for the version.
256  /// @param VerMetaData An optional component for build meta data associated with the version.
257  SemanticVersion(const UInt16 VerMajor, const UInt16 VerMinor, const UInt16 VerPatch, const String& VerDesc, const String& VerMetaData) :
258  Major(VerMajor),
259  Minor(VerMinor),
260  Patch(VerPatch),
261  PreRelease(VerDesc),
262  MetaData(VerMetaData)
263  { }
264 
265  ///////////////////////////////////////////////////////////////////////////////
266  // String Conversions
267 
268  /// @brief Parses a String expressing a simple version.
269  /// @remarks This method expects a 3 character sequence where the middle character is the Delimiter separating the two components.
270  /// @param ToParse The String to be parsed.
271  /// @return Returns true if the String was successfully parsed, false if there was a failure.
272  Boole FromString(const String& ToParse)
273  {
274  Boole Ret = false;
275  StringStream NumConverter;
276  SemanticVersion TempVer;
277  // Find the first dot so we can parse the Major component.
278  size_t StartPos = 0;
279  size_t DelimPos = ToParse.find('.');
280  if( DelimPos != String::npos ) {
281  ConvertToUInt16(ToParse.substr(StartPos,DelimPos),TempVer.Major);
282  // Find the second dot so we can parse the Minor component.
283  StartPos = DelimPos + 1;
284  DelimPos = ToParse.find('.',StartPos);
285  if( DelimPos != String::npos ) {
286  ConvertToUInt16(ToParse.substr(StartPos,DelimPos - StartPos),TempVer.Minor);
287  // From here, it could end or there could be more delimiters. Search for the delims but don't freak out if we don't find them.
288  StartPos = DelimPos + 1;
289  DelimPos = ToParse.find_first_of("-+",StartPos);
290  ConvertToUInt16(ToParse.substr(StartPos,DelimPos - StartPos),TempVer.Patch);
291  if( DelimPos != String::npos && ToParse.at(DelimPos) == '-' ) {
292  StartPos = DelimPos + 1;
293  DelimPos = ToParse.find('+',StartPos);
294  TempVer.PreRelease = ToParse.substr(StartPos,DelimPos - StartPos);
295  }
296  if( DelimPos != String::npos && ToParse.at(DelimPos) == '+' ) {
297  StartPos = DelimPos + 1;
298  TempVer.MetaData = ToParse.substr(StartPos);
299  }
300  Ret = true;
301  }
302  }
303 
304  if( Ret ) {
305  *this = TempVer;
306  }
307  return Ret;
308  }
309  /// @brief Converts this version into a String.
310  /// @return Returns a String expressing this version.
311  String ToString() const
312  {
313  StringStream ConvertStream;
314  ConvertStream << this->Major << '.' << this->Minor << '.' << this->Patch;
315  if( !this->PreRelease.empty() ) {
316  ConvertStream << '-' << this->PreRelease;
317  }
318  if( !this->MetaData.empty() ) {
319  ConvertStream << '+' << this->MetaData;
320  }
321  return ConvertStream.str();
322  }
323 
324  ///////////////////////////////////////////////////////////////////////////////
325  // Operators
326 
327  /// @brief Assignment operator.
328  /// @param Other The other SemanticVersion to assign to this.
329  void operator=(const SemanticVersion& Other)
330  {
331  this->Major = Other.Major;
332  this->Minor = Other.Minor;
333  this->Patch = Other.Patch;
334  this->PreRelease = Other.PreRelease;
335  this->MetaData = Other.MetaData;
336  }
337 
338  ///////////////////////////////////////////////////////////////////////////////
339  // Comparison Operators
340 
341  /// @brief Less than comparison operator.
342  /// @param Other The other SemanticVersion to be compared to.
343  /// @return Returns true if this compares less than the other SemanticVersion.
344  Boole operator<(const SemanticVersion& Other) const
345  {
346  // Check the Major component
347  if( this->Major < Other.Major ) return true;
348  else if( this->Major > Other.Major ) return false;
349  // Check the Minor component
350  if( this->Minor < Other.Minor ) return true;
351  else if( this->Minor > Other.Minor ) return false;
352  // Check the Patch component
353  if( this->Patch < Other.Patch ) return true;
354  else if( this->Patch > Other.Patch ) return false;
355  // Check the PreRelease component
356  return ( this->PreRelease < Other.PreRelease );
357  }
358  /// @brief Less than or equal to comparison operator.
359  /// @param Other The other SemanticVersion to be compared to.
360  /// @return Returns true if this compares less than or equal to the other SemanticVersion.
361  Boole operator<=(const SemanticVersion& Other) const
362  {
363  // Check the Major component
364  if( this->Major < Other.Major ) return true;
365  else if( this->Major > Other.Major ) return false;
366  // Check the Minor component
367  if( this->Minor < Other.Minor ) return true;
368  else if( this->Minor > Other.Minor ) return false;
369  // Check the Patch component
370  if( this->Patch < Other.Patch ) return true;
371  else if( this->Patch > Other.Patch ) return false;
372  // Check the PreRelease component
373  return ( this->PreRelease <= Other.PreRelease );
374  }
375  /// @brief Greater than comparison operator.
376  /// @param Other The other SemanticVersion to be compared to.
377  /// @return Returns true if this compares greater than the other SemanticVersion.
378  Boole operator>(const SemanticVersion& Other) const
379  {
380  // Check the Major component
381  if( this->Major > Other.Major ) return true;
382  else if( this->Major < Other.Major ) return false;
383  // Check the Minor component
384  if( this->Minor > Other.Minor ) return true;
385  else if( this->Minor < Other.Minor ) return false;
386  // Check the Patch component
387  if( this->Patch > Other.Patch ) return true;
388  else if( this->Patch < Other.Patch ) return false;
389  // Check the PreRelease component
390  return ( this->PreRelease > Other.PreRelease );
391  }
392  /// @brief Greater than or equal to comparison operator.
393  /// @param Other The other SemanticVersion to be compared to.
394  /// @return Returns true if this compares greater than or equal to the other SemanticVersion.
395  Boole operator>=(const SemanticVersion& Other) const
396  {
397  // Check the Major component
398  if( this->Major > Other.Major ) return true;
399  else if( this->Major < Other.Major ) return false;
400  // Check the Minor component
401  if( this->Minor > Other.Minor ) return true;
402  else if( this->Minor < Other.Minor ) return false;
403  // Check the Patch component
404  if( this->Patch > Other.Patch ) return true;
405  else if( this->Patch < Other.Patch ) return false;
406  // Check the PreRelease component
407  return ( this->PreRelease >= Other.PreRelease );
408  }
409 
410  /// @brief Equality comparison operator.
411  /// @param Other The other SemanticVersion to be compared to.
412  /// @return Returns true if the two SimpleVersions are equal.
413  Boole operator==(const SemanticVersion& Other) const
414  { return ( this->Major == Other.Major && this->Minor == Other.Minor && this->Patch == Other.Patch && this->PreRelease == Other.PreRelease ); }
415  /// @brief Inequality comparison operator.
416  /// @param Other The other SemanticVersion to be compared to.
417  /// @return Returns true if the two SimpleVersions are inequal.
418  Boole operator!=(const SemanticVersion& Other) const
419  { return ( this->Major != Other.Major || this->Minor != Other.Minor || this->Patch != Other.Patch || this->PreRelease != Other.PreRelease ); }
420  };//SemanticVersion
421 }//Mezzanine
422 
423 #endif
void operator=(const SimpleVersion &Other)
Assignment operator.
Definition: version.h:135
SimpleVersion(const SimpleVersion &Other)
Copy Constructor.
Definition: version.h:83
void ConvertToUInt16(const String &ToConvert, UInt16 &Var)
Convenience method for converting text to numbers.
Definition: version.h:202
SemanticVersion(const UInt16 VerMajor, const UInt16 VerMinor, const UInt16 VerPatch, const String &VerDesc, const String &VerMetaData)
Descriptive Constructor.
Definition: version.h:257
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
SimpleVersion(const UInt16 VerMajor, const UInt16 VerMinor)
Number Constructor.
Definition: version.h:96
Boole operator==(const SimpleVersion &Other) const
Equality comparison operator.
Definition: version.h:181
UInt16 Patch
The Patch component of the version to be expressed.
Definition: version.h:214
A Version class matching the Semantic Version specification.
Definition: version.h:195
SemanticVersion(const SemanticVersion &Other)
Copy Constructor.
Definition: version.h:231
SemanticVersion(const String &ToParse)
Parse Constructor.
Definition: version.h:240
Boole operator!=(const SimpleVersion &Other) const
Inequality comparison operator.
Definition: version.h:186
All the definitions for datatypes as well as some basic conversion functions are defined here...
void operator=(const SemanticVersion &Other)
Assignment operator.
Definition: version.h:329
SimpleVersion(const String &ToParse)
String Constructor.
Definition: version.h:90
Boole operator<(const SimpleVersion &Other) const
Less than comparison operator.
Definition: version.h:144
UInt16 Major
The Major component of the version to be expressed.
Definition: version.h:210
UInt16 Major
The Major component of the version to be expressed.
Definition: version.h:68
SimpleVersion()
Blank Constructor.
Definition: version.h:76
Boole operator!=(const SemanticVersion &Other) const
Inequality comparison operator.
Definition: version.h:418
Boole operator>(const SimpleVersion &Other) const
Greater than comparison operator.
Definition: version.h:162
String PreRelease
An optional pre-release string providing additional context for the version.
Definition: version.h:216
std::stringstream StringStream
A Datatype used for streaming operations with strings.
Definition: datatypes.h:176
UInt16 Minor
The Minor component of the version to be expressed.
Definition: version.h:70
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
uint16_t UInt16
An 16-bit unsigned integer.
Definition: datatypes.h:122
Boole operator>=(const SimpleVersion &Other) const
Greater than or equal to comparison operator.
Definition: version.h:171
Boole FromString(const String &ToParse, const Char8 Delim= '.')
Parses a String expressing a simple version.
Definition: version.h:109
A very basic class for expressing an API or syntax version.
Definition: version.h:53
Boole operator>=(const SemanticVersion &Other) const
Greater than or equal to comparison operator.
Definition: version.h:395
Boole operator>(const SemanticVersion &Other) const
Greater than comparison operator.
Definition: version.h:378
String ToString() const
Converts this version into a String.
Definition: version.h:311
SemanticVersion()
Blank Constructor.
Definition: version.h:224
Boole FromString(const String &ToParse)
Parses a String expressing a simple version.
Definition: version.h:272
Boole operator<=(const SimpleVersion &Other) const
Less than or equal to comparison operator.
Definition: version.h:153
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
Boole operator<(const SemanticVersion &Other) const
Less than comparison operator.
Definition: version.h:344
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
String ToString(const Char8 Delim= '.') const
Converts this version into a String.
Definition: version.h:123
UInt16 Minor
The Minor component of the version to be expressed.
Definition: version.h:212
String MetaData
An optional component for build meta data associated with the version.
Definition: version.h:218
Boole operator==(const SemanticVersion &Other) const
Equality comparison operator.
Definition: version.h:413
void ConvertToUInt16(const String &ToConvert, UInt16 &Var)
Convenience method for converting text to numbers.
Definition: version.h:60
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
SemanticVersion(const UInt16 VerMajor, const UInt16 VerMinor, const UInt16 VerPatch)
Number Component Constructor.
Definition: version.h:246
Boole operator<=(const SemanticVersion &Other) const
Less than or equal to comparison operator.
Definition: version.h:361