Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <map>
- #include <cstring>
- #include <type_traits>
- #include <ctime>
- #define DEFINE_BASES(class, ...) \
- static hash_t GetClassNameHashStatic() \
- { \
- static hash_t classNameHash(HashString(#class)); \
- return classNameHash; \
- } \
- \
- hash_t GetClassNameHash() const \
- { \
- return GetClassNameHashStatic(); \
- } \
- \
- template <typename _empty> \
- void RegisterAllSubclasses() const \
- { \
- \
- } \
- \
- template <typename _empty, typename T, typename... Args> \
- void RegisterAllSubclasses() const \
- { \
- T::RegisterAllSubclasses(); \
- RegisterAllSubclasses<void, Args...>(); \
- } \
- \
- virtual void RegisterAllSubclasses() const \
- { \
- RegisterSubclass(static_cast<const void*>(this), class::GetClassNameHash());\
- RegisterAllSubclasses<void, __VA_ARGS__>(); \
- }
- typedef unsigned int hash_t;
- hash_t HashString(const char* str)
- {
- const unsigned char* key = (const unsigned char*)(str);
- size_t len = strlen(str);
- hash_t h = 0;
- for (size_t i = 0; i < len; ++i)
- {
- h += key[i];
- h += (h << 10);
- h ^= (h >> 6 );
- }
- h += (h << 3 );
- h ^= (h >> 11);
- h += (h << 15);
- return h;
- }
- class Object
- {
- public:
- static hash_t GetClassNameHashStatic()
- {
- static hash_t classNameHash(HashString("Object"));
- return classNameHash;
- }
- virtual hash_t GetClassNameHash() const
- {
- return GetClassNameHashStatic();
- }
- void* To(hash_t className)
- {
- if (_bases.size() == 0)
- {
- BuildInheritanceCache();
- }
- auto result = _bases.find(className);
- return (result != _bases.end() ? (*result).second : NULL);
- }
- const void* To(hash_t className) const
- {
- if (_bases.size() == 0)
- {
- BuildInheritanceCache();
- }
- auto result = _bases.find(className);
- return (result != _bases.end() ? (*result).second : NULL);
- }
- protected:
- void RegisterSubclass(const void* ptr, hash_t className) const
- {
- _bases[className] = const_cast<void*>(ptr);
- }
- virtual void RegisterAllSubclasses() const
- {
- RegisterSubclass(static_cast<const void*>(this), Object::GetClassNameHash());
- }
- private:
- void BuildInheritanceCache() const
- {
- _bases.clear();
- RegisterAllSubclasses();
- }
- mutable std::map<hash_t, void*> _bases;
- };
- ////////////////////////////////////////////////////////////////////////////////
- template <typename T>
- T my_dynamic_cast(Object* ptr)
- {
- return static_cast<T>(ptr->To(std::remove_pointer<T>::type::GetClassNameHashStatic()));
- }
- template <typename T>
- T my_dynamic_cast(const Object* ptr)
- {
- return static_cast<T>(ptr->To(std::remove_pointer<T>::type::GetClassNameHashStatic()));
- }
- ////////////////////////////////////////////////////////////////////////////////
- class Base1 : virtual public Object
- {
- public:
- int _base1;
- DEFINE_BASES(Base1, Object);
- };
- class Base2 : virtual public Object
- {
- public:
- int _base2;
- DEFINE_BASES(Base2, Object);
- };
- class Base3 : virtual public Object
- {
- public:
- int _base3;
- DEFINE_BASES(Base3, Object);
- };
- class Derived1 : public Base1, public Base2
- {
- public:
- int _derived1;
- DEFINE_BASES(Derived1, Base1, Base2);
- };
- class Derived2 : public Base3
- {
- public:
- int _derived2;
- DEFINE_BASES(Derived2, Base3);
- };
- class MostDerived : public Derived1, public Derived2
- {
- public:
- int _mostDerived;
- DEFINE_BASES(MostDerived, Derived1, Derived2);
- };
- int main()
- {
- Object* o = new MostDerived;
- MostDerived* d;
- time_t t = clock();
- for (int i = 0; i < 10000000; ++i)
- {
- d = dynamic_cast<MostDerived*>(o);
- }
- printf("%i\n", clock() - t);
- t = clock();
- for (int i = 0; i < 10000000; ++i)
- {
- d = my_dynamic_cast<MostDerived*>(o);
- }
- printf("%i\n", clock() - t);
- delete d;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement