BGC Tools
Public Member Functions | Static Public Member Functions | Data Fields | Static Public Attributes | Properties | Static Private Member Functions | Private Attributes | Static Private Attributes
BGC.Utility.ApplicationVersion Struct Reference

Sequence-based versioning parsing, managing, and comparing More...

Collaboration diagram for BGC.Utility.ApplicationVersion:
Collaboration graph
[legend]

Public Member Functions

 ApplicationVersion (string version)
 
 ApplicationVersion (params ushort[] version)
 
bool Between (in ApplicationVersion lowerBound, in ApplicationVersion upperBound)
 Checks if this ApplicationVersion in the range [lowerBound, upperBound) More...
 
bool MatchesPattern (string pattern)
 Compares the version number to one that supports asterisk wildcards Unspecified elements are considered wildcards More...
 
bool IsNull ()
 
override bool Equals (object obj)
 
override int GetHashCode ()
 
override string ToString ()
 
bool Equals (in ApplicationVersion other)
 

Static Public Member Functions

static ApplicationVersion BuildFromWild (string version, bool upperBound)
 
static bool operator< (in ApplicationVersion lVersion, in ApplicationVersion rVersion)
 
static bool operator> (in ApplicationVersion lVersion, in ApplicationVersion rVersion)
 
static bool operator<= (in ApplicationVersion lVersion, in ApplicationVersion rVersion)
 
static bool operator>= (in ApplicationVersion lVersion, in ApplicationVersion rVersion)
 
static bool operator== (in ApplicationVersion lVersion, in ApplicationVersion rVersion)
 
static bool operator!= (in ApplicationVersion lVersion, in ApplicationVersion rVersion)
 
static implicit operator ApplicationVersion (string version)
 
static implicit operator string (in ApplicationVersion version)
 

Data Fields

int Length => versions.Length
 
ushort Major => this[0]
 
ushort Minor => this[1]
 
ushort Build => this[2]
 
ushort Revision => this[3]
 

Static Public Attributes

static readonly ApplicationVersion NullVersion = new ApplicationVersion(nullVersion)
 
static readonly ApplicationVersion Max = new ApplicationVersion(maxVersion)
 

Properties

ushort this[int i] [get]
 

Static Private Member Functions

static string PrintVersionElement (ushort versionElement)
 

Private Attributes

const string WILD_CARD = "*"
 
const ushort WILD_CARD_CEIL = ushort.MaxValue
 
const ushort WILD_CARD_FLOOR = ushort.MinValue
 
const char DELIM = '.'
 
readonly ushort [] versions
 

Static Private Attributes

static readonly string DELIM_STR = DELIM.ToString()
 
static readonly ushort [] nullVersion = new ushort[] { 0, 0, 0, 0 }
 
static readonly ushort [] maxVersion = new ushort[] { ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue }
 

Detailed Description

Sequence-based versioning parsing, managing, and comparing

Definition at line 10 of file ApplicationVersion.cs.

Constructor & Destructor Documentation

◆ ApplicationVersion() [1/2]

BGC.Utility.ApplicationVersion.ApplicationVersion ( string  version)
inline

Definition at line 51 of file ApplicationVersion.cs.

52  {
53  ushort[] parsedVersion = null;
54 
55  if (string.IsNullOrEmpty(version))
56  {
57  parsedVersion = nullVersion;
58  }
59  else
60  {
61  try
62  {
63  parsedVersion = version.Split(DELIM).Select(ushort.Parse).ToArray();
64  }
65  catch (Exception)
66  {
67  Debug.LogError($"Failed to parse version string: {version}");
68  }
69  }
70 
71  if (parsedVersion == null || parsedVersion.Length == 0)
72  {
73  parsedVersion = nullVersion;
74  }
75 
76  if (parsedVersion.Length <= 4)
77  {
78  versions = parsedVersion;
79  }
80  else
81  {
82  Debug.LogError($"ApplicationVersion constructed by string with more than 4 fields: \"{version}\"");
83  versions = new ushort[4]
84  {
85  parsedVersion[0],
86  parsedVersion[1],
87  parsedVersion[2],
88  parsedVersion[3]
89  };
90  }
91  }
static readonly ushort [] nullVersion

