gwnavruntime/blob/userdatablobregistry.h Source File

userdatablobregistry.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #pragma once
8 
11 
12 namespace Kaim
13 {
14 
15 class UserDataBlobCategory
16 {
17  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
18 
19 public:
20  UserDataBlobCategory(BlobCategory* blobCategory);
21 
22  void SetUserData(KyUInt32 blobTypeId, void* userData)
23  {
24  KyUInt32 blobEnum = BlobTypeIdUtils::GetBlobEnum(blobTypeId);
25  m_userDatas[blobEnum] = userData;
26  }
27 
28  void* GetUserData(KyUInt32 blobTypeId) const
29  {
30  KyUInt32 blobEnum = BlobTypeIdUtils::GetBlobEnum(blobTypeId);
31  return m_userDatas[blobEnum];
32  }
33 
34 public: // internal
35  BlobCategory* m_blobCategory;
36  KyArray<void*> m_userDatas;
37 };
38 
39 
40 class UserDataBlobNameSpace
41 {
42  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
43 
44 public:
45  UserDataBlobNameSpace(BlobNameSpace* blobNameSpace);
46  ~UserDataBlobNameSpace();
47  KyResult SetUserData(KyUInt32 blobTypeId, void* userData);
48  void* GetUserData(KyUInt32 blobTypeId) const;
49 
50 public: // internal
51  BlobNameSpace* m_blobNameSpace;
52  KyArray<UserDataBlobCategory*> m_userDataCategories;
53 };
54 
55 
56 class UserDataBlobRegistry
57 {
58  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
59 
60 public:
61  ~UserDataBlobRegistry();
62  KyResult Set(KyUInt32 blobTypeId, void* userData);
63  void* Get(KyUInt32 blobTypeId);
64 
65 public: // internal
66  UserDataBlobNameSpace* GetOrCreateUserDataNameSpace(BlobNameSpace* blobNameSpace);
67  UserDataBlobNameSpace* GetUserDataNameSpace(BlobNameSpace* blobNameSpace);
68 
69  KyArray<UserDataBlobNameSpace*> m_userDataNameSpaces;
70 };
71 
72 class UserBlobRegistryInternal
73 {
74 public: // internal
75  static KyResult CheckCollisions(UserDataBlobRegistry& current, UserDataBlobRegistry& other);
76  static void TransferContent(UserDataBlobRegistry& current, UserDataBlobRegistry& other);
77  static void Clear(UserDataBlobRegistry& current);
78 };
79 
80 class UserBlobRegistryIdx
81 {
82 public:
83  UserBlobRegistryIdx()
84  : m_nameSpaceIdx(0)
85  , m_categoryIdx(0)
86  , m_blobTypeEnum(0)
87  , m_blobTypeId(KyUInt32MAXVAL)
88  {}
89 
90  KyUInt32 GetBlobTypeId()
91  {
92  return m_blobTypeId;
93  }
94 
95 public: //internal
96  void* GetAndAdvance(const UserDataBlobRegistry& registry);
97 
98  KyUInt32 m_nameSpaceIdx;
99  KyUInt32 m_categoryIdx;
100  KyUInt32 m_blobTypeEnum;
101 
102  KyUInt32 m_blobTypeId;
103 };
104 
105 
106 class UserBlobRegistryRefCountBase : public RefCountBase<UserBlobRegistryRefCountBase, Stat_Default_Mem>
107 {
108 };
109 
110 
111 template<class T, class TOwned, bool IsTOwnedAutodelete>
112 class UserBlobRegistryBase : public UserBlobRegistryRefCountBase
113 {
114  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
115 public:
116  typedef UserBlobRegistryBase<T, TOwned, IsTOwnedAutodelete> SelfType;
117 
118  ~UserBlobRegistryBase()
119  {
120  Clear();
121  }
122 
123  KyResult SetVal(KyUInt32 blobTypeId, T* object)
124  {
125  if (m_userDatas.Set(blobTypeId, object) == KY_ERROR)
126  return KY_ERROR;
127  m_owned.PushBack(object);
128  return KY_SUCCESS;
129  }
130 
131  KyResult SetRef(KyUInt32 blobTypeId, T* object)
132  {
133  return m_userDatas.Set(blobTypeId, object);
134  }
135 
136  template<class BlobT>
137  KyResult SetVal(T* object)
138  {
139  return SetVal(BlobT::GetBlobTypeId(), object);
140  }
141 
142  template<class BlobT>
143  KyResult SetRef(T* object)
144  {
145  return SetRef(BlobT::GetBlobTypeId(), object);
146  }
147 
148  T* Get(KyUInt32 blobTypeId)
149  {
150  return (T*)m_userDatas.Get(blobTypeId);
151  }
152 
154  KyResult TransferContent(SelfType& fromRegistry)
155  {
156  // Check if this registry already contains elements from the given registry!
157  KyResult result = UserBlobRegistryInternal::CheckCollisions(m_userDatas, fromRegistry.m_userDatas);
158  if (result == KY_ERROR)
159  return result;
160 
161 
162  // If no collision, transfer the content,
163  // First, transfer owned data
164  m_owned.Reserve(m_owned.GetCount() + fromRegistry.m_owned.GetCount());
165  m_owned.Append(fromRegistry.m_owned);
166  fromRegistry.m_owned.Clear();
167 
168  // Second set userdata
169  UserBlobRegistryInternal::TransferContent(m_userDatas, fromRegistry.m_userDatas);
170 
171  return KY_SUCCESS;
172  }
173 
174  bool IsEmpty()
175  {
176  return m_userDatas.m_userDataNameSpaces.IsEmpty();
177  }
178 
179  void Clear()
180  {
181  UserBlobRegistryInternal::Clear(m_userDatas);
182  if (IsTOwnedAutodelete == false)
183  {
184  for (KyUInt32 i = 0; i < m_owned.GetCount(); ++i)
185  delete m_owned[i];
186  }
187  m_owned.Clear();
188  }
189 
190  T* GetAndAdvance(UserBlobRegistryIdx& idx) const
191  {
192  return (T*) idx.GetAndAdvance(m_userDatas);
193  }
194 
195 
196 public:
197  UserDataBlobRegistry m_userDatas;
198  KyArray<TOwned> m_owned;
199 };
200 
201 template<class T>
202 class UserBlobRegistry : public UserBlobRegistryBase<T, T*, false>
203 {
204 
205 };
206 
207 template<class T>
208 class UserPtrBlobRegistry : public UserBlobRegistryBase<T, Ptr<T>, true>
209 {
210 
211 };
212 
213 
214 }
215 
216 
217 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KY_ERROR
use result == KY_ERROR to test for error
Definition: types.h:132
#define KyUInt32MAXVAL
KyUInt32 max value
Definition: types.h:68