blob: 9257b22cfcebc018894afd63b76f1e53fda72c5c [file] [log] [blame]
Nikhil Rajd88e47c2019-08-19 10:04:23 +01001//
Jim Flynnbbfe6032020-07-20 16:57:44 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
Nikhil Rajd88e47c2019-08-19 10:04:23 +01003// SPDX-License-Identifier: MIT
4//
5#pragma once
6
Aron Virginas-Tare898db92019-08-22 12:56:34 +01007#include <cstdint>
8#include <string>
9#include <ostream>
10#include <sstream>
Nikhil Rajd88e47c2019-08-19 10:04:23 +010011
Jim Flynnbbfe6032020-07-20 16:57:44 +010012namespace arm
Nikhil Rajd88e47c2019-08-19 10:04:23 +010013{
14
Jim Flynnbbfe6032020-07-20 16:57:44 +010015namespace pipe
Nikhil Rajd88e47c2019-08-19 10:04:23 +010016{
17
Aron Virginas-Tare898db92019-08-22 12:56:34 +010018constexpr inline uint32_t EncodeVersion(uint32_t major, uint32_t minor, uint32_t patch)
19{
20 return (major << 22) | (minor << 12) | patch;
21}
Nikhil Rajd88e47c2019-08-19 10:04:23 +010022
23// Encodes a semantic version https://semver.org/ into a 32 bit integer in the following fashion
24//
25// bits 22:31 major: Unsigned 10-bit integer. Major component of the schema version number.
26// bits 12:21 minor: Unsigned 10-bit integer. Minor component of the schema version number.
27// bits 0:11 patch: Unsigned 12-bit integer. Patch component of the schema version number.
28//
29class Version
30{
31public:
32 Version(uint32_t encodedValue)
33 {
34 m_Major = (encodedValue >> 22) & 1023;
35 m_Minor = (encodedValue >> 12) & 1023;
36 m_Patch = encodedValue & 4095;
37 }
38
Aron Virginas-Tare898db92019-08-22 12:56:34 +010039 Version(uint32_t major, uint32_t minor, uint32_t patch) :
40 m_Major(major),
41 m_Minor(minor),
42 m_Patch(patch)
43 {}
Nikhil Rajd88e47c2019-08-19 10:04:23 +010044
45 uint32_t GetEncodedValue()
46 {
Aron Virginas-Tare898db92019-08-22 12:56:34 +010047 return EncodeVersion(m_Major, m_Minor, m_Patch);
Nikhil Rajd88e47c2019-08-19 10:04:23 +010048 }
49
Aron Virginas-Tare898db92019-08-22 12:56:34 +010050 uint32_t GetMajor() { return m_Major; }
51 uint32_t GetMinor() { return m_Minor; }
52 uint32_t GetPatch() { return m_Patch; }
53
54 bool operator==(const Version& other) const
55 {
56 return m_Major == other.m_Major && m_Minor == other.m_Minor && m_Patch == other.m_Patch;
57 }
58
59 std::string ToString() const
60 {
61 constexpr char separator = '.';
62
63 std::stringstream stringStream;
64 stringStream << m_Major << separator << m_Minor << separator << m_Patch;
65
66 return stringStream.str();
67 }
Nikhil Rajd88e47c2019-08-19 10:04:23 +010068
69private:
70 uint32_t m_Major;
71 uint32_t m_Minor;
72 uint32_t m_Patch;
73};
74
Aron Virginas-Tare898db92019-08-22 12:56:34 +010075inline std::ostream& operator<<(std::ostream& os, const Version& version)
76{
77 os << version.ToString();
78 return os;
79}
80
Jim Flynnbbfe6032020-07-20 16:57:44 +010081} // namespace pipe
Aron Virginas-Tare898db92019-08-22 12:56:34 +010082
Jim Flynnbbfe6032020-07-20 16:57:44 +010083} // namespace arm