We need a mechanism by which we can iterate all items in the attribute map and perform an arbitrary operation on each. 更多...
We need a mechanism by which we can iterate all items in the attribute map and perform an arbitrary operation on each.
We require polymorphic behavior, as attribute arrays are templated on their attribute type, and derived from a generic base class. However, we cannot have a virtual templated method, so we use a different approach.
Effectively, we wish to cast the attribute array depending on the type member of the base class as we iterate through the map. This might look something like this:
template <typename FuncType> void ForEach(FuncType Func) { for (const auto& MapEntry : Map) { const uint32 Type = MapEntry.Value->GetType(); switch (Type) { case 0: Func(static_cast<TMeshAttributeArraySet<FVector>*>(MapEntry.Value.Get()); break; case 1: Func(static_cast<TMeshAttributeArraySet<FVector4>*>(MapEntry.Value.Get()); break; case 2: Func(static_cast<TMeshAttributeArraySet<FVector2D>*>(MapEntry.Value.Get()); break; case 3: Func(static_cast<TMeshAttributeArraySet<float>*>(MapEntry.Value.Get()); break; .... } } }
(The hope is that the compiler would optimize the switch into a jump table so we get O(1) dispatch even as the number of attribute types increases.)
The approach taken here is to generate a jump table at compile time, one entry per possible attribute type. The function Dispatch(...) is the actual function which gets called. MakeJumpTable() is the constexpr function which creates a static jump table at compile time.