Advertisement
SolidDesu

datahandle.h

Mar 13th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #ifndef _DATAHANDLE_H_
  2. #define _DATAHANDLE_H_
  3.  
  4. #include <vector>
  5. #include <string>
  6.  
  7. template<typename DATA>
  8. class Data
  9. {
  10. public:
  11.     Data() : handle(0){}
  12.     DATA* operator->();
  13.     void Release();
  14.  
  15.     static Data<DATA> Create();
  16.     static Data<DATA> Create(DATA value);
  17. private:
  18.     Data(unsigned int index) : index(index), magic(++Data<DATA>::magic_next){}
  19.     union
  20.     {
  21.         struct
  22.         {
  23.             unsigned int index : 16;
  24.             unsigned int magic : 16;
  25.         };
  26.         unsigned int handle;
  27.     };
  28.  
  29.     //
  30.     static std::vector<DATA> data_storage;
  31.     static std::vector<unsigned int> magic_storage;
  32.     static std::vector<unsigned int> free_storage;
  33.     static unsigned int magic_next;
  34. };
  35.  
  36. template<typename DATA>
  37. std::vector<DATA> Data<DATA>::data_storage;
  38. template<typename DATA>
  39. std::vector<unsigned int> Data<DATA>::magic_storage;
  40. template<typename DATA>
  41. std::vector<unsigned int> Data<DATA>::free_storage;
  42. template<typename DATA>
  43. unsigned int Data<DATA>::magic_next = 0;
  44.  
  45. template<typename DATA>
  46. DATA* Data<DATA>::operator->()
  47. {
  48.     if (magic_storage[index] != magic)
  49.         return 0;
  50.     return &(data_storage[index]);
  51. }
  52.  
  53. template<typename DATA>
  54. void Data<DATA>::Release()
  55. {
  56.     if (magic == 0)
  57.         return;
  58.     if (Data<DATA>::magic_storage[index] != magic)
  59.         return;
  60.     Data<DATA>::magic_storage[index] = 0;
  61.     Data<DATA>::free_storage.push_back(index);
  62.     Data<DATA>::data_storage[index] = DATA();
  63. }
  64.  
  65. template<typename DATA>
  66. Data<DATA> Data<DATA>::Create()
  67. {
  68.     Data<DATA> resource;
  69.     unsigned int index;
  70.     if (free_storage.empty())
  71.     {
  72.         index = data_storage.size();
  73.         data_storage.push_back(DATA());
  74.         resource = Data<DATA>(index);
  75.         magic_storage.push_back(resource.magic);
  76.     }
  77.     else
  78.     {
  79.         index = free_storage.back();
  80.         free_storage.pop_back();
  81.         resource = Data<DATA>(index);
  82.         magic_storage[index] = resource.magic;
  83.     }
  84.  
  85.     return resource;
  86. }
  87.  
  88. template<typename DATA>
  89. Data<DATA> Data<DATA>::Create(DATA value)
  90. {
  91.     Data<DATA> resource;
  92.     unsigned int index;
  93.     if (free_storage.empty())
  94.     {
  95.         index = data_storage.size();
  96.         data_storage.push_back(value);
  97.         resource = Data<DATA>(index);
  98.         magic_storage.push_back(resource.magic);
  99.     }
  100.     else
  101.     {
  102.         index = free_storage.back();
  103.         free_storage.pop_back();
  104.         resource = Data<DATA>(index);
  105.         data_storage[index] = value;
  106.         magic_storage[index] = resource.magic;
  107.     }
  108.  
  109.     return resource;
  110. }
  111.  
  112. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement