added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / chunked_binary.hpp
1 /*************************************************************************
2  *
3  * REALM CONFIDENTIAL
4  * __________________
5  *
6  *  [2011] - [2015] Realm Inc
7  *  All Rights Reserved.
8  *
9  * NOTICE:  All information contained herein is, and remains
10  * the property of Realm Incorporated and its suppliers,
11  * if any.  The intellectual and technical concepts contained
12  * herein are proprietary to Realm Incorporated
13  * and its suppliers and may be covered by U.S. and Foreign Patents,
14  * patents in process, and are protected by trade secret or copyright law.
15  * Dissemination of this information or reproduction of this material
16  * is strictly forbidden unless prior written permission is obtained
17  * from Realm Incorporated.
18  *
19  **************************************************************************/
20
21 #ifndef REALM_NOINST_CHUNKED_BINARY_HPP
22 #define REALM_NOINST_CHUNKED_BINARY_HPP
23
24 #include <realm/binary_data.hpp>
25 #include <realm/column_binary.hpp>
26 #include <realm/table.hpp>
27
28 #include <realm/util/buffer_stream.hpp>
29 #include <realm/impl/input_stream.hpp>
30
31
32 namespace realm {
33
34 /// ChunkedBinaryData manages a vector of BinaryData. It is used to facilitate
35 /// extracting large binaries from binary columns and tables.
36 class ChunkedBinaryData {
37 public:
38
39     ChunkedBinaryData();
40     ChunkedBinaryData(const BinaryData& bd);
41     ChunkedBinaryData(const BinaryIterator& bd);
42     ChunkedBinaryData(const BinaryColumn& col, size_t index);
43
44     /// size() returns the number of bytes in the chunked binary.
45     /// FIXME: This operation is O(n).
46     size_t size() const;
47
48     /// is_null returns true if the chunked binary has zero chunks or if
49     /// the first chunk points to the nullptr.
50     bool is_null() const;
51
52     /// FIXME: O(n)
53     char operator[](size_t index) const;
54
55     std::string hex_dump(const char* separator = " ", int min_digits = -1) const;
56
57     void write_to(util::ResettableExpandableBufferOutputStream& out) const;
58
59     /// copy_to() copies the chunked binary data to \a buffer of size
60     /// \a buffer_size starting at \a offset in the ChunkedBinary.
61     /// copy_to() copies until the end of \a buffer or the end of
62     /// the ChunkedBinary whichever comes first.
63     /// copy_to() returns the number of copied bytes.
64     size_t copy_to(char* buffer, size_t buffer_size, size_t offset) const;
65
66     /// copy_to() allocates a buffer of size() in \a dest and
67     /// copies the chunked binary data to \a dest.
68     size_t copy_to(std::unique_ptr<char[]>& dest) const;
69
70     /// get_first_chunk() is used in situations
71     /// where it is known that there is exactly one
72     /// chunk. This is the case if the ChunkedBinary
73     /// has been constructed from BinaryData.
74     BinaryData get_first_chunk() const;
75
76 private:
77     BinaryIterator m_begin;
78     friend class ChunkedBinaryInputStream;
79 };
80
81 // FIXME: When ChunkedBinaryData is moved into Core, this should be moved as well.
82 class ChunkedBinaryInputStream : public _impl::NoCopyInputStream {
83 public:
84     explicit ChunkedBinaryInputStream(const ChunkedBinaryData& chunks)
85         : m_it(chunks.m_begin)
86     {
87     }
88
89     bool next_block(const char*& begin, const char*& end) override
90     {
91         BinaryData block = m_it.get_next();
92         begin = block.data();
93         end = begin + block.size();
94         return begin != end;
95     }
96
97 private:
98     BinaryIterator m_it;
99 };
100
101
102 /// Implementation:
103
104
105 inline ChunkedBinaryData::ChunkedBinaryData()
106 {
107 }
108
109 inline ChunkedBinaryData::ChunkedBinaryData(const BinaryData& bd) : m_begin{bd}
110 {
111 }
112
113 inline ChunkedBinaryData::ChunkedBinaryData(const BinaryIterator& bd) : m_begin{bd}
114 {
115 }
116
117 inline ChunkedBinaryData::ChunkedBinaryData(const BinaryColumn& col, size_t index)
118     : m_begin{&col, index}
119 {
120 }
121
122
123 } // namespace realm
124
125 #endif // REALM_NOINST_CHUNKED_BINARY_HPP