BeVersion.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
10 #include <Bentley/WString.h>
11 
13 
14 #define VERSION_FORMAT "%" PRIu16 ".%" PRIu16 ".%" PRIu16 ".%" PRIu16
15 #define VERSION_FORMAT_MAJOR_MINOR "%" PRIu16 ".%" PRIu16
16 #define VERSION_PARSE_FORMAT "%d.%d.%d.%d"
17 
18 typedef struct BeVersion& BeVersionR;
19 typedef const struct BeVersion& BeVersionCR;
20 
21 //=======================================================================================
23 // @bsiclass Bentley Systems
24 //=======================================================================================
25 struct BeVersion
26  {
27 private:
28  uint16_t m_major;
29  uint16_t m_minor;
30  uint16_t m_sub1;
31  uint16_t m_sub2;
32 
33 public:
34  enum Mask : uint64_t
35  {
36  VERSION_Major = (((uint64_t) 0xffff) << 48),
37  VERSION_Minor = (((uint64_t) 0xffff) << 32),
38  VERSION_Sub1 = (((uint64_t) 0xffff) << 16),
39  VERSION_Sub2 = ((uint64_t) 0xffff),
40  VERSION_MajorMinor = (VERSION_Major | VERSION_Minor),
41  VERSION_MajorMinorSub1 = (VERSION_Major | VERSION_Minor | VERSION_Sub1),
42  VERSION_All = (VERSION_Major | VERSION_Minor | VERSION_Sub1 | VERSION_Sub2),
43  };
44 
45  BeVersion() : BeVersion(0, 0, 0, 0) {}
46  BeVersion(uint16_t major, uint16_t minor) : BeVersion(major, minor, 0, 0) {}
47  BeVersion(uint16_t major, uint16_t minor, uint16_t sub1, uint16_t sub2) : m_major(major), m_minor(minor), m_sub1(sub1), m_sub2(sub2) {}
48  BeVersion(int major, int minor) : BeVersion((uint16_t) major, (uint16_t) minor) {}
53  BeVersion(Utf8CP versionStr, Utf8CP format = VERSION_PARSE_FORMAT) : BeVersion() { FromString(versionStr, format); }
54 
57  uint16_t GetMajor() const { return m_major; }
58 
61  uint16_t GetMinor() const { return m_minor; }
62 
65  uint16_t GetSub1() const { return m_sub1; }
66 
68  uint16_t GetSub2() const { return m_sub2; }
69 
70  uint64_t GetInt64(Mask mask) const { return mask & ((((uint64_t) m_major) << 48) | (((uint64_t) m_minor) << 32) | ((uint64_t) m_sub1 << 16) | (uint64_t) m_sub2); }
71  int CompareTo(BeVersionCR other, Mask mask = VERSION_All) const { return (GetInt64(mask) == other.GetInt64(mask)) ? 0 : (GetInt64(mask) > other.GetInt64(mask) ? 1 : -1); }
72 
73  bool operator==(BeVersionCR rhs) const { return CompareTo(rhs) == 0; }
74  bool operator!=(BeVersionCR rhs) const { return CompareTo(rhs) != 0; }
75  bool operator<(BeVersionCR rhs) const { return CompareTo(rhs) < 0; }
76  bool operator<=(BeVersionCR rhs) const { return CompareTo(rhs) <= 0; }
77  bool operator>(BeVersionCR rhs) const { return CompareTo(rhs) > 0; }
78  bool operator>=(BeVersionCR rhs) const { return CompareTo(rhs) >= 0; }
79 
80  bool IsEmpty() const { return 0 == GetInt64(VERSION_All); }
81 
85  Utf8String ToString(Utf8CP format = VERSION_FORMAT) const { return Utf8PrintfString(format, m_major, m_minor, m_sub1, m_sub2); }
86 
89  Utf8String ToMajorMinorString(Utf8CP format = VERSION_FORMAT_MAJOR_MINOR) const { return Utf8PrintfString(format, m_major, m_minor); }
90 
94  void FromString(Utf8CP versionStr, Utf8CP format = VERSION_PARSE_FORMAT)
95  {
96  int major = 0, minor = 0, sub1 = 0, sub2 = 0;
97  sscanf(versionStr, format, &major, &minor, &sub1, &sub2);
98  m_major = (uint16_t) (0xFFFF & major);
99  m_minor = (uint16_t) (0xFFFF & minor);
100  m_sub1 = (uint16_t) (0xFFFF & sub1);
101  m_sub2 = (uint16_t) (0xFFFF & sub2);
102  }
103  };
104 
Contains a UTF-8 encoded string.
Definition: WString.h:275
Utf8String ToMajorMinorString(Utf8CP format="%"PRIu16".%"PRIu16) const
Generates a 2-part version string from this BeVersion that only uses the major and minor version numb...
Definition: BeVersion.h:89
bool operator<(BeVersionCR rhs) const
Definition: BeVersion.h:75
Construct a Utf8String by creating a formatted string.
Definition: WString.h:414
void FromString(Utf8CP versionStr, Utf8CP format="%d.%d.%d.%d")
Parses version string into a BeVersion.
Definition: BeVersion.h:94
#define VERSION_FORMAT
Definition: BeVersion.h:14
BeVersion(uint16_t major, uint16_t minor)
Definition: BeVersion.h:46
uint16_t GetMajor() const
Get the Major version.
Definition: BeVersion.h:57
BeVersion(int major, int minor)
Definition: BeVersion.h:48
bool operator!=(BeVersionCR rhs) const
Definition: BeVersion.h:74
uint16_t GetSub2() const
Get the Sub2 version. Changes to this number mean new features were added, but older versions may con...
Definition: BeVersion.h:68
int CompareTo(BeVersionCR other, Mask mask=VERSION_All) const
Definition: BeVersion.h:71
uint16_t GetSub1() const
Get the Sub1 version.
Definition: BeVersion.h:65
BeVersion()
Definition: BeVersion.h:45
BeVersion(Utf8CP versionStr, Utf8CP format="%d.%d.%d.%d")
Constructs a BeVersion from a version string.
Definition: BeVersion.h:53
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
bool operator>(BeVersionCR rhs) const
Definition: BeVersion.h:77
Mask
Definition: BeVersion.h:34
Utf8String ToString(Utf8CP format="%"PRIu16".%"PRIu16".%"PRIu16".%"PRIu16) const
Generates a string from this BeVersion.
Definition: BeVersion.h:85
unsigned long long uint64_t
Definition: Bentley.r.h:95
uint16_t GetMinor() const
Get the Minor version.
Definition: BeVersion.h:61
bool operator<=(BeVersionCR rhs) const
Definition: BeVersion.h:76
#define VERSION_FORMAT_MAJOR_MINOR
Definition: BeVersion.h:15
struct BeVersion & BeVersionR
Definition: BeVersion.h:18
BeVersion(uint16_t major, uint16_t minor, uint16_t sub1, uint16_t sub2)
Definition: BeVersion.h:47
unsigned short uint16_t
Definition: Bentley.r.h:91
#define VERSION_PARSE_FORMAT
Definition: BeVersion.h:16
Utf8Char const * Utf8CP
Definition: Bentley.h:229
bool operator==(BeVersionCR rhs) const
Definition: BeVersion.h:73
A 4-digit number that specifies version number.
Definition: BeVersion.h:25
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
uint64_t GetInt64(Mask mask) const
Definition: BeVersion.h:70
bool operator>=(BeVersionCR rhs) const
Definition: BeVersion.h:78
const struct BeVersion & BeVersionCR
Definition: BeVersion.h:19
bool IsEmpty() const
Definition: BeVersion.h:80

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.