Skip to content

Commit e620fe1

Browse files
authored
Merge pull request #25 from sourcehold/template-change-struct-instance-approach
[TEMPLATES] Change StructResolver instance approach
2 parents 73a5641 + abf741a commit e620fe1

3 files changed

Lines changed: 12 additions & 21 deletions

File tree

src/core/StructTest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
#include "ViewportRenderState.h"
44

55
MACRO_STRUCT_RESOLVER(ViewportRenderState, false, 0x100) ViewportRenderState_Struct;
6+
7+
// in this case, it will just instantiate the struct, calling the constructor, but only if implemented
8+
MACRO_STRUCT_INSTANCE(0x100);

src/core/ViewportRenderState.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33

44
#include "windowslib.h"
55

6-
// could also be placed in own file against the clutter
7-
// PROBLEM: Needs definition of address twice, could however be helped if addresses are moved into own struct/file
8-
MACRO_STRUCT_INSTANCE(ViewportRenderState, 0x100)
9-
{
10-
// needs direct create return to properly work
11-
// interestingly, "ViewportRenderState" here would be so big,
12-
// a stack overflow would occur if it was not optimized to be no copy
13-
// works with variables
14-
// might work good together with the copy prevention
15-
return ViewportRenderState();
16-
}
17-
186
ViewportRenderState::ViewportRenderState() { MACRO_CALL_MEMBER(ViewportRenderState_Func::_constructor_, this)(); }
197

208
// FUNCTION: STRONGHOLDCRUSADER 0x004e1fa0

src/precomp/StructResolver.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55

66
struct StructResolver {
77
private:
8-
template <typename T, int gameAddress> static T instance();
8+
template <typename T, int gameAddress> struct Instance;
99

1010
template <typename T, bool implemented, int gameAddress> struct InternalResolver;
1111
template <typename T, int gameAddress> struct InternalResolver<T, true, gameAddress> {
12-
private:
13-
static T instance;
14-
15-
public:
1612
static T* const ptr;
1713
};
1814
template <typename T, int gameAddress> struct InternalResolver<T, false, gameAddress> {
@@ -44,9 +40,10 @@ struct StructResolver {
4440

4541
template <typename T, int gameAddress>
4642
T* const StructResolver::InternalResolver<T, false, gameAddress>::ptr = reinterpret_cast<T*>(gameAddress);
43+
4744
template <typename T, int gameAddress>
48-
T StructResolver::InternalResolver<T, true, gameAddress>::instance = StructResolver::instance<T, gameAddress>();
49-
template <typename T, int gameAddress> T* const StructResolver::InternalResolver<T, true, gameAddress>::ptr = &instance;
45+
T* const StructResolver::InternalResolver<T, true, gameAddress>::ptr
46+
= &StructResolver::Instance<T, gameAddress>::instance;
5047

5148
template <int address> bool StructResolver::AddressUsageKeeper<address>::initialized = false;
5249

@@ -64,7 +61,10 @@ StructResolver::Resolver<T, implemented, gameAddress>::Initializer::Initializer(
6461
template struct StructResolver::Resolver<STRUCT_TYPE, IMPLEMENTED, GAME_ADDRESS>; \
6562
typedef StructResolver::Resolver<STRUCT_TYPE, IMPLEMENTED, GAME_ADDRESS>::Ptr
6663

67-
#define MACRO_STRUCT_INSTANCE(STRUCT_TYPE, GAME_ADDRESS) \
68-
template <> STRUCT_TYPE StructResolver::instance<STRUCT_TYPE, GAME_ADDRESS>()
64+
#define MACRO_STRUCT_INSTANCE(GAME_ADDRESS) \
65+
template <typename T> struct StructResolver::Instance<T, GAME_ADDRESS> { \
66+
static T instance; \
67+
}; \
68+
template <typename T> T StructResolver::Instance<T, GAME_ADDRESS>::instance
6969

7070
#endif // STRUCT_RESOLVER

0 commit comments

Comments
 (0)