1/**
2 * Copyright (c) 2006-2023 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20
21#include "ByteData.h"
22#include "common/Exception.h"
23#include "common/int.h"
24
25#include <string.h>
26
27namespace love
28{
29namespace data
30{
31
32love::Type ByteData::type("ByteData", &Data::type);
33
34ByteData::ByteData(size_t size)
35 : size(size)
36{
37 create();
38 memset(data, 0, size);
39}
40
41ByteData::ByteData(const void *d, size_t size)
42 : size(size)
43{
44 create();
45 memcpy(data, d, size);
46}
47
48ByteData::ByteData(void *d, size_t size, bool own)
49 : size(size)
50{
51 if (own)
52 data = (char *) d;
53 else
54 {
55 create();
56 memcpy(data, d, size);
57 }
58}
59
60ByteData::ByteData(const ByteData &d)
61 : size(d.size)
62{
63 create();
64 memcpy(data, d.data, size);
65}
66
67ByteData::~ByteData()
68{
69 delete[] data;
70}
71
72void ByteData::create()
73{
74 if (size == 0)
75 throw love::Exception("ByteData size must be greater than 0.");
76
77 try
78 {
79 data = new char[size];
80 }
81 catch (std::exception &)
82 {
83 throw love::Exception("Out of memory.");
84 }
85}
86
87ByteData *ByteData::clone() const
88{
89 return new ByteData(*this);
90}
91
92void *ByteData::getData() const
93{
94 return data;
95}
96
97size_t ByteData::getSize() const
98{
99 return size;
100}
101
102} // data
103} // love
104