1 /*************************************************************************
3 * Copyright 2016 Realm Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 **************************************************************************/
19 #ifndef REALM_OWNED_DATA_HPP
20 #define REALM_OWNED_DATA_HPP
22 #include <realm/util/assert.hpp>
29 /// A chunk of owned data.
32 /// Construct a null reference.
37 /// If \a data_to_copy is 'null', \a data_size must be zero.
38 OwnedData(const char* data_to_copy, size_t data_size)
41 REALM_ASSERT_DEBUG(data_to_copy || data_size == 0);
43 m_data = std::unique_ptr<char[]>(new char[data_size]);
44 memcpy(m_data.get(), data_to_copy, data_size);
48 /// If \a unique_data is 'null', \a data_size must be zero.
49 OwnedData(std::unique_ptr<char[]> unique_data, size_t data_size) noexcept
50 : m_data(std::move(unique_data))
53 REALM_ASSERT_DEBUG(m_data || m_size == 0);
56 OwnedData(const OwnedData& other)
57 : OwnedData(other.m_data.get(), other.m_size)
60 OwnedData& operator=(const OwnedData& other);
62 OwnedData(OwnedData&&) = default;
63 OwnedData& operator=(OwnedData&&) = default;
65 const char* data() const
75 std::unique_ptr<char[]> m_data;
79 inline OwnedData& OwnedData::operator=(const OwnedData& other)
83 m_data = std::unique_ptr<char[]>(new char[other.m_size]);
84 memcpy(m_data.get(), other.m_data.get(), other.m_size);
89 m_size = other.m_size;
96 #endif // REALM_OWNED_DATA_HPP