Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid runtime indirection when initializing plCreatable class indices #1449

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Sources/Plasma/FeatureLib/pfPython/plPythonSDLModifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ class plPythonSDLModifier : public plSDLModifier
SDLMap fMap;
plPythonFileMod* fOwner;

plPythonSDLModifier() : fOwner() { }

PyObject* ISDLVarToPython(plSimpleStateVariable* var);
PyObject* ISDLVarIdxToPython(plSimpleStateVariable* var, int type, int idx);

Expand All @@ -94,6 +92,7 @@ class plPythonSDLModifier : public plSDLModifier
void IPutCurrentStateIn(plStateDataRecord* dstState) override;
void ISetCurrentStateFrom(const plStateDataRecord* srcState) override;
public:
plPythonSDLModifier() : fOwner() {}
plPythonSDLModifier(plPythonFileMod* owner);
~plPythonSDLModifier();

Expand Down
8 changes: 2 additions & 6 deletions Sources/Plasma/NucleusLib/pnFactory/plCreatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ public: \
return #plClassName; \
} \
private: \
static uint16_t plClassName##ClassIndex; \
static void SetClassIndex(uint16_t hClass) { \
plClassName##ClassIndex = hClass; \
} \
static const uint16_t plClassName##ClassIndex; \
public: \
uint16_t ClassIndex() const override { \
return plClassName::Index(); \
Expand Down Expand Up @@ -206,8 +203,7 @@ public: \
} \
static bool HasDerivedClass(uint16_t hDer) { \
return plFactory::DerivesFrom(plClassName##ClassIndex, hDer); \
} \
friend class plClassName##__Creator;
}



Expand Down
36 changes: 16 additions & 20 deletions Sources/Plasma/NucleusLib/pnFactory/plCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static_assert(plCreator::VerifyKeyedIndex<plClassName, CLASS_INDEX_SCOPED(plClas
\
static_assert(plCreator::VerifyNonKeyedIndex<plClassName, CLASS_INDEX_SCOPED(plClassName)>(), \
#plClassName " is in the non-KeyedObject section of plCreatableIndex but " \
"derives from hsKeyedObject."); //
"derives from hsKeyedObject.") //


#define VERIFY_EXTERNAL_CREATABLE(plClassName) \
Expand All @@ -104,7 +104,7 @@ static_assert(plCreator::VerifyKeyedIndex<plClassName, EXTERN_CLASS_INDEX_SCOPED
\
static_assert(plCreator::VerifyNonKeyedIndex<plClassName, EXTERN_CLASS_INDEX_SCOPED(plClassName)>(), \
#plClassName " is in the non-KeyedObject section of plCreatableIndex but " \
"derives from hsKeyedObject."); //
"derives from hsKeyedObject.") //


#define REGISTER_CREATABLE( plClassName ) \
Expand All @@ -115,8 +115,7 @@ class plClassName##__Creator : public plCreator
public: \
plClassName##__Creator() \
{ \
plFactory::Register( CLASS_INDEX_SCOPED(plClassName), this); \
plClassName::SetClassIndex(ClassIndex()); \
plFactory::Register(CLASS_INDEX_SCOPED(plClassName), this); \
} \
virtual ~plClassName##__Creator() \
{ \
Expand All @@ -132,8 +131,8 @@ public:
\
}; \
static plClassName##__Creator static##plClassName##__Creator; \
uint16_t plClassName::plClassName##ClassIndex = 0; \
VERIFY_CREATABLE(plClassName); //
const uint16_t plClassName::plClassName##ClassIndex = CLASS_INDEX_SCOPED(plClassName); \
VERIFY_CREATABLE(plClassName) //


#define REGISTER_NONCREATABLE( plClassName ) \
Expand All @@ -143,8 +142,7 @@ class plClassName##__Creator : public plCreator
public: \
plClassName##__Creator() \
{ \
plFactory::Register( CLASS_INDEX_SCOPED(plClassName), this); \
plClassName::SetClassIndex(ClassIndex()); \
plFactory::Register(CLASS_INDEX_SCOPED(plClassName), this); \
} \
virtual ~plClassName##__Creator() \
{ \
Expand All @@ -160,8 +158,8 @@ public:
\
}; \
static plClassName##__Creator static##plClassName##__Creator; \
uint16_t plClassName::plClassName##ClassIndex = 0; \
VERIFY_CREATABLE(plClassName); //
const uint16_t plClassName::plClassName##ClassIndex = CLASS_INDEX_SCOPED(plClassName); \
VERIFY_CREATABLE(plClassName) //


#define DECLARE_EXTERNAL_CREATABLE( plClassName ) \
Expand All @@ -178,8 +176,7 @@ public:
} \
void Register() \
{ \
plFactory::Register( EXTERN_CLASS_INDEX_SCOPED(plClassName), this); \
plClassName::SetClassIndex(ClassIndex()); \
plFactory::Register(EXTERN_CLASS_INDEX_SCOPED(plClassName), this); \
} \
void UnRegister() \
{ \
Expand All @@ -195,16 +192,16 @@ public:
\
}; \
static plClassName##__Creator static##plClassName##__Creator; \
uint16_t plClassName::plClassName##ClassIndex = 0; \
VERIFY_EXTERNAL_CREATABLE(plClassName); //
const uint16_t plClassName::plClassName##ClassIndex = EXTERN_CLASS_INDEX_SCOPED(plClassName); \
VERIFY_EXTERNAL_CREATABLE(plClassName) //


