HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fileVersion.h
Go to the documentation of this file.
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 
18 
19 // Hold parse, and compare file format versions. Used by both crate and
20 // text file formats.
22 public:
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 
37  /// Create a version from a dot-separated c-style string, e.g. "1.2.3".
38  SDF_API
39  static SdfFileVersion FromString(char const *str);
40 
41  /// Create a version from a dot-separated std::string.
42  static SdfFileVersion FromString(std::string str) {
43  return FromString(str.c_str());
44  }
45 
46  /// Return a version number as a single 32-bit integer. From most to
47  /// least significant, the returned integer's bytes are 0,
48  /// major-version, minor-version, patch-version.
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 
55  /// Return a dotted decimal integer string for this version, the
56  /// patch version is excluded if it is 0, e.g. "1.0" or "1.2.3".
57  SDF_API
58  std::string AsString() const;
59 
60  /// Return a dotted decimal integer string for this version, the
61  /// patch version is excluded if it is 0, e.g. "1.0.0" or "1.2.3".
62  SDF_API
63  std::string AsFullString() const;
64 
65  /// Return true if this version is not zero in all components.
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 
100 
101 #endif // PXR_USD_SDF_FILE_VERSION_H
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
static SDF_API SdfFileVersion FromString(char const *str)
Create a version from a dot-separated c-style string, e.g. "1.2.3".
constexpr SdfFileVersion(uint8_t majver, uint8_t minver, uint8_t patchver)
Definition: fileVersion.h:29
static SdfFileVersion FromString(std::string str)
Create a version from a dot-separated std::string.
Definition: fileVersion.h:42
OutGridT const XformOp bool bool
bool CanWrite(SdfFileVersion const &fileVer) const
Definition: fileVersion.h:83
constexpr SdfFileVersion()
Definition: fileVersion.h:26
SDF_API std::string AsFullString() const
bool IsValid() const
Return true if this version is not zero in all components.
Definition: fileVersion.h:66
SDF_API std::string AsString() const
constexpr uint32_t AsInt() const
Definition: fileVersion.h:49
uint8_t patchver
Definition: fileVersion.h:24
GT_API const UT_StringHolder version
uint8_t minver
Definition: fileVersion.h:24
#define SDF_API
Definition: api.h:23
SdfFileVersion(const uint8_t version[])
Definition: fileVersion.h:34
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
bool CanRead(SdfFileVersion const &fileVer) const
Definition: fileVersion.h:76
uint8_t majver
Definition: fileVersion.h:24