refactor: TYPE_ -> static TYPE

This commit is contained in:
maelstrom 2025-02-04 20:18:58 +01:00
parent 7cf7343406
commit da1f6b1d24
8 changed files with 25 additions and 30 deletions

View file

@ -148,10 +148,10 @@ Qt::ItemFlags ExplorerModel::flags(const QModelIndex &index) const
: Qt::NoItemFlags | Qt::ItemIsDropEnabled; : Qt::NoItemFlags | Qt::ItemIsDropEnabled;
} }
QImage ExplorerModel::iconOf(InstanceType* type) const { QImage ExplorerModel::iconOf(const InstanceType* type) const {
if (instanceIconCache.count(type->className)) return instanceIconCache[type->className]; if (instanceIconCache.count(type->className)) return instanceIconCache[type->className];
InstanceType* currentClass = type; const InstanceType* currentClass = type;
while (currentClass->explorerIcon.empty()) currentClass = currentClass->super; while (currentClass->explorerIcon.empty()) currentClass = currentClass->super;
QImage icon("assets/icons/" + QString::fromStdString(currentClass->explorerIcon)); QImage icon("assets/icons/" + QString::fromStdString(currentClass->explorerIcon));

View file

@ -45,7 +45,7 @@ public:
private: private:
InstanceRef rootItem; InstanceRef rootItem;
QModelIndex toIndex(InstanceRef item); QModelIndex toIndex(InstanceRef item);
QImage iconOf(InstanceType* type) const; QImage iconOf(const InstanceType* type) const;
}; };
// #endif // #endif

View file