◆ ApplicationVersion() [2/2]

BGC.Utility.ApplicationVersion.ApplicationVersion ( params ushort []  version)
inline

Definition at line 93 of file ApplicationVersion.cs.

94  {
95  if (version.Length <= 4)
96  {
97  versions = (ushort[])version.Clone();
98  }
99  else
100  {
101  Debug.LogError($"ApplicationVersion constructed by array with more than 4 fields: {{{string.Join(", ", version.Select(PrintVersionElement))}}}");
102  versions = new ushort[4]
103  {
104  version[0],
105  version[1],
106  version[2],
107  version[3]
108  };
109  }
110  }

Member Function Documentation

◆ Between()

bool BGC.Utility.ApplicationVersion.Between ( in ApplicationVersion  lowerBound,
in ApplicationVersion  upperBound 
)
inline

Checks if this ApplicationVersion in the range [lowerBound, upperBound)

Parameters
lowerBoundInclusive lowerBound
upperBoundExclusive upperBound

Definition at line 117 of file ApplicationVersion.cs.

118  {
119  if (lowerBound >= upperBound)
120  {
121  throw new ArgumentException($"lowerBound ({lowerBound}) exceeds upperBound ({upperBound})");
122  }
123 
124  return (this < upperBound) && (this >= lowerBound);
125  }

◆ BuildFromWild()

static ApplicationVersion BGC.Utility.ApplicationVersion.BuildFromWild ( string  version,
bool  upperBound 
)
inlinestatic

Definition at line 170 of file ApplicationVersion.cs.

171  {
172  ushort[] parsedVersion = null;
173  try
174  {
175  string[] splitVersion = version.Split(DELIM);
176  parsedVersion = new ushort[4];
177 
178  for (int i = 0; i < splitVersion.Length; i++)
179  {
180  if (splitVersion[i] == WILD_CARD)
181  {
182  parsedVersion[i] = upperBound ? WILD_CARD_CEIL : WILD_CARD_FLOOR;
183  }
184  else
185  {
186  parsedVersion[i] = ushort.Parse(splitVersion[i]);
187  }
188  }
189 
190  //End with Floor for unspecified elements
191  //This way, the range "100.3" to "101.1" means [100.3.0.0 , 101.1.0.0)
192  for (int i = splitVersion.Length; i < parsedVersion.Length; i++)
193  {
194  parsedVersion[i] = 0;
195  }
196 
197  }
198  catch (Exception excp)
199  {
200  throw new ArgumentException(
201  message: $"Bad version parsing: {version}",
202  innerException: excp);
203  }
204 
205  return new ApplicationVersion(parsedVersion);
206  }

◆ Equals() [1/2]

override bool BGC.Utility.ApplicationVersion.Equals ( object  obj)
inline

Definition at line 212 of file ApplicationVersion.cs.

213  {
214  if (ReferenceEquals(null, obj))
215  {
216  return false;
217  }
218 
219  if (ReferenceEquals(this, obj))
220  {
221  return true;
222  }
223 
224  return obj.GetType() == GetType() && Equals((ApplicationVersion)obj);
225  }
override bool Equals(object obj)

◆ Equals() [2/2]

bool BGC.Utility.ApplicationVersion.Equals ( in ApplicationVersion  other)
inline

Definition at line 247 of file ApplicationVersion.cs.

248  {
249  if (ReferenceEquals(this, other))
250  {
251  return true;
252  }
253 
254  for (int i = 0; i < 4; i++)
255  {
256  //Cascade down version significance
257  //Only stop on a mismatch
258  if (this[i] != other[i])
259  {
260  return false;
261  }
262  }
263 
264  //Equal
265  return true;
266  }

◆ GetHashCode()

override int BGC.Utility.ApplicationVersion.GetHashCode ( )
inline

