# Resource index

**URL:** <https://discourse.kitware.com/t/resource-index/221>\
**Category:** Design\
**Created:** [February 6, 2019, 6:46pm UTC](https://discourse.kitware.com/t/resource-index/221 "2019-02-06T18:46:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.kitware.com/user_avatar/discourse.kitware.com/dcthomp/32/10_2.png) [@dcthomp](https://discourse.kitware.com/u/dcthomp)\
**Post date:** [February 6, 2019, 6:46pm UTC](https://discourse.kitware.com/t/resource-index/221/1 "2019-02-06T18:46:47Z")

</div>

@tj.corona  
It looks like resource classes only provide a virtual method to obtain a Resource::Index. What would you think about adding

```auto
class Foo : public smtk::resource::Resource
{
  static constexpr Index TypeIndex =
    std::type_index(typeid(Foo)).hash_code());
};

```

to resources so that we can do things like ask a resource manager for resources of a compile-time-known type?

```auto
smtk::resource::Manager::Ptr mgr;
auto rsrcs = mgr->resources().get<IndexTag>().equal_range(Foo::TypeIndex);
for (rsrc : rsrcs) { ... }

```

---

<div class="post-metadata">

**Author:** ![tj.corona](https://discourse.kitware.com/user_avatar/discourse.kitware.com/tj.corona/32/35_2.png) [@tj.corona](https://discourse.kitware.com/u/tj.corona)\
**Post date:** [February 6, 2019, 10:15pm UTC](https://discourse.kitware.com/t/resource-index/221/2 "2019-02-06T22:15:02Z")

</div>

Makes sense to me. Better still would be to put it in DerivedFrom.h, so it doesn’t have to be placed in every resource (hooray for CRTP!).

---

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.kitware.com/user_avatar/discourse.kitware.com/dcthomp/32/10_2.png) [@dcthomp](https://discourse.kitware.com/u/dcthomp)\
**Post date:** [February 7, 2019, 3:27pm UTC](https://discourse.kitware.com/t/resource-index/221/3 "2019-02-07T15:27:45Z")

</div>

Sadly, classes cannot provide `constexpr` to their own index (since `typeid` is ill-formed before the class is fully defined and thus no class member can hold its own Index). However, it is possible to [provide a static method to compute it](https://gitlab.kitware.com/cmb/smtk/merge_requests/1474).

---

<div class="post-metadata">

**Author:** ![tj.corona](https://discourse.kitware.com/user_avatar/discourse.kitware.com/tj.corona/32/35_2.png) [@tj.corona](https://discourse.kitware.com/u/tj.corona)\
**Post date:** [February 7, 2019, 9:13pm UTC](https://discourse.kitware.com/t/resource-index/221/4 "2019-02-07T21:13:40Z")

</div>

To close this out, [here](https://gitlab.kitware.com/cmb/smtk/merge_requests/1475) is the merge request that implemented this feature.
