cppDataPipeline 0.3.0
C++ Implementation of the FAIR Data Pipeline API
Loading...
Searching...
No Matches
semver.hxx
1
12#ifndef __FDP_SEMVER_HXX__
13#define __FDP_SEMVER_HXX__
14
15#include <ostream>
16#include <string>
17#include <vector>
18#include <stdexcept>
19
20namespace FairDataPipeline {
27namespace Versioning {
28
39enum class meta_tag {
40 ALPHA,
41 BETA,
43 RELEASE
44};
45
54class version {
55public:
61 version() = default;
62
81 version(unsigned int major, unsigned int minor, unsigned int patch,
82 meta_tag tag = meta_tag::RELEASE, unsigned int tag_v = 1E9,
83 std::string meta_data = "")
84 : major_(major), minor_(minor), patch_(patch), meta_tag_(tag),
85 tag_v_(tag_v), meta_data_(meta_data) {}
86
87 version(const std::string version_str);
88
89 version(const version &v)
90 : major_(v.major_), minor_(v.minor_), patch_(v.patch_),
91 meta_tag_(v.meta_tag_), tag_v_(v.tag_v_), meta_data_(v.meta_data_) {}
92
93 void bump_major() { major_ += 1; }
94 void bump_minor() { minor_ += 1; }
95 void bump_patch() { patch_ += 1; }
96
97 void alpha() {
98 tag_v_ = (meta_tag_ == meta_tag::ALPHA) ? tag_v_ + 1 : 0;
99 meta_tag_ = meta_tag::ALPHA;
100 }
101
102 void beta() {
103 tag_v_ = (meta_tag_ == meta_tag::BETA) ? tag_v_ + 1 : 0;
104 meta_tag_ = meta_tag::BETA;
105 }
106
107 void prerelease() {
108 tag_v_ = (meta_tag_ == meta_tag::RELEASE_CANDIDATE) ? tag_v_ + 1 : 0;
109 meta_tag_ = meta_tag::RELEASE_CANDIDATE;
110 }
111
112 void release() {
113 tag_v_ = 1E9;
114 meta_tag_ = meta_tag::RELEASE;
115 }
116
117 std::string to_string() const {
118 std::string out_str_;
119
120 out_str_ += std::to_string(major_) + ".";
121 out_str_ += std::to_string(minor_) + ".";
122 out_str_ += std::to_string(patch_);
123
124 if (meta_tag_ != meta_tag::RELEASE) {
125 out_str_ += "-" + get_tag_str();
126
127 if (tag_v_ != 1E9) {
128 out_str_ += std::to_string(tag_v_);
129 }
130 }
131 if (!meta_data_.empty()) {
132 out_str_ += "+" + meta_data_;
133 }
134
135 return out_str_;
136 }
137
138 friend std::ostream &operator<<(std::ostream &os, const version &v) {
139 os << v.to_string();
140 return os;
141 }
142
143 friend bool operator<(const version &v1, const version &v2) {
144 if (v1.major_ < v2.major_)
145 return true;
146 else if (v2.major_ < v1.major_)
147 return false;
148 if (v1.minor_ < v2.minor_)
149 return true;
150 else if (v2.minor_ < v1.minor_)
151 return false;
152 if (v1.patch_ < v2.patch_)
153 return true;
154 else if (v2.patch_ < v1.patch_)
155 return false;
156 if (v1.meta_tag_ < v2.meta_tag_)
157 return true;
158 else if (v2.meta_tag_ < v1.meta_tag_)
159 return false;
160 if (v1.tag_v_ < v2.tag_v_)
161 return true;
162 else if (v2.tag_v_ < v1.tag_v_)
163 return false;
164 return false;
165 }
166
167 friend bool operator>(const version &v1, const version &v2) {
168 return v2 < v1;
169 }
170
171 friend bool operator==(const version &v1, const version &v2) {
172 bool same_ = v1.major_ == v2.major_;
173 same_ = same_ && (v1.minor_ == v2.minor_);
174 same_ = same_ && (v1.patch_ == v2.patch_);
175 same_ = same_ && (v1.meta_tag_ == v2.meta_tag_);
176 same_ = same_ && (v1.tag_v_ == v2.tag_v_);
177 same_ = same_ && (v1.meta_data_ == v2.meta_data_);
178 return same_;
179 }
180
181 friend bool operator!=(const version &v1, const version &v2) {
182 return !(v1 == v2);
183 }
184
185 friend bool operator<=(const version &v1, const version &v2) {
186 return (v1 == v2) || v1 < v2;
187 }
188
189 friend bool operator>=(const version &v1, const version &v2) {
190 return (v1 == v2) || v1 > v2;
191 }
192
193private:
194 unsigned int major_ = 0;
195 unsigned int minor_ = 1;
196 unsigned int patch_ = 0;
197 meta_tag meta_tag_ = meta_tag::RELEASE;
198 unsigned int tag_v_ = 1E9;
199 std::string meta_data_ = "";
200 std::string get_tag_str() const;
201};
202}; // namespace Versioning
203}; // namespace FairDataPipeline
204
205#endif
class describing the semantic version of an object
Definition: semver.hxx:54
version()=default
Construct a new version with the default start value of 0.1.0
version(unsigned int major, unsigned int minor, unsigned int patch, meta_tag tag=meta_tag::RELEASE, unsigned int tag_v=1E9, std::string meta_data="")
Construct a new version from integer representation.
Definition: semver.hxx:81
meta_tag
the allowed tags for versions describing version state
Definition: semver.hxx:39