2d3067749e096b86b68c1658b3ad0da218e86f42.svn-base
2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*************************************************************************
*
* REALM CONFIDENTIAL
* __________________
*
* [2011] - [2015] Realm Inc
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Realm Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Realm Incorporated
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Realm Incorporated.
*
**************************************************************************/
#ifndef REALM_OWNED_DATA_HPP
#define REALM_OWNED_DATA_HPP
#include <cstring>
#include <memory>
namespace realm {
/// A chunk of owned data.
class OwnedData {
public:
/// Construct a null reference.
OwnedData() noexcept {}
/// If \a data is 'null', \a size must be zero.
OwnedData(const char* data, size_t size) : m_size(size)
{
REALM_ASSERT_DEBUG(data || size == 0);
if (data) {
m_data = std::unique_ptr<char[]>(new char[size]);
memcpy(m_data.get(), data, size);
}
}
/// If \a data is 'null', \a size must be zero.
OwnedData(std::unique_ptr<char[]> data, size_t size) noexcept :
m_data(std::move(data)), m_size(size)
{
REALM_ASSERT_DEBUG(m_data || m_size == 0);
}
OwnedData(const OwnedData& other) : OwnedData(other.m_data.get(), other.m_size) { }
OwnedData& operator=(const OwnedData& other);
OwnedData(OwnedData&&) = default;
OwnedData& operator=(OwnedData&&) = default;
const char* data() const { return m_data.get(); }
size_t size() const { return m_size; }
private:
std::unique_ptr<char[]> m_data;
size_t m_size = 0;
};
inline OwnedData& OwnedData::operator=(const OwnedData& other)
{
if (this != &other) {
if (other.m_data) {
m_data = std::unique_ptr<char[]>(new char[other.m_size]);
memcpy(m_data.get(), other.m_data.get(), other.m_size);
} else {
m_data = nullptr;
}
m_size = other.m_size;
}
return *this;
}
} // namespace realm
#endif // REALM_OWNED_DATA_HPP