Loading...
Searching...
No Matches
vectorImpl_Single.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_IMPL_SINGLE_H
8#define PXR_EXEC_VDF_VECTOR_IMPL_SINGLE_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/exec/vdf/api.h"
13#include "pxr/exec/vdf/boxedContainerTraits.h"
15#include "pxr/exec/vdf/fixedSizeHolder.h"
16#include "pxr/exec/vdf/forEachCommonType.h"
17#include "pxr/exec/vdf/mask.h"
18#include "pxr/exec/vdf/vectorDataTyped.h"
19#include "pxr/exec/vdf/vectorImpl_Empty.h"
20
22
23PXR_NAMESPACE_OPEN_SCOPE
24
27template<typename TYPE>
28class VDF_API_TYPE Vdf_VectorImplSingle final
29 : public Vdf_VectorDataTyped<TYPE>
30{
31 static_assert(
32 !Vdf_IsBoxedContainer<TYPE>,
33 "Only Vdf_VectorImplBoxed may hold boxed values");
34
35public:
36
37 Vdf_VectorImplSingle() = default;
38
39 explicit Vdf_VectorImplSingle(const TYPE &value) :
40 _data(value) {
41 }
42
43 explicit Vdf_VectorImplSingle(TYPE &&value) :
44 _data(std::move(value)) {
45 }
46
48 _data(rhs._data) {
49 }
50
52 _data(std::move(rhs._data)) {
53 }
54
55 ~Vdf_VectorImplSingle() override = default;
56
57 void MoveInto(Vdf_VectorData::DataHolder *destData) override
58 {
59 destData->Destroy();
60 destData->New< Vdf_VectorImplSingle >(std::move(*this));
61 }
62
63 void Clone(Vdf_VectorData::DataHolder *destData) const override
64 {
65 // XXX:optimization
66 // Here, since we have destData, we can dynamic_cast it to a
67 // Vdf_SingleElement and if it is, we can assign the element directly
68 // without having to delete and Clone(). So far that hasn't shown up
69 // in any profile.
70 destData->Destroy();
71 destData->New< Vdf_VectorImplSingle >(*this);
72 }
73
74 void CloneSubset(const VdfMask &mask,
75 Vdf_VectorData::DataHolder *destData) const override
76 {
77 // We only have one element, not much point in looking at the mask.
78 Clone(destData);
79 }
80
81 void Box(const VdfMask::Bits &bits,
82 Vdf_VectorData::DataHolder *destData) const override
83 {
84 // We should never box single values. Attempting to do so will yield
85 // either a copy of this impl, if the mask is suitable, or an empty
86 // impl. There is no circumstance which will yield a boxed impl.
87 TF_VERIFY(false, "Attempted to box single-element vector");
88
89 if (bits.GetSize() == 1 && bits.AreAllSet()) {
90 destData->Destroy();
91 destData->New< Vdf_VectorImplSingle >(*this);
92 } else {
93 destData->Destroy();
94 destData->New< Vdf_VectorImplEmpty<TYPE> >(1);
95 }
96 }
97
98 void Merge(const VdfMask::Bits &bits,
99 Vdf_VectorData::DataHolder *destData) const override
100 {
101 if (bits.AreAllSet()) {
102 Clone(destData);
103 }
104 }
105
106 size_t GetSize() const override
107 {
108 return 1;
109 }
110
111 size_t GetNumStoredElements() const override
112 {
113 return 1;
114 }
115
116 size_t EstimateElementMemory() const override
117 {
118 // Clients of execution may overload VdfEstimateSize to provide a more
119 // accurate estimate based on the held value.
120 return VdfEstimateSize(_data.Get());
121 }
122
123 Vdf_VectorData::Info GetInfo() override
124 {
125 return Vdf_VectorData::Info(
126 /* data = */ &_data.GetMutable(),
127 /* size = */ 1);
128 }
129
130private :
131
133};
134
135#define VDF_DECLARE_EXTERN_VECTOR_IMPL_SINGLE(type) \
136 extern template class VDF_API_TYPE Vdf_VectorImplSingle<type>;
137VDF_FOR_EACH_COMMON_TYPE(VDF_DECLARE_EXTERN_VECTOR_IMPL_SINGLE)
138#undef VDF_DECLARE_EXTERN_VECTOR_IMPL_SINGLE
139
140PXR_NAMESPACE_CLOSE_SCOPE
141
142#endif
Low-level utilities for informing users of various internal and external diagnostic conditions.
Fast, compressed bit array which is capable of performing logical operations without first decompress...
size_t GetSize() const
Returns the size of the bit array, ie.
bool AreAllSet() const
Returns true, if all the bits in this bit array are set.
Vdf_FixedSizeHolder holds an object of type T of any size, but the sizeof(Vdf_FixedSizeHolder<T>) is ...
void New(Args &&... args)
Creates an instance.
void Destroy()
Destroys a held instance.
Implements a Vdf_VectorData storage that is always empty.
Implements a Vdf_VectorData storage that is holds a single element.
A VdfMask is placed on connections to specify the data flowing through them.
Definition: mask.h:37
size_t VdfEstimateSize(const T &)
Estimate the memory footprint of instance t of type T.
Definition: estimateSize.h:55
#define TF_VERIFY(cond, format,...)
Checks a condition and reports an error if it evaluates false.
Definition: diagnostic.h:266