Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. class Context
  2. {
  3. public:
  4.     template<typename Property>
  5.     void Put(typename Property::value_type& val)
  6.     {
  7.         m_map[typeid(Property)] = std::static_pointer_cast<void>(val);
  8.     }
  9.  
  10.     template<typename Property>
  11.     typename Property::value_type Get() const
  12.     {
  13.         typedef Property::value_type result_type;
  14.         auto it = m_map.find(typeid(Property));
  15.         if (it == m_map.end())
  16.             return result_type();
  17.         return std::static_pointer_cast<typename result_type::element_type>(it->second);
  18.     }
  19. private:
  20.     std::map<std::type_index, std::shared_ptr<void>> m_map;
  21. };
  22.  
  23. struct first_bone
  24. {
  25.     typedef BonePtr value_type;
  26. };
  27.  
  28. void example()
  29. {
  30.     Context ctx;
  31.     BonePtr b;
  32.     ctx.Put<first_bone>(b);
  33.     BonePtr bb = ctx.Get<first_bone>();
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement