ChimeraTK-DeviceAccess  03.18.00
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 <string>
9 
10 namespace ChimeraTK {
11 
12  /********************************************************************************************************************/
13 
23  class VersionNumber {
24  public:
28  VersionNumber() : _value(nextVersionNumber()), _time(std::chrono::system_clock::now()) {}
29 
31  VersionNumber(const VersionNumber& other) = default;
32 
34  VersionNumber& operator=(const VersionNumber& other) = default;
35 
37  explicit VersionNumber(std::chrono::system_clock::time_point timestamp);
38 
45  explicit VersionNumber(std::nullptr_t) : _value(0) {}
46 
48  [[nodiscard]] std::chrono::time_point<std::chrono::system_clock> getTime() const { return _time; }
49 
56  bool operator==(const VersionNumber& other) const { return _value == other._value; }
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 
67  explicit operator std::string() const;
68 
69  private:
73  std::uint64_t _value;
74 
78  std::chrono::time_point<std::chrono::system_clock> _time;
79 
87  static uint64_t nextVersionNumber() { return ++_lastGeneratedVersionNumber; }
88 
92  static std::atomic<uint64_t> _lastGeneratedVersionNumber;
93 
94  friend std::ostream& operator<<(std::ostream& stream, const VersionNumber& version);
95  };
96 
97  /********************************************************************************************************************/
98 
99  inline VersionNumber::VersionNumber(std::chrono::system_clock::time_point timestamp)
100  : _value(nextVersionNumber()), _time(timestamp) {}
101 
102  /********************************************************************************************************************/
103  /********************************************************************************************************************/
104 
106  std::ostream& operator<<(std::ostream& stream, const VersionNumber& version);
107 
108  /********************************************************************************************************************/
109 
110 } // namespace ChimeraTK
ChimeraTK::VersionNumber::VersionNumber
VersionNumber(std::nullptr_t)
Create null version number, which is guaranteed to be smaller than all version numbers generated with...
Definition: VersionNumber.h:45
ChimeraTK::VersionNumber::VersionNumber
VersionNumber()
Default constructor: Generate new unique version number with current time as time stamp.
Definition: VersionNumber.h:28
ChimeraTK::testable_rebot_sleep::now
boost::chrono::steady_clock::time_point now()
Definition: testableRebotSleep.cc:7
ChimeraTK::VersionNumber::operator>=
bool operator>=(const VersionNumber &other) const
Definition: VersionNumber.h:60
ChimeraTK::VersionNumber::operator==
bool operator==(const VersionNumber &other) const
Comparison operators.
Definition: VersionNumber.h:56
ChimeraTK::VersionNumber::operator=
VersionNumber & operator=(const VersionNumber &other)=default
Copy the full state of another VersionNumber object.
ChimeraTK::VersionNumber::operator<
bool operator<(const VersionNumber &other) const
Definition: VersionNumber.h:59
ChimeraTK::VersionNumber::getTime
std::chrono::time_point< std::chrono::system_clock > getTime() const
Return the time stamp associated with this version number.
Definition: VersionNumber.h:48
ChimeraTK::operator<<
std::ostream & operator<<(std::ostream &stream, const DataDescriptor::FundamentalType &fundamentalType)
Definition: DataDescriptor.cpp:195
ChimeraTK::VersionNumber
Class for generating and holding version numbers without exposing a numeric representation.
Definition: VersionNumber.h:23
ChimeraTK::VersionNumber::operator!=
bool operator!=(const VersionNumber &other) const
Definition: VersionNumber.h:57
ChimeraTK::VersionNumber::operator<<
friend std::ostream & operator<<(std::ostream &stream, const VersionNumber &version)
Stream operator passing the human readable representation to an ostream.
Definition: VersionNumber.cc:20
ChimeraTK::VersionNumber::operator<=
bool operator<=(const VersionNumber &other) const
Definition: VersionNumber.h:61
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::VersionNumber::operator>
bool operator>(const VersionNumber &other) const
Definition: VersionNumber.h:58