1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
6 | #include "Error/BsException.h" |
7 | |
8 | namespace bs |
9 | { |
10 | /** @addtogroup General |
11 | * @{ |
12 | */ |
13 | |
14 | /** Serializable blob of raw memory. */ |
15 | struct DataBlob |
16 | { |
17 | UINT8* data = nullptr; |
18 | UINT32 size = 0; |
19 | }; |
20 | |
21 | /** @} */ |
22 | |
23 | /** @cond RTTI */ |
24 | /** @addtogroup RTTI-Impl-Core |
25 | * @{ |
26 | */ |
27 | |
28 | template<> struct RTTIPlainType<DataBlob> |
29 | { |
30 | enum { id = TID_DataBlob }; enum { hasDynamicSize = 1 }; |
31 | |
32 | static void toMemory(const DataBlob& data, char* memory) |
33 | { |
34 | UINT32 size = getDynamicSize(data); |
35 | |
36 | memcpy(memory, &size, sizeof(UINT32)); |
37 | memory += sizeof(UINT32); |
38 | |
39 | memcpy(memory, data.data, data.size); |
40 | } |
41 | |
42 | static UINT32 fromMemory(DataBlob& data, char* memory) |
43 | { |
44 | UINT32 size; |
45 | memcpy(&size, memory, sizeof(UINT32)); |
46 | memory += sizeof(UINT32); |
47 | |
48 | if(data.data != nullptr) |
49 | bs_free(data.data); |
50 | |
51 | data.size = size - sizeof(UINT32); |
52 | data.data = (UINT8*)bs_alloc(data.size); |
53 | memcpy(data.data, memory, data.size); |
54 | |
55 | return size; |
56 | } |
57 | |
58 | static UINT32 getDynamicSize(const DataBlob& data) |
59 | { |
60 | UINT64 dataSize = data.size + sizeof(UINT32); |
61 | |
62 | #if BS_DEBUG_MODE |
63 | if(dataSize > std::numeric_limits<UINT32>::max()) |
64 | { |
65 | BS_EXCEPT(InternalErrorException, "Data overflow! Size doesn't fit into 32 bits." ); |
66 | } |
67 | #endif |
68 | |
69 | return (UINT32)dataSize; |
70 | } |
71 | }; |
72 | |
73 | /** @} */ |
74 | /** @endcond */ |
75 | } |