Advertisement
Guest User

Untitled

a guest
Jan 30th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4. #include <cstring>
  5. #include <type_traits>
  6. #include <ctime>
  7.  
  8. #define DEFINE_BASES(class, ...)                                                    \
  9.     static hash_t GetClassNameHashStatic()                                          \
  10.     {                                                                               \
  11.         static hash_t classNameHash(HashString(#class));                            \
  12.         return classNameHash;                                                       \
  13.     }                                                                               \
  14.                                                                                     \
  15.     hash_t GetClassNameHash() const                                                 \
  16.     {                                                                               \
  17.         return GetClassNameHashStatic();                                            \
  18.     }                                                                               \
  19.                                                                                     \
  20.     template <typename _empty>                                                      \
  21.     void RegisterAllSubclasses() const                                              \
  22.     {                                                                               \
  23.                                                                                     \
  24.     }                                                                               \
  25.                                                                                     \
  26.     template <typename _empty, typename T, typename... Args>                        \
  27.     void RegisterAllSubclasses() const                                              \
  28.     {                                                                               \
  29.         T::RegisterAllSubclasses();                                                 \
  30.         RegisterAllSubclasses<void, Args...>();                                     \
  31.     }                                                                               \
  32.                                                                                     \
  33.     virtual void RegisterAllSubclasses() const                                      \
  34.     {                                                                               \
  35.         RegisterSubclass(static_cast<const void*>(this), class::GetClassNameHash());\
  36.         RegisterAllSubclasses<void, __VA_ARGS__>();                                 \
  37.     }
  38.  
  39. typedef unsigned int hash_t;
  40.  
  41. hash_t HashString(const char* str)
  42. {
  43.     const unsigned char* key = (const unsigned char*)(str);
  44.     size_t len = strlen(str);
  45.     hash_t h = 0;
  46.  
  47.  
  48.     for (size_t i = 0; i < len; ++i)
  49.     {
  50.         h += key[i];
  51.         h += (h << 10);
  52.         h ^= (h >> 6 );
  53.     }
  54.  
  55.     h += (h << 3 );
  56.     h ^= (h >> 11);
  57.     h += (h << 15);
  58.  
  59.     return h;
  60. }
  61.  
  62. class Object
  63. {
  64.     public:
  65.         static hash_t GetClassNameHashStatic()
  66.         {
  67.             static hash_t classNameHash(HashString("Object"));
  68.             return classNameHash;
  69.         }
  70.  
  71.         virtual hash_t GetClassNameHash() const
  72.         {
  73.             return GetClassNameHashStatic();
  74.         }
  75.  
  76.         void* To(hash_t className)
  77.         {
  78.             if (_bases.size() == 0)
  79.             {
  80.                 BuildInheritanceCache();
  81.             }
  82.  
  83.             auto result = _bases.find(className);
  84.             return (result != _bases.end() ? (*result).second : NULL);
  85.         }
  86.  
  87.         const void* To(hash_t className) const
  88.         {
  89.             if (_bases.size() == 0)
  90.             {
  91.                 BuildInheritanceCache();
  92.             }
  93.  
  94.             auto result = _bases.find(className);
  95.             return (result != _bases.end() ? (*result).second : NULL);
  96.         }
  97.  
  98.     protected:
  99.         void RegisterSubclass(const void* ptr, hash_t className) const
  100.         {
  101.             _bases[className] = const_cast<void*>(ptr);
  102.         }
  103.  
  104.         virtual void RegisterAllSubclasses() const
  105.         {
  106.             RegisterSubclass(static_cast<const void*>(this), Object::GetClassNameHash());
  107.         }
  108.  
  109.     private:
  110.         void BuildInheritanceCache() const
  111.         {
  112.             _bases.clear();
  113.             RegisterAllSubclasses();
  114.         }
  115.  
  116.         mutable std::map<hash_t, void*> _bases;
  117. };
  118.  
  119. ////////////////////////////////////////////////////////////////////////////////
  120.  
  121. template <typename T>
  122. T my_dynamic_cast(Object* ptr)
  123. {
  124.     return static_cast<T>(ptr->To(std::remove_pointer<T>::type::GetClassNameHashStatic()));
  125. }
  126.  
  127. template <typename T>
  128. T my_dynamic_cast(const Object* ptr)
  129. {
  130.     return static_cast<T>(ptr->To(std::remove_pointer<T>::type::GetClassNameHashStatic()));
  131. }
  132.  
  133. ////////////////////////////////////////////////////////////////////////////////
  134.  
  135. class Base1 : virtual public Object
  136. {
  137.     public:
  138.         int _base1;
  139.         DEFINE_BASES(Base1, Object);
  140. };
  141.  
  142. class Base2 : virtual public Object
  143. {
  144.     public:
  145.         int _base2;
  146.         DEFINE_BASES(Base2, Object);
  147. };
  148.  
  149. class Base3 : virtual public Object
  150. {
  151.     public:
  152.         int _base3;
  153.         DEFINE_BASES(Base3, Object);
  154. };
  155.  
  156. class Derived1 : public Base1, public Base2
  157. {
  158.     public:
  159.         int _derived1;
  160.         DEFINE_BASES(Derived1, Base1, Base2);
  161. };
  162.  
  163. class Derived2 : public Base3
  164. {
  165.     public:
  166.         int _derived2;
  167.         DEFINE_BASES(Derived2, Base3);
  168. };
  169.  
  170. class MostDerived : public Derived1, public Derived2
  171. {
  172.     public:
  173.         int _mostDerived;
  174.         DEFINE_BASES(MostDerived, Derived1, Derived2);
  175. };
  176.  
  177. int main()
  178. {
  179.     Object*   o = new MostDerived;
  180.     MostDerived* d;
  181.    
  182.     time_t t = clock();
  183.     for (int i = 0; i < 10000000; ++i)
  184.     {
  185.         d = dynamic_cast<MostDerived*>(o);
  186.     }
  187.     printf("%i\n", clock() - t);
  188.  
  189.     t = clock();
  190.     for (int i = 0; i < 10000000; ++i)
  191.     {
  192.         d = my_dynamic_cast<MostDerived*>(o);
  193.     }
  194.     printf("%i\n", clock() - t);
  195.    
  196.     delete d;
  197.  
  198.     return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement