Duplicated code
Nielsbishere opened this issue · 3 comments
Is your design request related to a problem? Please describe.
A lot of code that does almost exactly the same thing could be prevented by using templates or by smarter design decisions. Examples of these cases:
material_pool
model_pool
Describe the solution you'd like
When using something like setting textures, use an enum to index into a TextureHandle[]
instead of duplicating the same function and rewriting the same thing a few times.
Template functions could check for a field to be present in a class, therefore allowing you to do something like:
if constexpr(HasField(MyVertexFormat, pos))
; //Copy into pos, since it exists
Note that you'd have to define HasField yourself.
Describe alternatives you've considered
N.A.
Additional context
N.A.
I have a PoC for HasField:
#define HAS_FIELD(funcName, name, ...) \
template<typename, typename, typename = int> \
struct T##funcName { \
static constexpr bool value = false; \
}; \
\
template<typename T, typename Ret> \
struct T##funcName<T, Ret, \
std::enable_if_t<std::is_same_v<Ret, decltype(T::name)>, int> \
> { \
static constexpr bool value = true; \
}; \
\
template<typename T> \
struct funcName { \
static constexpr bool value = T##funcName<T, __VA_ARGS__, int>::value; \
};
This can be used like the following:
struct MyType { };
struct MyType0 { float field; };
HAS_FIELD(HasField, field, float);
static constexpr bool bools[2] = { HasField<MyType>::value, HasField<MyType0>::value };
Meaning that you can type;
namespace wr::magic {
HAS_FIELD(HasPosition, pos, XMFLOAT3);
}
//model loader
if constexpr(wr::magic::HasPosition<T>::value)
; //Load into position buffer
I'm not sure what code you exactly mean here. Can you give some line numbers?
For example; in model pool, there's the sample code copy pasted 5x one for every vertex type. We could have checks for for example position and then only call the set function if it has it. Makes it easier to modify as well.