gwnavruntime/blob/blobaggregate.h Source File

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