#define REGISTER_EXTERNAL_CREATABLE(plClassName) \
static##plClassName##__Creator.Register(); //
static##plClassName##__Creator.Register() //


#define UNREGISTER_EXTERNAL_CREATABLE(plClassName) \
plFactory::UnRegister(EXTERN_CLASS_INDEX_SCOPED(plClassName), &static##plClassName##__Creator);
plFactory::UnRegister(EXTERN_CLASS_INDEX_SCOPED(plClassName), &static##plClassName##__Creator)


#define REGISTER_EXTERNAL_NONCREATABLE( plClassName ) \
Expand All @@ -214,8 +211,7 @@ class plClassName##__Creator : public plCreator
public: \
plClassName##__Creator() \
{ \
plFactory::Register( EXTERN_CLASS_INDEX_SCOPED(plClassName), this); \
plClassName::SetClassIndex(ClassIndex()); \
plFactory::Register(EXTERN_CLASS_INDEX_SCOPED(plClassName), this); \
} \
virtual ~plClassName##__Creator() \
{ \
Expand All @@ -231,8 +227,8 @@ public:
\
}; \
static plClassName##__Creator static##plClassName##__Creator; \
uint16_t plClassName::plClassName##ClassIndex = 0; \
VERIFY_EXTERNAL_CREATABLE(plClassName); //
const uint16_t plClassName::plClassName##ClassIndex = EXTERN_CLASS_INDEX_SCOPED(plClassName); \
VERIFY_EXTERNAL_CREATABLE(plClassName) //


#endif // plCreator_inc
6 changes: 4 additions & 2 deletions Sources/Plasma/NucleusLib/pnFactory/plFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,13 @@ uint16_t plFactory::FindClassIndex(const char* className)

if (className && theFactory)
{
for (plCreator* creator : theFactory->fCreators)
for (size_t i = 0; i < theFactory->fCreators.size(); i++)
{
plCreator* creator = theFactory->fCreators[i];
if (creator && stricmp(className, creator->ClassName()) == 0)
{
return creator->ClassIndex();
hsAssert(i < numClasses, "Class index out of range??");
return static_cast<uint16_t>(i);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Plasma/PubUtilLib/plAvatar/plAvatarCreatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ REGISTER_NONCREATABLE(plAvTask);
REGISTER_CREATABLE(plAvAnimTask);
REGISTER_CREATABLE(plAvOneShotTask);
REGISTER_CREATABLE(plAvOneShotLinkTask);
REGISTER_CREATABLE(plAvSeekTask)
REGISTER_CREATABLE(plAvSeekTask);

#include "plAvBrain.h"
REGISTER_NONCREATABLE(plArmatureBrain);
Expand All @@ -100,7 +100,7 @@ REGISTER_CREATABLE(plAvBrainGeneric);
REGISTER_CREATABLE(plAvBrainHuman);

#include "plAvBrainRideAnimatedPhysical.h"
REGISTER_CREATABLE(plAvBrainRideAnimatedPhysical)
REGISTER_CREATABLE(plAvBrainRideAnimatedPhysical);

#include "plAvBrainSwim.h"
REGISTER_CREATABLE(plAvBrainSwim);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Plasma/PubUtilLib/plMessage/plMessageCreatable.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ REGISTER_CREATABLE(plSetListenerMsg);

#include "plLoadAgeMsg.h"
REGISTER_CREATABLE(plLoadAgeMsg);
REGISTER_CREATABLE(plLinkInDoneMsg)
REGISTER_CREATABLE(plLinkInDoneMsg);
REGISTER_CREATABLE(plLinkOutUnloadMsg);

#include "plLOSHitMsg.h"
Expand Down