Definition at line 227 of file ApplicationVersion.cs.

228  {
229  unchecked
230  {
231  int hashCode = 0;
232 
233  for (int i = 0; i < 4; i++)
234  {
235  hashCode = (hashCode * 397) ^ this[i].GetHashCode();
236  }
237 
238  return hashCode;
239  }
240  }

◆ IsNull()

bool BGC.Utility.ApplicationVersion.IsNull ( )

◆ MatchesPattern()

bool BGC.Utility.ApplicationVersion.MatchesPattern ( string  pattern)
inline

Compares the version number to one that supports asterisk wildcards Unspecified elements are considered wildcards

Parameters
pattern
Returns

Definition at line 135 of file ApplicationVersion.cs.

136  {
137  try
138  {
139  string[] splitVersion = pattern.Split(DELIM);
140 
141  if (splitVersion.Length > 4)
142  {
143  throw new ArgumentException(
144  message: $"Version pattern had too many version elements: {splitVersion.Length}",
145  paramName: nameof(pattern));
146  }
147 
148  for (int i = 0; i < splitVersion.Length; i++)
149  {
150  if (splitVersion[i] != WILD_CARD)
151  {
152  if (this[i] != ushort.Parse(splitVersion[i]))
153  {
154  return false;
155  }
156  }
157  }
158 
159  }
160  catch (Exception excp)
161  {
162  throw new ArgumentException(
163  message: $"Bad version parsing: {pattern}",
164  innerException: excp);
165  }
166 
167  return true;
168  }

◆ operator ApplicationVersion()

static implicit BGC.Utility.ApplicationVersion.operator ApplicationVersion ( string  version)
static

◆ operator string()

static implicit BGC.Utility.ApplicationVersion.operator string ( in ApplicationVersion  version)
static

◆ operator!=()

static bool BGC.Utility.ApplicationVersion.operator!= ( in ApplicationVersion  lVersion,
in ApplicationVersion  rVersion 
)
static

◆ operator<()

static bool BGC.Utility.ApplicationVersion.operator< ( in ApplicationVersion  lVersion,
in ApplicationVersion  rVersion 
)
inlinestatic

Definition at line 268 of file ApplicationVersion.cs.

269  {
270  for (int i = 0; i < 4; i++)
271  {
272  //Cascade down version significance
273  //Only stop on a mismatch
274  if (lVersion[i] != rVersion[i])
275  {
276  return lVersion[i] < rVersion[i];
277  }
278  }
279 
280  //Equal
281  return false;
282  }

◆ operator<=()

static bool BGC.Utility.ApplicationVersion.operator<= ( in ApplicationVersion  lVersion,
in ApplicationVersion  rVersion 
)
inlinestatic

Definition at line 300 of file ApplicationVersion.cs.

301  {
302  for (int i = 0; i < 4; i++)
303  {
304  //Cascade down version significance
305  //Only stop on a mismatch
306  if (lVersion[i] != rVersion[i])
307  {
308  return lVersion[i] < rVersion[i];
309  }
310  }
311 
312  //Equal
313  return true;
314  }

◆ operator==()

static bool BGC.Utility.ApplicationVersion.operator== ( in ApplicationVersion  lVersion,
in ApplicationVersion  rVersion 
)
inlinestatic

Definition at line 332 of file ApplicationVersion.cs.

333  {
334  if (ReferenceEquals(lVersion, rVersion))
335  {
336  return true;
337  }
338 
339  return lVersion.Equals(rVersion);
340  }

◆ operator>()

static bool BGC.Utility.ApplicationVersion.operator> ( in ApplicationVersion  lVersion,
in ApplicationVersion  rVersion 
)
inlinestatic

Definition at line 284 of file ApplicationVersion.cs.

285  {
286  for (int i = 0; i < 4; i++)
287  {
288  //Cascade down version significance
289  //Only stop on a mismatch
290  if (lVersion[i] != rVersion[i])
291  {
292  return lVersion[i] > rVersion[i];
293  }
294  }
295 
296  //Equal
297  return false;
298  }

