comparison base/ById.h @ 1746:826e0cb77d8e by-id

Add isa
author Chris Cannam
date Tue, 02 Jul 2019 15:28:46 +0100
parents b92bdcd4954b
children d0ef65d8dd89
comparison
equal deleted inserted replaced
1745:074b860a7828 1746:826e0cb77d8e
119 class AnyById 119 class AnyById
120 { 120 {
121 public: 121 public:
122 static void add(int, std::shared_ptr<WithId>); 122 static void add(int, std::shared_ptr<WithId>);
123 static void release(int); 123 static void release(int);
124 static std::shared_ptr<WithId> get(int); 124 static std::shared_ptr<WithId> get(int);
125 125
126 template <typename Derived>
127 static bool isa(int id) {
128 std::shared_ptr<WithId> p = get(id);
129 return bool(std::dynamic_pointer_cast<Derived>(p));
130 }
131
126 template <typename Derived> 132 template <typename Derived>
127 static std::shared_ptr<Derived> getAs(int id) { 133 static std::shared_ptr<Derived> getAs(int id) {
128 std::shared_ptr<WithId> p = get(id); 134 std::shared_ptr<WithId> p = get(id);
129 return std::dynamic_pointer_cast<Derived>(p); 135 return std::dynamic_pointer_cast<Derived>(p);
130 } 136 }
152 static void release(std::shared_ptr<Item> item) { 158 static void release(std::shared_ptr<Item> item) {
153 release(item->getId()); 159 release(item->getId());
154 } 160 }
155 161
156 template <typename Derived> 162 template <typename Derived>
163 static bool isa(Id id) {
164 return AnyById::isa<Derived>(id.untyped);
165 }
166
167 template <typename Derived>
157 static std::shared_ptr<Derived> getAs(Id id) { 168 static std::shared_ptr<Derived> getAs(Id id) {
158 return AnyById::getAs<Derived>(id.untyped); 169 return AnyById::getAs<Derived>(id.untyped);
159 } 170 }
160 171
161 static std::shared_ptr<Item> get(Id id) { 172 static std::shared_ptr<Item> get(Id id) {