gwnavruntime/blob/blobarray.h Source File

blobarray.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 
22 template <class T>
23 class BlobArray
24 {
25  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
26 public:
27  typedef T ValueType;
28 
29  // ------------------------------ Functions -----------------------------
30 
31  BlobArray() : m_count(0), m_offset(0) {}
32 
33  const T* GetValues() const { return (const T*)((char*)&m_offset + m_offset); }
34 
35  T* GetValues() { return (T*)((char*)&m_offset + m_offset); }
36 
37  KyUInt32 GetCount() const { return m_count; }
38 
39 public: // internal
42 };
43 
44 
45 
46 // --------------------------------- inline implementation ---------------------------------
47 
48 template <class T>
49 void SwapEndianness(Endianness::Target e, BlobArray<T>& self)
50 {
52  {
53  // use count and values once they have been swapped
54  SwapEndianness(e, self.m_count);
55  SwapEndianness(e, self.m_offset);
56 
57  T* values = self.GetValues();
58  for (KyUInt32 i = 0; i < self.m_count; ++i)
59  SwapEndianness(e, values[i]);
60  }
61  else
62  {
63  // use count and values before they are swapped
64  KyUInt32 count = self.m_count;
65  T* values = self.GetValues();
66 
67  SwapEndianness(e, self.m_count);
68  SwapEndianness(e, self.m_offset);
69 
70  for (KyUInt32 i = 0; i < count; ++i)
71  SwapEndianness(e, values[i]);
72  }
73 }
74 
75 
76 // Helper template function to initialiaze Array<> from BlobArray<>
77 // Each element of the Array<> is initialized with operator=(blobElement)
78 template <class ArrayT, class BlobArrayT>
79 void InitArrayFromBlobArray_Assign(ArrayT& kyArray, const BlobArrayT& blobArray)
80 {
81  const typename BlobArrayT::ValueType* blobValues = blobArray.GetValues();
82  KyUInt32 blobValuesCount = blobArray.GetCount();
83  kyArray.Clear();
84  kyArray.Resize(blobValuesCount);
85  for (KyUInt32 i = 0; i < blobValuesCount; ++i)
86  kyArray[i] = blobValues[i];
87 }
88 
89 // Helper template function to initialiaze Array<> from BlobArray<>
90 // Each element of the Array<> is initialized with InitFromBlob(blobElement)
91 template <class ArrayT, class BlobArrayT>
92 void InitArrayFromBlobArray_InitFromBlob(ArrayT& kyArray, const BlobArrayT& blobArray)
93 {
94  const typename BlobArrayT::ValueType* blobValues = blobArray.GetValues();
95  KyUInt32 blobValuesCount = blobArray.GetCount();
96  kyArray.Clear();
97  kyArray.Resize(blobValuesCount);
98  for (KyUInt32 i = 0; i < blobValuesCount; ++i)
99  kyArray[i].InitFromBlob(blobValues[i]);
100 }
101 
102 // Helper template function to initialize Array<String> from BlobArray<BlobArray<char> >
103 template <class ArrayT, class BlobArrayT>
104 void InitArrayFromBlobArray_String(ArrayT& kyArray, const BlobArrayT& blobArray)
105 {
106  const typename BlobArrayT::ValueType* blobValues = blobArray.GetValues();
107  KyUInt32 blobValuesCount = blobArray.GetCount();
108  kyArray.Clear();
109  kyArray.Resize(blobValuesCount);
110  for (KyUInt32 i = 0; i < blobValuesCount; ++i)
111  kyArray[i] = blobValues[i].GetValues();
112 }
113 
114 
115 }
116 
KyInt32 m_offset
Set by BLOB_ARRAY and BLOB_ARRAY_COPY during BaseBlobBuilder::DoBuild(). Do not modify.
Definition: blobarray.h:41
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
The same endianness type as the current platform.
Definition: endianness.h:29
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:27
A BlobArray an array that is compatible with the blob serialization framework.
Definition: blobarray.h:23
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::int32_t KyInt32
int32_t
Definition: types.h:24
KyUInt32 m_count
The number of elements in this BlobArray. Set by BLOB_ARRAY and BLOB_ARRAY_COPY during BaseBlobBuilde...
Definition: blobarray.h:40