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_ARRAY_BLOB_HPP
20 #define REALM_ARRAY_BLOB_HPP
22 #include <realm/array.hpp>
27 class ArrayBlob : public Array {
29 static constexpr size_t max_binary_size = 0xFFFFF8 - Array::header_size;
31 explicit ArrayBlob(Allocator&) noexcept;
32 ~ArrayBlob() noexcept override
36 // Disable copying, this is not allowed.
37 ArrayBlob& operator=(const ArrayBlob&) = delete;
38 ArrayBlob(const ArrayBlob&) = delete;
40 const char* get(size_t index) const noexcept;
41 BinaryData get_at(size_t& pos) const noexcept;
42 bool is_null(size_t index) const noexcept;
43 ref_type add(const char* data, size_t data_size, bool add_zero_term = false);
44 void insert(size_t pos, const char* data, size_t data_size, bool add_zero_term = false);
45 ref_type replace(size_t begin, size_t end, const char* data, size_t data_size, bool add_zero_term = false);
46 void erase(size_t begin, size_t end);
48 /// Get the specified element without the cost of constructing an
49 /// array instance. If an array instance is already available, or
50 /// you need to get multiple values, then this method will be
52 static const char* get(const char* header, size_t index) noexcept;
54 /// Create a new empty blob (binary) array and attach this
55 /// accessor to it. This does not modify the parent reference
56 /// information of this accessor.
58 /// Note that the caller assumes ownership of the allocated
59 /// underlying node. It is not owned by the accessor.
62 /// Construct a blob of the specified size and return just the
63 /// reference to the underlying memory. All bytes will be
64 /// initialized to zero.
65 static MemRef create_array(size_t init_size, Allocator&);
67 size_t blob_size() const noexcept;
70 void to_dot(std::ostream&, StringData title = StringData()) const;
74 size_t calc_byte_len(size_t for_size, size_t width) const override;
75 size_t calc_item_count(size_t bytes, size_t width) const noexcept override;
81 // Creates new array (but invalid, call init_from_ref() to init)
82 inline ArrayBlob::ArrayBlob(Allocator& allocator) noexcept
87 inline bool ArrayBlob::is_null(size_t index) const noexcept
89 return (get(index) == nullptr);
92 inline const char* ArrayBlob::get(size_t index) const noexcept
94 return m_data + index;
97 inline ref_type ArrayBlob::add(const char* data, size_t data_size, bool add_zero_term)
99 return replace(m_size, m_size, data, data_size, add_zero_term);
102 inline void ArrayBlob::insert(size_t pos, const char* data, size_t data_size, bool add_zero_term)
104 replace(pos, pos, data, data_size, add_zero_term);
107 inline void ArrayBlob::erase(size_t begin, size_t end)
109 const char* data = nullptr;
110 size_t data_size = 0;
111 replace(begin, end, data, data_size);
114 inline const char* ArrayBlob::get(const char* header, size_t pos) noexcept
116 const char* data = get_data_from_header(header);
120 inline void ArrayBlob::create()
122 size_t init_size = 0;
123 MemRef mem = create_array(init_size, get_alloc()); // Throws
127 inline MemRef ArrayBlob::create_array(size_t init_size, Allocator& allocator)
129 bool context_flag = false;
130 int_fast64_t value = 0;
131 return Array::create(type_Normal, context_flag, wtype_Ignore, init_size, value, allocator); // Throws
134 inline size_t ArrayBlob::calc_byte_len(size_t for_size, size_t) const
136 return header_size + for_size;
139 inline size_t ArrayBlob::calc_item_count(size_t bytes, size_t) const noexcept
141 return bytes - header_size;
147 #endif // REALM_ARRAY_BLOB_HPP