Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Duplicated code #222

Open
Nielsbishere opened this issue Apr 1, 2019 · 3 comments
Open

Duplicated code #222

Nielsbishere opened this issue Apr 1, 2019 · 3 comments
Labels
design change Request to re-design a system/class/etc.

Comments

@Nielsbishere
Copy link
Contributor

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.

@Nielsbishere Nielsbishere added the design change Request to re-design a system/class/etc. label Apr 1, 2019
@Nielsbishere
Copy link
Contributor Author

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

@VZout
Copy link
Contributor

VZout commented May 24, 2019

I'm not sure what code you exactly mean here. Can you give some line numbers?

@Nielsbishere
Copy link
Contributor Author

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
design change Request to re-design a system/class/etc.
Projects
None yet
Development

No branches or pull requests

2 participants