HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vectorAccessor.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_EXEC_VDF_VECTOR_ACCESSOR_H
8 #define PXR_EXEC_VDF_VECTOR_ACCESSOR_H
9 
10 #include "pxr/pxr.h"
11 
15 
16 #include "pxr/base/arch/demangle.h"
17 
19 #include "pxr/base/tf/diagnostic.h"
20 
22 
23 /// Accessor class. This is used to provide fast access with making sure
24 /// that the type checks are done no matter what.
25 ///
26 template<typename T>
28 {
29 public:
30  static_assert(
31  !Vdf_IsBoxedContainer<T>,
32  "Vdf_VectorAccessor does not provide access to boxed containers");
33 
34  /// Default constructor.
35  ///
37  _numValues(0),
38  _boxed(false)
39  {}
40 
41  /// Constructor.
42  ///
45  const Vdf_VectorData::Info &info)
46  : _boxed(info.layout == Vdf_VectorData::Info::Layout::Boxed) {
47 
48  const std::type_info &haveType = data->GetTypeInfo();
49 
50  if (ARCH_UNLIKELY(!TfSafeTypeCompare(haveType, typeid(T)))) {
52  "Invalid type. Vector is holding %s, tried to use as %s",
53  ArchGetDemangled(haveType).c_str(),
54  ArchGetDemangled(typeid(T)).c_str());
55  }
56 
57  // Setup the compressed index mapping, if any.
58  _indexMapping = nullptr;
60  _numValues = info.size;
61  _data = (T *)info.data;
62  _indexMapping = info.compressedIndexMapping;
63  _indexMappingHint = 0;
64  }
65 
66  // Access for vector data that is not boxed.
67  else if (!_boxed) {
68  _numValues = info.size;
69  _data = (T *)info.data - info.first;
70  }
71 
72  // Access for boxed vector data.
73  else {
74  // We expect exactly a single data element in this case.
76  !info.compressedIndexMapping &&
77  info.size == 1 && info.first == 0 && info.last == 0);
78 
79  using BoxedVectorType = Vdf_BoxedContainer<T>;
80  BoxedVectorType *boxedVector = (BoxedVectorType *)info.data;
81  _numValues = boxedVector->size();
82  _data = boxedVector->data();
83  }
84  }
85 
86  /// Returns true if vector is empty.
87  ///
88  bool IsEmpty() const { return _numValues == 0; }
89 
90  /// Returns size of the vector, ie. the number of values it holds.
91  ///
92  size_t GetNumValues() const { return _numValues; }
93 
94  /// Returns \c true if this accessor is providing element-wise access into
95  /// a boxed container.
96  ///
97  bool IsBoxed() const { return _boxed; }
98 
99  /// Returns a reference to an element.
100  ///
101  T &operator[](size_t idx) const {
102  if (ARCH_UNLIKELY(_indexMapping)) {
103  idx = _indexMapping->FindDataIndex(idx, &_indexMappingHint);
104  }
105  return _data[idx];
106  }
107 
108 private:
109  size_t _numValues;
110  T *_data;
111  Vdf_CompressedIndexMapping *_indexMapping;
112  mutable size_t _indexMappingHint;
113  bool _boxed;
114 
115 };
116 
118 
119 #endif
size_t GetNumValues() const
Vdf_VectorAccessor(Vdf_VectorData *data, const Vdf_VectorData::Info &info)
bool IsEmpty() const
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
Vdf_CompressedIndexMapping * compressedIndexMapping
Definition: vectorData.h:205
ARCH_API std::string ArchGetDemangled(const std::string &typeName)
Abstract base class for storing data in a VdfVector.
Definition: vectorData.h:26
#define ARCH_UNLIKELY(x)
Definition: hints.h:30
#define TF_DEV_AXIOM(cond)
#define TF_FATAL_ERROR
virtual const std::type_info & GetTypeInfo() const =0
VDF_API size_t FindDataIndex(size_t logicalIdx, size_t *blockHint) const
T & operator[](size_t idx) const
bool IsBoxed() const
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
OIIO_UTIL_API const char * c_str(string_view str)
PXR_NAMESPACE_OPEN_SCOPE bool TfSafeTypeCompare(const std::type_info &t1, const std::type_info &t2)
Definition: format.h:1821