载入中...
搜索中...
未找到
ByteData.cpp
浏览该文件的文档.
1#include "ByteData.h"
2#include <string.h>
3#include <cstdint>
4#include <cstring>
5#include <utility>
6#include "CompressedData.h"
7#include "DataView.h"
8#include "common/Exception.h"
9
10// ByteData.cpp
11namespace eve {
12namespace data {
13
15 create();
16 memset(data, 0, size);
17}
18
19ByteData::ByteData(const void *d, size_t size) : size(size) {
20 create();
21 memcpy(data, d, size);
22}
23
24ByteData::ByteData(void *d, size_t size, bool own) : size(size) {
25 if (own)
26 data = (char *)d;
27 else {
28 create();
29 memcpy(data, d, size);
30 }
31}
32
34 create();
35 memcpy(data, d.data, size);
36}
37
38ByteData::~ByteData() { delete[] data; }
39
40void ByteData::create() {
41 if (size == 0) throw eve::Exception("ByteData size must be greater than 0.");
42
43 try {
44 data = new char[size];
45 } catch (std::exception &) {
46 throw eve::Exception("Create ByteData run out of memory.");
47 }
48}
49
50ByteData *ByteData::clone() const { return new ByteData(*this); }
51
52void *ByteData::getData() const { return data; }
53
54size_t ByteData::getSize() const { return size; }
55
56} // namespace data
57} // namespace eve
58
59// DataView.cpp
60namespace eve {
61namespace data {
62
63
64DataView::DataView(Data *data, size_t offset, size_t size) : data(data), offset(offset), size(size) {
65 if (offset >= data->getSize() || size > data->getSize() || offset > data->getSize() - size)
66 throw eve::Exception("Offset and size of Data View must fit within the original Data's size.");
67
68 if (size == 0) throw eve::Exception("DataView size must be greater than 0.");
69}
70
71DataView::DataView(const DataView &d) : data(d.data), offset(d.offset), size(d.size) {}
72
74
75DataView *DataView::clone() const { return new DataView(*this); }
76
77void *DataView::getData() const { return (uint8_t *)data->getData() + offset; }
78
79size_t DataView::getSize() const { return size; }
80
81} // namespace data
82} // namespace eve
83
84// CompressedData.cpp
85namespace eve {
86namespace data {
87
88CompressedData::CompressedData(std::string format, char* cdata, size_t compressedsize, size_t rawsize, bool own)
89 : format(std::move(format)), data(nullptr), dataSize(compressedsize), originalSize(rawsize) {
90 if (own)
91 data = cdata;
92 else {
93 try {
94 data = new char[dataSize];
95 } catch (std::bad_alloc &) {
96 throw eve::Exception("Create CompressedData run out of memory.");
97 }
98
99 memcpy(data, cdata, dataSize);
100 }
101}
102
104 : format(c.format), data(nullptr), dataSize(c.dataSize), originalSize(c.originalSize) {
105 try {
106 data = new char[dataSize];
107 } catch (std::bad_alloc &) {
108 throw eve::Exception("Copy CompressedData run out of memory.");
109 }
110
111 memcpy(data, c.data, dataSize);
112}
113
115
117
118std::string CompressedData::getFormat() const { return format; }
119
120size_t CompressedData::getDecompressedSize() const { return originalSize; }
121
122void *CompressedData::getData() const { return data; }
123
124size_t CompressedData::getSize() const { return dataSize; }
125
126} // namespace data
127} // namespace eve
std::int32_t c
size_t offset
float d
float size
Definition TreeMesh.cpp:158
Data public API.
Definition Data.h:8
virtual void * getData() const =0
Gets a pointer to the data. This pointer will obviously not be valid if the Data object is destroyed.
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
In-memory byte buffer implementing eve::Data (ref-counted).
Definition ByteData.h:13
void * getData() const override
Returns the data.
Definition ByteData.cpp:52
size_t getSize() const override
Returns the size.
Definition ByteData.cpp:54
ByteData(size_t size)
Allocates an uninitialized buffer of the given size.
Definition ByteData.cpp:14
virtual ~ByteData()
Byte data.
Definition ByteData.cpp:38
ByteData * clone() const override
Implements eve::Data.
Definition ByteData.cpp:50
Stores byte data compressed via DataModule::compress.
CompressedData * clone() const override
Deep copy. @ownership Caller deletes.
Definition ByteData.cpp:116
virtual ~CompressedData()
Compressed data.
Definition ByteData.cpp:114
CompressedData(std::string format, char *cdata, size_t compressedsize, size_t rawsize, bool own=true)
Constructor just stores already-compressed data in the object.
Definition ByteData.cpp:88
size_t getDecompressedSize() const
Gets the original (uncompressed) size of the compressed data. May return 0 if the uncompressed size i...
Definition ByteData.cpp:120
void * getData() const override
Returns the data.
Definition ByteData.cpp:122
size_t getSize() const override
Returns the size.
Definition ByteData.cpp:124
std::string getFormat() const
Gets the format that was used to compress the data.
Definition ByteData.cpp:118
Contains a reference to a subsection of an existing Data object.
Definition DataView.h:16
DataView(Data *data, size_t offset, size_t size)
Data view.
Definition ByteData.cpp:64
DataView * clone() const override
Deep copy. @ownership Caller deletes.
Definition ByteData.cpp:75
size_t getSize() const override
Returns the size.
Definition ByteData.cpp:79
virtual ~DataView()
Data view.
Definition ByteData.cpp:73
void * getData() const override
Returns the data.
Definition ByteData.cpp:77
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16