Commit f3628f7e authored by David Thompson's avatar David Thompson Committed by Kitware Robot

Merge topic 'syntactic-sugar'

fa6832df Add some syntactic sugar...
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Acked-by: Haocheng LIU's avatarHaocheng Liu <haocheng.liu@kitware.com>
Merge-request: !913
parents 78f90b36 fa6832df
...@@ -62,6 +62,13 @@ public: ...@@ -62,6 +62,13 @@ public:
ResourcePtr resource() const override; ResourcePtr resource() const override;
ManagerPtr modelResource() const; ManagerPtr modelResource() const;
/// Return the templated object (usually EntityRef or a subclass) that points to this component.
template <typename T>
T referenceAs() const
{
return T(this->modelResource(), this->id());
}
bool reparent(ManagerPtr newParent); bool reparent(ManagerPtr newParent);
int dimension() const; int dimension() const;
......
...@@ -50,6 +50,16 @@ EntityRef::EntityRef(ManagerPtr mgr, const smtk::common::UUID& inEntity) ...@@ -50,6 +50,16 @@ EntityRef::EntityRef(ManagerPtr mgr, const smtk::common::UUID& inEntity)
{ {
} }
/// Construct a entityref referencing a given \a entity residing in the given \a mgr.
EntityRef::EntityRef(EntityPtr src)
{
if (src)
{
m_manager = src->modelResource();
m_entity = src->id();
}
}
/// Change the underlying manager the entityref references. /// Change the underlying manager the entityref references.
bool EntityRef::setManager(ManagerPtr mgr) bool EntityRef::setManager(ManagerPtr mgr)
{ {
...@@ -89,6 +99,17 @@ const smtk::common::UUID& EntityRef::entity() const ...@@ -89,6 +99,17 @@ const smtk::common::UUID& EntityRef::entity() const
return this->m_entity; return this->m_entity;
} }
/// Return the smtk::model::Entity record for this model entity.
smtk::model::EntityPtr EntityRef::entityRecord() const
{
auto mgr = this->manager();
if (!mgr)
{
return nullptr;
}
return mgr->findEntity(this->m_entity, true);
}
/// Return the resource component for this model entity. /// Return the resource component for this model entity.
smtk::resource::ComponentPtr EntityRef::component() const smtk::resource::ComponentPtr EntityRef::component() const
{ {
......
...@@ -7,8 +7,7 @@ ...@@ -7,8 +7,7 @@
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information. // PURPOSE. See the above copyright notice for more information.
//========================================================================= //=========================================================================
#ifndef __smtk_model_EntityRef_h #pragma once
#define __smtk_model_EntityRef_h
/*! \file */ /*! \file */
#include "smtk/CoreExports.h" // For EXPORT macro. #include "smtk/CoreExports.h" // For EXPORT macro.
...@@ -51,6 +50,14 @@ class MeshSet; ...@@ -51,6 +50,14 @@ class MeshSet;
: superclass(inManager, entityId) \ : superclass(inManager, entityId) \
{ \ { \
} \ } \
thisclass(EntityPtr src) \
{ \
if (src) \
{ \
m_manager = src->modelResource(); \
m_entity = src->id(); \
} \
} \
virtual ~thisclass() {} /* Avoid warnings about non-virtual destructor */ \ virtual ~thisclass() {} /* Avoid warnings about non-virtual destructor */ \
bool isValid() const { return this->EntityRef::isValid(); } \ bool isValid() const { return this->EntityRef::isValid(); } \
bool isValid(EntityPtr* entRec) const override \ bool isValid(EntityPtr* entRec) const override \
...@@ -94,6 +101,7 @@ public: ...@@ -94,6 +101,7 @@ public:
SMTK_BASE_TYPE(EntityRef); SMTK_BASE_TYPE(EntityRef);
EntityRef(); EntityRef();
EntityRef(ManagerPtr manager, const smtk::common::UUID& entityId); EntityRef(ManagerPtr manager, const smtk::common::UUID& entityId);
EntityRef(EntityPtr src);
bool setManager(ManagerPtr manager); bool setManager(ManagerPtr manager);
ManagerPtr manager(); ManagerPtr manager();
...@@ -102,6 +110,7 @@ public: ...@@ -102,6 +110,7 @@ public:
bool setEntity(const smtk::common::UUID& entityId); bool setEntity(const smtk::common::UUID& entityId);
const smtk::common::UUID& entity() const; const smtk::common::UUID& entity() const;
smtk::model::EntityPtr entityRecord() const;
smtk::resource::ComponentPtr component() const; smtk::resource::ComponentPtr component() const;
int dimension() const; int dimension() const;
...@@ -415,5 +424,3 @@ EntityRef& EntityRef::removeMemberEntities(T begin, T end) ...@@ -415,5 +424,3 @@ EntityRef& EntityRef::removeMemberEntities(T begin, T end)
} // namespace model } // namespace model
} // namespace smtk } // namespace smtk
#endif // __smtk_model_EntityRef_h
...@@ -210,6 +210,20 @@ void testModelMethods() ...@@ -210,6 +210,20 @@ void testModelMethods()
test(m1.session() == m0.session(), "Expected sessions to match for model and its submodel."); test(m1.session() == m0.session(), "Expected sessions to match for model and its submodel.");
} }
void testResourceComponentConversion()
{
ManagerPtr sm = Manager::create();
SessionRef sess = sm->createSession("native");
Model m0 = sm->addModel();
smtk::model::EntityPtr mep = m0.entityRecord();
test(!!mep, "No component for Model entity-ref.");
test(mep->referenceAs<smtk::model::Model>() == m0, "Could not convert from component to ref.");
test(smtk::model::Model(mep) == m0, "Could not convert from ref to component.");
smtk::resource::ComponentPtr cmp = m0.component();
test(cmp == smtk::dynamic_pointer_cast<smtk::resource::Component>(mep),
"Component/Entity mismatch.");
}
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
(void)argc; (void)argc;
...@@ -559,6 +573,7 @@ int main(int argc, char* argv[]) ...@@ -559,6 +573,7 @@ int main(int argc, char* argv[])
testTemplatedPropertyMethods(); testTemplatedPropertyMethods();
testVolumeEntityRef(); testVolumeEntityRef();
testModelMethods(); testModelMethods();
testResourceComponentConversion();
} }
catch (const std::string& msg) catch (const std::string& msg)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment