Loading...
Searching...
No Matches
vectorAccessor.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_EXEC_VDF_VECTOR_ACCESSOR_H
8#define PXR_EXEC_VDF_VECTOR_ACCESSOR_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/exec/vdf/boxedContainer.h"
13#include "pxr/exec/vdf/compressedIndexMapping.h"
14#include "pxr/exec/vdf/vectorData.h"
15
17
20
21PXR_NAMESPACE_OPEN_SCOPE
22
26template<typename T>
28{
29public:
30 static_assert(
31 !Vdf_IsBoxedContainer<T>,
32 "Vdf_VectorAccessor does not provide access to boxed containers");
33
37 _numValues(0),
38 _boxed(false)
39 {}
40
44 Vdf_VectorData *data,
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;
59 if (ARCH_UNLIKELY(info.compressedIndexMapping)) {
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
88 bool IsEmpty() const { return _numValues == 0; }
89
92 size_t GetNumValues() const { return _numValues; }
93
97 bool IsBoxed() const { return _boxed; }
98
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
108private:
109 size_t _numValues;
110 T *_data;
111 Vdf_CompressedIndexMapping *_indexMapping;
112 mutable size_t _indexMappingHint;
113 bool _boxed;
114
115};
116
117PXR_NAMESPACE_CLOSE_SCOPE
118
119#endif
Low-level utilities for informing users of various internal and external diagnostic conditions.
This simple container stores multiple values that flow through the network as a single data flow elem...
const T * data() const
Returns a pointer to the immutable data elements.
This collection of IndexBlockMappings is all the info required to take a logical index into a compres...
Accessor class.
Vdf_VectorAccessor()
Default constructor.
bool IsBoxed() const
Returns true if this accessor is providing element-wise access into a boxed container.
T & operator[](size_t idx) const
Returns a reference to an element.
bool IsEmpty() const
Returns true if vector is empty.
Vdf_VectorAccessor(Vdf_VectorData *data, const Vdf_VectorData::Info &info)
Constructor.
size_t GetNumValues() const
Returns size of the vector, ie.
Abstract base class for storing data in a VdfVector.
Definition: vectorData.h:27
Demangle C++ typenames generated by the typeid() facility.
std::string ArchGetDemangled()
Return demangled RTTI generated-type name.
Definition: demangle.h:86
#define TF_DEV_AXIOM(cond)
The same as TF_AXIOM, but compiled only in dev builds.
Definition: diagnostic.h:205
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
Definition: diagnostic.h:91
Safely compare C++ RTTI type structures.
bool TfSafeTypeCompare(const std::type_info &t1, const std::type_info &t2)
Safely compare std::type_info structures.