ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
VersionNumber.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
5#include <atomic>
6#include <chrono>
7#include <cstdint>
8#include <format>
9#include <string>
10
11namespace ChimeraTK {
12
13 /********************************************************************************************************************/
14
25 public:
29 VersionNumber() : _value(nextVersionNumber()), _time(std::chrono::system_clock::now()) {}
30
32 VersionNumber(const VersionNumber& other) = default;
33
35 VersionNumber& operator=(const VersionNumber& other) = default;
36
38 explicit VersionNumber(std::chrono::system_clock::time_point timestamp);
39
46 explicit VersionNumber(std::nullptr_t) : _value(0) {}
47
49 [[nodiscard]] std::chrono::time_point<std::chrono::system_clock> getTime() const { return _time; }
50
57 bool operator==(const VersionNumber& other) const { return _value == other._value; }
58 bool operator!=(const VersionNumber& other) const { return _value != other._value; }
59 bool operator>(const VersionNumber& other) const { return _value > other._value; }
60 bool operator<(const VersionNumber& other) const { return _value < other._value; }
61 bool operator>=(const VersionNumber& other) const { return _value >= other._value; }
62 bool operator<=(const VersionNumber& other) const { return _value <= other._value; }
63
68 explicit operator std::string() const;
69
70 private:
74 std::uint64_t _value;
75
79 std::chrono::time_point<std::chrono::system_clock> _time;
80
88 static uint64_t nextVersionNumber() { return ++_lastGeneratedVersionNumber; }
89
93 static std::atomic<uint64_t> _lastGeneratedVersionNumber;
94
95 friend std::ostream& operator<<(std::ostream& stream, const VersionNumber& version);
96
97 template<class T, class CharT>
98 friend struct std::formatter;
99 };
100
101 /********************************************************************************************************************/
102
103 inline VersionNumber::VersionNumber(std::chrono::system_clock::time_point timestamp)
104 : _value(nextVersionNumber()), _time(timestamp) {}
105
106 /********************************************************************************************************************/
107 /********************************************************************************************************************/
108
110 std::ostream& operator<<(std::ostream& stream, const VersionNumber& version);
111
112 /********************************************************************************************************************/
113
114} // namespace ChimeraTK
115
116/**********************************************************************************************************************/
117
119template<class CharT>
120struct std::formatter<ChimeraTK::VersionNumber, CharT> {
121 bool printVersion{false};
122 bool printTime{false};
123
124 template<class ParseContext>
125 constexpr ParseContext::iterator parse(ParseContext& ctx) {
126 auto it = ctx.begin();
127 if(it == ctx.end()) {
128 return it;
129 }
130 if(*it == 'v') {
131 printVersion = true;
132 ++it;
133 }
134 if(*it == 't') {
135 printTime = true;
136 ++it;
137 }
138 if(it != ctx.end() && *it != '}') {
139 throw std::format_error("Invalid format args for ChimeraTK::VersionNumber.");
140 }
141 if(!printVersion && !printTime) {
142 // print version by default
143 printVersion = true;
144 }
145 return it;
146 }
147
148 template<typename FormatContext>
149 auto format(const ChimeraTK::VersionNumber& v, FormatContext& ctx) const {
150 std::string fmt;
151 if(printVersion) {
152 fmt = "v{0}";
153 }
154 if(printTime) {
155 fmt += "@{1}";
156 }
157 return std::vformat_to(ctx.out(), fmt, std::make_format_args(v._value, v._time));
158 }
159};
160
161/**********************************************************************************************************************/
Class for generating and holding version numbers without exposing a numeric representation.
bool operator>=(const VersionNumber &other) const
bool operator!=(const VersionNumber &other) const
std::chrono::time_point< std::chrono::system_clock > getTime() const
Return the time stamp associated with this version number.
VersionNumber(std::nullptr_t)
Create null version number, which is guaranteed to be smaller than all version numbers generated with...
bool operator==(const VersionNumber &other) const
Comparison operators.
VersionNumber & operator=(const VersionNumber &other)=default
Copy the full state of another VersionNumber object.
bool operator>(const VersionNumber &other) const
bool operator<=(const VersionNumber &other) const
VersionNumber()
Default constructor: Generate new unique version number with current time as time stamp.
friend struct std::formatter
VersionNumber(const VersionNumber &other)=default
Copy constructor.
friend std::ostream & operator<<(std::ostream &stream, const VersionNumber &version)
Stream operator passing the human readable representation to an ostream.
bool operator<(const VersionNumber &other) const
std::ostream & operator<<(std::ostream &stream, const DataDescriptor::FundamentalType &fundamentalType)
STL namespace.
auto format(const ChimeraTK::VersionNumber &v, FormatContext &ctx) const
constexpr ParseContext::iterator parse(ParseContext &ctx)