diff -r 7c27cda03747 -r 97df974b5ed5 src/model.h --- a/src/model.h Fri Mar 06 23:45:44 2020 +0200 +++ b/src/model.h Mon Mar 09 14:21:54 2020 +0200 @@ -44,19 +44,33 @@ const QString& getName() const; QVariant getObjectProperty(const int index, const ldraw::Property property) const; std::vector getPolygons(class DocumentManager* documents) const; - QModelIndex lookup(ldraw::Id id) const; - ldraw::Id resolve(const QModelIndex& index) const; + QModelIndex lookup(ldraw::id_t id) const; + ldraw::id_t resolve(const QModelIndex& index) const; + template + ldraw::Id checkType(ldraw::id_t id) const; + template + const R* get(ldraw::Id id) const; signals: - void objectAdded(ldraw::Id id, int position); + void objectAdded(ldraw::id_t id, int position); private: using ModelObjectPointer = std::unique_ptr; template - ldraw::Id append(Args&&... args); + ldraw::Id append(Args&&... args); void append(ModelObjectPointer&& object); template - ldraw::Id insert(int position, Args&&... args); + ldraw::Id insert(int position, Args&&... args); ldraw::Object* objectAt(const QModelIndex& index); const ldraw::Object* objectAt(const QModelIndex& index) const; + template + T* objectAt(ldraw::Id id) + { + return static_cast(this->objectAt(this->lookup(id))); + } + template + const T* objectAt(ldraw::Id id) const + { + return static_cast(this->objectAt(this->lookup(id))); + } void getObjectPolygons( const int index, std::vector& polygons_out, @@ -65,13 +79,31 @@ QString path; LDHeader header; std::vector body; - std::map objectsById; + std::map objectsById; mutable std::vector cachedPolygons; mutable bool needRecache = true; }; +/** + * \brief Checks type of object behind id + * Checks whether the specified id refers to an object of the specified type. + * \returns id casted to subclass if appropriate, null id otherwise + */ +template +ldraw::Id Model::checkType(ldraw::id_t id) const +{ + if (dynamic_cast(this->objectAt(this->lookup(id))) != nullptr) + { + return ldraw::Id{id.value}; + } + else + { + return ldraw::NULL_ID; + } +} + template -ldraw::Id Model::append(Args&&... args) +ldraw::Id Model::append(Args&&... args) { emit layoutAboutToBeChanged(); this->body.push_back(std::make_unique(args...)); @@ -79,11 +111,11 @@ this->objectsById[pointer->id] = pointer; emit objectAdded(pointer->id, this->body.size() - 1); emit layoutChanged(); - return pointer->id; + return ldraw::Id{pointer->id}; } template -ldraw::Id Model::insert(int position, Args&&... args) +ldraw::Id Model::insert(int position, Args&&... args) { emit layoutAboutToBeChanged(); this->body.insert(position, std::make_unique(args...)); @@ -91,5 +123,11 @@ this->objectsById[pointer->id] = pointer; emit objectAdded(pointer->id, position); emit layoutChanged(); - return pointer->id; + return ldraw::Id{pointer->id}; } + +template +const R* Model::get(ldraw::Id id) const +{ + return this->objectAt(id); +}