gwnavruntime/blob/userdatablobregistry.h Source File

userdatablobregistry.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 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 
8 // primary contact: GUAL - secondary contact: NOBODY
9 #ifndef Navigation_UserDataBlobRegistry_H
10 #define Navigation_UserDataBlobRegistry_H
11 
12 
15 
16 
17 namespace Kaim
18 {
19 
20 
21 class UserDataBlobCategory
22 {
23  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
24 
25 public:
26  UserDataBlobCategory(BlobCategory* blobCategory);
27 
28  void SetUserData(KyUInt32 blobTypeId, void* userData)
29  {
30  KyUInt32 blobEnum = BlobTypeIdUtils::GetBlobEnum(blobTypeId);
31  m_userDatas[blobEnum] = userData;
32  }
33 
34  void* GetUserData(KyUInt32 blobTypeId) const
35  {
36  KyUInt32 blobEnum = BlobTypeIdUtils::GetBlobEnum(blobTypeId);
37  return m_userDatas[blobEnum];
38  }
39 
40 public: // internal
41  BlobCategory* m_blobCategory;
42  KyArray<void*> m_userDatas;
43 };
44 
45 
46 class UserDataBlobNameSpace
47 {
48  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
49 
50 public:
51  UserDataBlobNameSpace(BlobNameSpace* blobNameSpace);
52  ~UserDataBlobNameSpace();
53  KyResult SetUserData(KyUInt32 blobTypeId, void* userData);
54  void* GetUserData(KyUInt32 blobTypeId) const;
55 
56 public: // internal
57  BlobNameSpace* m_blobNameSpace;
58  KyArray<UserDataBlobCategory*> m_userDataCategories;
59 };
60 
61 
62 class UserDataBlobRegistry
63 {
64  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
65 
66 public:
67  ~UserDataBlobRegistry();
68  KyResult Set(KyUInt32 blobTypeId, void* userData);
69  void* Get(KyUInt32 blobTypeId);
70 
71 public: // internal
72  UserDataBlobNameSpace* GetOrCreateUserDataNameSpace(BlobNameSpace* blobNameSpace);
73  UserDataBlobNameSpace* GetUserDataNameSpace(BlobNameSpace* blobNameSpace);
74 
75  KyArray<UserDataBlobNameSpace*> m_userDataNameSpaces;
76 };
77 
78 class UserBlobRegistryInternal
79 {
80 public: // internal
81  static KyResult CheckCollisions(UserDataBlobRegistry& current, UserDataBlobRegistry& other);
82  static void TransferContent(UserDataBlobRegistry& current, UserDataBlobRegistry& other);
83  static void Clear(UserDataBlobRegistry& current);
84 };
85 
86 class UserBlobRegistryIdx
87 {
88 public:
89  UserBlobRegistryIdx()
90  : m_nameSpaceIdx(0)
91  , m_categoryIdx(0)
92  , m_blobTypeEnum(0)
93  , m_blobTypeId(KyUInt32MAXVAL)
94  {}
95 
96  KyUInt32 GetBlobTypeId()
97  {
98  return m_blobTypeId;
99  }
100 
101 public: //internal
102  void* GetAndAdvance(const UserDataBlobRegistry& registry);
103 
104  KyUInt32 m_nameSpaceIdx;
105  KyUInt32 m_categoryIdx;
106  KyUInt32 m_blobTypeEnum;
107 
108  KyUInt32 m_blobTypeId;
109 };
110 
111 
112 class UserBlobRegistryRefCountBase : public RefCountBase<UserBlobRegistryRefCountBase, Stat_Default_Mem>
113 {
114 };
115 
116 
117 template<class T, class TOwned, bool IsTOwnedAutodelete>
118 class UserBlobRegistryBase : public UserBlobRegistryRefCountBase
119 {
120  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
121 public:
122  typedef UserBlobRegistryBase<T, TOwned, IsTOwnedAutodelete> SelfType;
123 
124  ~UserBlobRegistryBase()
125  {
126  Clear();
127  }
128 
129  KyResult SetVal(KyUInt32 blobTypeId, T* object)
130  {
131  if (KY_FAILED(m_userDatas.Set(blobTypeId, object)))
132  return KY_ERROR;
133  m_owned.PushBack(object);
134  return KY_SUCCESS;
135  }
136 
137  KyResult SetRef(KyUInt32 blobTypeId, T* object)
138  {
139  return m_userDatas.Set(blobTypeId, object);
140  }
141 
142  template<class BlobT>
143  KyResult SetVal(T* object)
144  {
145  return SetVal(BlobT::GetBlobTypeId(), object);
146  }
147 
148  template<class BlobT>
149  KyResult SetRef(T* object)
150  {
151  return SetRef(BlobT::GetBlobTypeId(), object);
152  }
153 
154  T* Get(KyUInt32 blobTypeId)
155  {
156  return (T*)m_userDatas.Get(blobTypeId);
157  }
158 
160  KyResult TransferContent(SelfType& fromRegistry)
161  {
162  // Check if this registry already contains elements from the given registry!
163  KyResult result = UserBlobRegistryInternal::CheckCollisions(m_userDatas, fromRegistry.m_userDatas);
164  if (KY_FAILED(result))
165  return result;
166 
167 
168  // If no collision, transfer the content,
169  // First, transfer owned data
170  m_owned.Reserve(m_owned.GetCount() + fromRegistry.m_owned.GetCount());
171  m_owned.Append(fromRegistry.m_owned);
172  fromRegistry.m_owned.Clear();
173 
174  // Second set userdata
175  UserBlobRegistryInternal::TransferContent(m_userDatas, fromRegistry.m_userDatas);
176 
177  return KY_SUCCESS;
178  }
179 
180  bool IsEmpty()
181  {
182  return m_userDatas.m_userDataNameSpaces.IsEmpty();
183  }
184 
185  void Clear()
186  {
187  UserBlobRegistryInternal::Clear(m_userDatas);
188  if (IsTOwnedAutodelete == false)
189  {
190  for (KyUInt32 i = 0; i < m_owned.GetCount(); ++i)
191  delete m_owned[i];
192  }
193  m_owned.Clear();
194  }
195 
196  T* GetAndAdvance(UserBlobRegistryIdx& idx) const
197  {
198  return (T*) idx.GetAndAdvance(m_userDatas);
199  }
200 
201 
202 public:
203  UserDataBlobRegistry m_userDatas;
204  KyArray<TOwned> m_owned;
205 };
206 
207 template<class T>
208 class UserBlobRegistry : public UserBlobRegistryBase<T, T*, false>
209 {
210 
211 };
212 
213 template<class T>
214 class UserPtrBlobRegistry : public UserBlobRegistryBase<T, Ptr<T>, true>
215 {
216 
217 };
218 
219 
220 }
221 
222 
223 #endif
224 
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
#define KY_FAILED(expression)
Shorthand for Kaim::Result::Fail().
Definition: types.h:274
Definition: gamekitcrowddispersion.h:20
#define KY_SUCCESS
Shorthand for Kaim::Result::Success.
Definition: types.h:273
#define KY_ERROR
Shorthand for Kaim::Result::Failure.
Definition: types.h:272
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
#define KyUInt32MAXVAL
The maximum value that can be stored in the KyUInt32 variable type.
Definition: types.h:226