Commit 260a723b authored by T.J. Corona's avatar T.J. Corona

Add a static field for resource index

parent 49a3b7bb
......@@ -34,12 +34,17 @@ class DerivedFrom : public Parent
public:
typedef Parent ParentResource;
/// A static index for this resource type.
///
/// NOTE: because we are using CRTP, it is possible to make this value static
/// and redefined for each resource type, regardless of inheritance.
static const Resource::Index type_index;
/// given a resource index, return whether or not this resource is or is
/// derived from the resource described by the index.
virtual bool isOfType(const Resource::Index& index) const override
{
return std::type_index(typeid(Self)).hash_code() == index ? true
: ParentResource::isOfType(index);
return DerivedFrom<Self, Parent>::type_index == index ? true : ParentResource::isOfType(index);
}
/// given a resource's unique name, return whether or not this resource is or
......@@ -57,6 +62,10 @@ protected:
{
}
};
template <typename Self, typename Parent>
const Resource::Index DerivedFrom<Self, Parent>::type_index =
std::type_index(typeid(Self)).hash_code();
}
}
......
......@@ -184,5 +184,13 @@ int TestResourceManager(int, char** const)
smtkTest(resourceASet.size() == 3,
"Resource manager should have three resources of type ResourceA registered.");
// Test fetching resources by exact index; this will only
// return instances that are of the given class not including subclasses.
auto indexA = ResourceA::type_index;
auto resourcesByIndex =
resourceManager->resources().get<smtk::resource::IndexTag>().equal_range(indexA);
int count = std::distance(resourcesByIndex.first, resourcesByIndex.second);
smtkTest(count == 2, "Fetched " << count << " instead of 2 resources by type-index failed.");
return 0;
}
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