Loading...
Searching...
No Matches
fileVersion.h
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_USD_SDF_FILE_VERSION_H
8#define PXR_USD_SDF_FILE_VERSION_H
9
10#include "pxr/pxr.h"
11#include "pxr/usd/sdf/api.h"
12
13#include <cstdint>
14#include <cstdlib>
15#include <string>
16
17PXR_NAMESPACE_OPEN_SCOPE
18
19// Hold parse, and compare file format versions. Used by both crate and
20// text file formats.
21class SdfFileVersion {
22public:
23 // Not named 'major' since that's a macro name conflict on POSIXes.
24 uint8_t majver, minver, patchver;
25
26 constexpr SdfFileVersion()
27 : SdfFileVersion(0,0,0) {}
28
29 constexpr SdfFileVersion(uint8_t majver, uint8_t minver, uint8_t patchver)
30 : majver(majver), minver(minver), patchver(patchver) {}
31
32 // For constructing from crate files header data. version is required to
33 // contain at least 3 values for the major, minor, and patch.
34 explicit SdfFileVersion(const uint8_t version[])
35 : SdfFileVersion(version[0], version[1], version[2]) {}
36
38 SDF_API
39 static SdfFileVersion FromString(char const *str);
40
42 static SdfFileVersion FromString(std::string str) {
43 return FromString(str.c_str());
44 }
45
49 constexpr uint32_t AsInt() const {
50 return static_cast<uint32_t>(majver) << 16 |
51 static_cast<uint32_t>(minver) << 8 |
52 static_cast<uint32_t>(patchver);
53 }
54
57 SDF_API
58 std::string AsString() const;
59
62 SDF_API
63 std::string AsFullString() const;
64
66 bool IsValid() const { return AsInt() != 0; }
67
68 explicit operator bool() const {
69 return IsValid();
70 }
71
72 // Return true if fileVer has the same major version as this, and has a
73 // lesser or same minor version. Patch version irrelevant, since the
74 // versioning scheme specifies that patch level changes are
75 // forward-compatible.
76 bool CanRead(SdfFileVersion const &fileVer) const {
77 return fileVer.majver == majver && fileVer.minver <= minver;
78 }
79
80 // Return true if fileVer has the same major version as this, and has a
81 // lesser minor version, or has the same minor version and a lesser or
82 // equal patch version.
83 bool CanWrite(SdfFileVersion const &fileVer) const {
84 return fileVer.majver == majver &&
85 (fileVer.minver < minver ||
86 (fileVer.minver == minver && fileVer.patchver <= patchver));
87 }
88
89#define LOGIC_OP(op) \
90 constexpr bool operator op(SdfFileVersion const &other) const { \
91 return AsInt() op other.AsInt(); \
92 }
93 LOGIC_OP(==); LOGIC_OP(!=);
94 LOGIC_OP(<); LOGIC_OP(>);
95 LOGIC_OP(<=); LOGIC_OP(>=);
96#undef LOGIC_OP
97};
98
99PXR_NAMESPACE_CLOSE_SCOPE
100
101#endif // PXR_USD_SDF_FILE_VERSION_H