@ -10,21 +10,19 @@
#include <optional> #include <optional>
// Static so that this variable name is "local" to this source file // Static so that this variable name is "local" to this source file
static InstanceType TYPE_ { const InstanceType Instance::TYPE = {
.super = NULL, .super = NULL,
.className = "Instance", .className = "Instance",
.constructor = NULL, // Instance is abstract and therefore not creatable .constructor = NULL, // Instance is abstract and therefore not creatable
.explorerIcon = "instance", .explorerIcon = "instance",
}; };
InstanceType* Instance::TYPE = &TYPE_;
// Instance is abstract, so it should not implement GetClass directly // Instance is abstract, so it should not implement GetClass directly
// InstanceType* Instance::GetClass() { // InstanceType* Instance::GetClass() {
// return &TYPE_; // return &TYPE_;
// } // }
Instance::Instance(InstanceType* type) { Instance::Instance(const InstanceType* type) {
this->name = type->className; this->name = type->className;
this->memberMap = std::make_unique<MemberMap>( MemberMap { this->memberMap = std::make_unique<MemberMap>( MemberMap {
@ -48,7 +46,7 @@ bool Instance::ancestryContinuityCheck(std::optional<std::shared_ptr<Instance>>
} }
bool Instance::SetParent(std::optional<std::shared_ptr<Instance>> newParent) { bool Instance::SetParent(std::optional<std::shared_ptr<Instance>> newParent) {
if (!ancestryContinuityCheck(newParent)) if (this->parentLocked || !ancestryContinuityCheck(newParent))
return false; return false;
auto lastParent = GetParent(); auto lastParent = GetParent();

View file

@ -19,7 +19,7 @@ typedef std::shared_ptr<Instance>(*InstanceConstructor)();
// Struct describing information about an instance // Struct describing information about an instance
struct InstanceType { struct InstanceType {
InstanceType* super; // May be null const InstanceType* super; // May be null
std::string className; std::string className;
InstanceConstructor constructor; InstanceConstructor constructor;
std::string explorerIcon = ""; std::string explorerIcon = "";
@ -38,18 +38,19 @@ private:
bool ancestryContinuityCheck(std::optional<std::shared_ptr<Instance>> newParent); bool ancestryContinuityCheck(std::optional<std::shared_ptr<Instance>> newParent);
protected: protected:
bool parentLocked;
std::unique_ptr<MemberMap> memberMap; std::unique_ptr<MemberMap> memberMap;
Instance(InstanceType*); Instance(const InstanceType*);
virtual ~Instance(); virtual ~Instance();
virtual void OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent); virtual void OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent);
public: public:
static InstanceType* TYPE; const static InstanceType TYPE;
std::string name; std::string name;
// Instance is abstract, so it should not implement GetClass directly // Instance is abstract, so it should not implement GetClass directly
virtual InstanceType* GetClass() = 0; virtual const InstanceType* GetClass() = 0;
bool SetParent(std::optional<std::shared_ptr<Instance>> newParent); bool SetParent(std::optional<std::shared_ptr<Instance>> newParent);
std::optional<std::shared_ptr<Instance>> GetParent(); std::optional<std::shared_ptr<Instance>> GetParent();
inline const std::vector<std::shared_ptr<Instance>> GetChildren() { return children; } inline const std::vector<std::shared_ptr<Instance>> GetChildren() { return children; }

View file

@ -6,23 +6,21 @@
#include <optional> #include <optional>
#include "physics/simulation.h" #include "physics/simulation.h"
static InstanceType TYPE_ { const InstanceType Part::TYPE = {
.super = Instance::TYPE, .super = &Instance::TYPE,
.className = "Part", .className = "Part",
.constructor = &Part::CreateGeneric, .constructor = &Part::CreateGeneric,
.explorerIcon = "part", .explorerIcon = "part",
}; };
InstanceType* Part::TYPE = &TYPE_; const InstanceType* Part::GetClass() {
return &TYPE;
InstanceType* Part::GetClass() {
return &TYPE_;
} }
Part::Part(): Part(PartConstructParams {}) { Part::Part(): Part(PartConstructParams {}) {
} }
Part::Part(PartConstructParams params): Instance(&TYPE_), position(params.position), rotation(params.rotation), Part::Part(PartConstructParams params): Instance(&TYPE), position(params.position), rotation(params.rotation),
scale(params.scale), material(params.material), anchored(params.anchored) { scale(params.scale), material(params.material), anchored(params.anchored) {
this->memberMap = std::make_unique<MemberMap>(MemberMap { this->memberMap = std::make_unique<MemberMap>(MemberMap {
.super = std::move(this->memberMap), .super = std::move(this->memberMap),

View file

@ -24,7 +24,7 @@ protected:
void OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent) override; void OnParentUpdated(std::optional<std::shared_ptr<Instance>> oldParent, std::optional<std::shared_ptr<Instance>> newParent) override;
void onUpdated(std::string); void onUpdated(std::string);
public: public:
static InstanceType* TYPE; const static InstanceType TYPE;
// TODO: Switch these over to our dedicated datatypes // TODO: Switch these over to our dedicated datatypes
glm::vec3 position; glm::vec3 position;
@ -43,5 +43,5 @@ public:
static inline std::shared_ptr<Part> New() { return std::make_shared<Part>(); }; static inline std::shared_ptr<Part> New() { return std::make_shared<Part>(); };
static inline std::shared_ptr<Part> New(PartConstructParams params) { return std::make_shared<Part>(params); }; static inline std::shared_ptr<Part> New(PartConstructParams params) { return std::make_shared<Part>(params); };
static inline InstanceRef CreateGeneric() { return std::make_shared<Part>(); }; static inline InstanceRef CreateGeneric() { return std::make_shared<Part>(); };
virtual InstanceType* GetClass() override; virtual const InstanceType* GetClass() override;
}; };

View file

@ -1,16 +1,14 @@
#include "workspace.h" #include "workspace.h"
static InstanceType TYPE_ { const InstanceType Workspace::TYPE = {
.super = Instance::TYPE, .super = &Instance::TYPE,
.className = "Workspace", .className = "Workspace",
.constructor = &Workspace::Create, .constructor = &Workspace::Create,
}; };
InstanceType* Workspace::TYPE = &TYPE_; const InstanceType* Workspace::GetClass() {
return &TYPE;
InstanceType* Workspace::GetClass() {
return &TYPE_;
} }
Workspace::Workspace(): Instance(&TYPE_) { Workspace::Workspace(): Instance(&TYPE) {
} }

View file

@ -6,11 +6,11 @@
class Workspace : public Instance { class Workspace : public Instance {
//private: //private:
public: public:
static InstanceType* TYPE; const static InstanceType TYPE;
Workspace(); Workspace();
static inline std::shared_ptr<Workspace> New() { return std::make_shared<Workspace>(); }; static inline std::shared_ptr<Workspace> New() { return std::make_shared<Workspace>(); };
static inline InstanceRef Create() { return std::make_shared<Workspace>(); }; static inline InstanceRef Create() { return std::make_shared<Workspace>(); };
virtual InstanceType* GetClass() override; virtual const InstanceType* GetClass() override;
}; };