◆ operator>=()

static bool BGC.Utility.ApplicationVersion.operator>= ( in ApplicationVersion  lVersion,
in ApplicationVersion  rVersion 
)
inlinestatic

Definition at line 316 of file ApplicationVersion.cs.

317  {
318  for (int i = 0; i < 4; i++)
319  {
320  //Cascade down version significance
321  //Only stop on a mismatch
322  if (lVersion[i] != rVersion[i])
323  {
324  return lVersion[i] > rVersion[i];
325  }
326  }
327 
328  //Equal
329  return true;
330  }

◆ PrintVersionElement()

static string BGC.Utility.ApplicationVersion.PrintVersionElement ( ushort  versionElement)
inlinestaticprivate

Definition at line 354 of file ApplicationVersion.cs.

355  {
356  if (versionElement == WILD_CARD_CEIL)
357  {
358  return WILD_CARD;
359  }
360 
361  return versionElement.ToString();
362  }

◆ ToString()

override string BGC.Utility.ApplicationVersion.ToString ( )

Referenced by BGC.Tests.ApplicationVersionTests.TestApplicationVersionFeatures().

Here is the caller graph for this function:

Field Documentation

◆ Build

ushort BGC.Utility.ApplicationVersion.Build => this[2]

◆ DELIM

const char BGC.Utility.ApplicationVersion.DELIM = '.'
private

Definition at line 15 of file ApplicationVersion.cs.

◆ DELIM_STR

readonly string BGC.Utility.ApplicationVersion.DELIM_STR = DELIM.ToString()
staticprivate

Definition at line 16 of file ApplicationVersion.cs.

◆ Length

int BGC.Utility.ApplicationVersion.Length => versions.Length

Definition at line 44 of file ApplicationVersion.cs.

◆ Major

ushort BGC.Utility.ApplicationVersion.Major => this[0]

◆ Max

readonly ApplicationVersion BGC.Utility.ApplicationVersion.Max = new ApplicationVersion(maxVersion)
static

Definition at line 22 of file ApplicationVersion.cs.

◆ maxVersion

readonly ushort [] BGC.Utility.ApplicationVersion.maxVersion = new ushort[] { ushort.MaxValue, ushort.MaxValue, ushort.MaxValue, ushort.MaxValue }
staticprivate

Definition at line 19 of file ApplicationVersion.cs.

◆ Minor

ushort BGC.Utility.ApplicationVersion.Minor => this[1]

◆ nullVersion

readonly ushort [] BGC.Utility.ApplicationVersion.nullVersion = new ushort[] { 0, 0, 0, 0 }
staticprivate

Definition at line 18 of file ApplicationVersion.cs.

◆ NullVersion

readonly ApplicationVersion BGC.Utility.ApplicationVersion.NullVersion = new ApplicationVersion(nullVersion)
static

Definition at line 21 of file ApplicationVersion.cs.

◆ Revision

ushort BGC.Utility.ApplicationVersion.Revision => this[3]

◆ versions

readonly ushort [] BGC.Utility.ApplicationVersion.versions
private

Definition at line 24 of file ApplicationVersion.cs.

◆ WILD_CARD

const string BGC.Utility.ApplicationVersion.WILD_CARD = "*"
private

Definition at line 12 of file ApplicationVersion.cs.

◆ WILD_CARD_CEIL

const ushort BGC.Utility.ApplicationVersion.WILD_CARD_CEIL = ushort.MaxValue
private

Definition at line 13 of file ApplicationVersion.cs.

◆ WILD_CARD_FLOOR

const ushort BGC.Utility.ApplicationVersion.WILD_CARD_FLOOR = ushort.MinValue
private

Definition at line 14 of file ApplicationVersion.cs.

Property Documentation

◆ this[int i]

ushort BGC.Utility.ApplicationVersion.this[int i]
get

Definition at line 27 of file ApplicationVersion.cs.


The documentation for this struct was generated from the following file: