gwnavruntime/blob/blobaggregate.h Source File

blobaggregate.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 
9 // primary contact: GUAL - secondary contact: NOBODY
10 #ifndef Navigation_BlobAggregate_H
11 #define Navigation_BlobAggregate_H
12 
20 
21 namespace Kaim
22 {
23 
24 /* Data at the beginning a BlobAggregate file */
25 class BlobAggregateFileHeader
26 {
27  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Blob)
28 public:
29  static const char* MagicString() { return "BlobAggFile"; }
30 
31 public:
32  BlobAggregateFileHeader();
33  void InitForWrite(Endianness::Type endianness, KyUInt32 blobCount);
34  KyResult CheckAndFixEndianness(bool& isEndiannessSwap);
35 
36 public:
37  char m_magicString[12]; // BlobAggFile0
38  KyUInt32 m_endianness;
39  KyUInt32 m_blobCount;
40 };
41 
42 
43 /* Data just before Blob in a BlobAggregate file */
44 class BlobAggregateBlobHeader
45 {
46  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Blob)
47 public:
48  BlobAggregateBlobHeader();
49  void Clear();
50  KyResult Init(KyUInt32 blobTypeId, KyUInt32 blobTypeVersion, KyUInt32 deepBlobSize, KyUInt32 shallowBlobSize);
51  KyResult Init(const BaseBlobHandler& blobHandler);
52  void SwapEndianness();
53  template<class T> bool Isa() const { return m_blobTypeId == T::GetBlobTypeId(); }
54 
55 public:
56  KyUInt32 m_blobTypeId;
57  KyUInt32 m_blobTypeVersion;
58  KyUInt32 m_deepBlobSize;
59  KyUInt32 m_shallowBlobSize;
60 };
61 
62 
63 class BlobAggregateWriteContext
64 {
65  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Blob)
66 public:
67  BlobAggregateWriteContext()
68  : m_file(KY_NULL)
69  , m_memory(KY_NULL)
70  , m_saveEndianness(Endianness::BigEndian)
71  , m_actuallyWriting(true)
72  {}
73 
74  File* m_file;
75  char* m_memory;
76  Endianness::Type m_saveEndianness;
77  bool m_actuallyWriting;
78 };
79 
80 
81 enum BlobAggregateReadOptions
82 {
83  BLOB_AGGREGATE_IGNORE_UNKNOWN_BLOBTYPES,
84  BLOB_AGGREGATE_WARN_UNKNOWN_BLOBTYPES
85 };
86 
87 class BlobAggregateReadContext
88 {
89  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Blob)
90 public:
91  BlobAggregateReadContext()
92  : m_file(KY_NULL)
93  , m_memory(KY_NULL)
94  , m_memStat(MemStat_Blob)
95  , m_heap(KY_NULL),
96  m_readOptions(BLOB_AGGREGATE_WARN_UNKNOWN_BLOBTYPES)
97  {}
98 
99  File* m_file;
100  char* m_memory;
101  KyInt32 m_memStat;
102  MemoryHeap* m_heap;
103  BlobAggregateReadOptions m_readOptions;
104 };
105 
106 
107 class BlobAggregateBlobCollection
108 {
109  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Blob)
110 public:
111  BlobAggregateBlobCollection(KyUInt32 blobTypeId) : m_blobTypeId(blobTypeId) {}
112  KyUInt32 m_blobTypeId;
113  KyArray<Ptr<BaseBlobHandler>, MemStat_Blob> m_blobHandlers;
114 };
115 
116 class BlobAggregate : public RefCountBase<BlobAggregate, MemStat_Blob>
117 {
118 public:
119  typedef BlobAggregateWriteContext WriteContext;
120  typedef BlobAggregateReadContext ReadContext;
121  typedef BlobAggregateBlobCollection BlobCollection;
122 
123  template<class T> class Collection
124  {
125  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Blob)
126  public:
127  Collection() : m_blobCollection(KY_NULL) {}
128 
129  KyUInt32 GetCount() const { return m_blobCollection ? m_blobCollection->m_blobHandlers.GetCount() : 0; }
130 
131  BlobHandler<T>* GetHandler(UPInt index) { return (BlobHandler<T>*)m_blobCollection->m_blobHandlers[index].GetPtr(); }
132  T* GetBlob(UPInt index) { return (T*)m_blobCollection->m_blobHandlers[index]->VoidBlob(); }
133 
134  BlobCollection* m_blobCollection;
135  };
136 
137 public:
138  BlobAggregate() : m_writeVersionBlob(true) {}
139 
140  virtual ~BlobAggregate() { Clear(); }
141 
142  void Clear();
143 
144  void AddBlob(BaseBlobHandler* blobHandler); // does call AddRef()
145 
146  KyUInt32 ComputeByteSize();
147 
148  KyResult Save(const char* fileName, FileOpenerBase* fileOpener, Endianness::Type endianness = Endianness::BigEndian);
149  KyResult Save(File* file, Endianness::Type endianness = Endianness::BigEndian);
150  KyResult SaveToMemory(void* memory, Endianness::Type endianness = Endianness::BigEndian);
151 
152  KyResult Load(const char* fileName, FileOpenerBase* fileOpener, KyInt32 memStat = MemStat_Blob, MemoryHeap* heap = KY_NULL, BlobAggregateReadOptions readOptions = BLOB_AGGREGATE_WARN_UNKNOWN_BLOBTYPES);
153  KyResult Load(File* file, KyInt32 memStat = MemStat_Blob, MemoryHeap* heap = KY_NULL, BlobAggregateReadOptions readOptions = BLOB_AGGREGATE_WARN_UNKNOWN_BLOBTYPES);
154  KyResult LoadFromMemory(void* memory, BlobAggregateReadOptions readOptions = BLOB_AGGREGATE_WARN_UNKNOWN_BLOBTYPES);
155 
156  template<class T>
157  Collection<T> GetCollection() const
158  {
159  Collection<T> collection;
160  collection.m_blobCollection = GetBlobCollection(T::GetBlobTypeId());
161  return collection;
162  }
163 
164 
165 private:
166  KyUInt32 Write (WriteContext& writeContext);
167  KyUInt32 WriteBlob (WriteContext& writeContext, const BaseBlobHandler& blobHandler) const;
168  KyUInt32 WriteBytes (WriteContext& writeContext, void* src, KyUInt32 size) const;
169 
170  KyResult Read (ReadContext& readContext);
171  void* ReadBytes (ReadContext& readContext, void* memoryOnStack, KyUInt32 size);
172  KyResult IgnoreBytes(ReadContext& readContext, KyUInt32 byteCount);
173 
174  BlobCollection* GetOrCreateCollection(KyUInt32 blobTypeId);
175  BlobCollection* GetBlobCollection(KyUInt32 blobTypeId) const;
176 
177  void UpdateVersionBlob();
178 
179  KyUInt32 GetBlobsCountInCollections() const;
180 
181 private:
182  KyUInt32 m_blobsCountInCollections; // "InCollections" means everything but SdkVersionBlob
183  KyArray<BlobCollection*, MemStat_Blob> m_collections;
184  Ptr<BlobHandler<SdkVersionBlob> > m_versionBlobHandler; // This blob is stored out of m_collections
185  bool m_writeVersionBlob;
186 };
187 
188 } // namespace Kaim
189 
190 
191 #endif // Navigation_BlobAggregate_H
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_NULL
Null value.
Definition: types.h:247
Big-endian format (used, for example, for PlayStation 3, Xbox 360).
Definition: endianness.h:31
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
Type
Enumerates possible endianness types.
Definition: endianness.h:28
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36