1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef DISTRIBUTED_KV_BLOB_H
17 #define DISTRIBUTED_KV_BLOB_H
18 
19 #include <string>
20 #include <vector>
21 #include "visibility.h"
22 
23 namespace OHOS {
24 namespace DistributedKv {
25 
26 class Blob  {
27 public:
28     /**
29      * @brief Constructor.
30      */
31     API_EXPORT Blob();
32 
33     /**
34      * @brief Destructor.
35      */
36     API_EXPORT ~Blob() = default;
37 
38     /**
39      * @brief Copy constructor for Blob.
40      */
41     API_EXPORT Blob(const Blob &blob);
42 
43     /**
44      * @brief Operator =.
45      */
46     API_EXPORT Blob &operator=(const Blob &blob);
47 
48     /**
49      * @brief Move constructor for Blob.
50      */
51     API_EXPORT Blob(Blob &&blob);
52 
53     /**
54      * @brief Operator =.
55      */
56     API_EXPORT Blob &operator=(Blob &&blob);
57 
58     /**
59      * @brief Construct a Blob use std::string.
60     */
61     API_EXPORT Blob(const std::string &str);
62 
63     /**
64      * @brief Operator =.
65      */
66     API_EXPORT Blob &operator=(const std::string &str);
67 
68     /**
69      * @brief Construct a Blob use char pointer and len.
70     */
71     API_EXPORT Blob(const char *str, size_t n);
72 
73     /**
74      * @brief Construct a Blob use char pointer.
75     */
76     API_EXPORT Blob(const char *str);
77 
78     /**
79      * @brief Operator =.
80      */
81     API_EXPORT Blob &operator=(const char *str);
82 
83     /**
84      * @brief Construct a Blob use std::vector<uint8_t>.
85     */
86     API_EXPORT Blob(const std::vector<uint8_t> &bytes);
87 
88     /**
89      * @brief Construct a Blob use std::vector<uint8_t>.
90     */
91     API_EXPORT Blob(std::vector<uint8_t> &&bytes);
92 
93     /**
94      * @brief Return a reference to the data of the blob.
95     */
96     API_EXPORT const std::vector<uint8_t> &Data() const;
97 
98     /**
99      * @brief Return std::vector<uint8_t> &.
100      */
101     API_EXPORT operator const std::vector<uint8_t> &() const;
102 
103     /**
104      * @brief Return std::vector<uint8_t> &&.
105      */
106     API_EXPORT operator std::vector<uint8_t> &&() noexcept;
107 
108     /**
109      * @brief Return the length of the referenced data(unit: byte).
110      * @return The length of the referenced data.
111     */
112     API_EXPORT size_t Size() const;
113 
114     /**
115      * @brief Return the occupied length when write this blob to rawdata.
116      * @return The occupied length.
117     */
118     int RawSize() const;
119 
120     /**
121      * @brief Indicate the referenced data is zero or not.
122      * @return Return true if the length of the referenced data is zero
123      *         otherwise return false.
124     */
125     API_EXPORT bool Empty() const;
126 
127     /**
128      * @brief  Return the the byte in the referenced data.
129      * @param n the number of byte(n < {@link size_t Size()})
130     */
131     API_EXPORT uint8_t operator[](size_t n) const;
132 
133     /**
134      * @brief Operator ==.
135      */
136     API_EXPORT bool operator==(const Blob &) const;
137 
138     /**
139      * @brief Change this blob to refer to an empty array.
140     */
141     API_EXPORT void Clear();
142 
143     /**
144      * @brief Change vector<uint8_t> to std::string.
145      * @return The string value.
146     */
147     API_EXPORT std::string ToString() const;
148 
149     /**
150      * @brief Comparison. Returns value.
151      *
152      * <  0 if "*this" <  "blob".
153      * == 0 if "*this" == "blob".
154      * >  0 if "*this" >  "blob".
155     */
156     API_EXPORT int Compare(const Blob &blob) const;
157 
158     /**
159      * @brief Whether the input blob is a prefix of "*this" or not.
160      * @param blob The input blob value.
161      * @return True if "blob" is a prefix of "*this", otherwise false.
162     */
163     API_EXPORT bool StartsWith(const Blob &blob) const;
164 
165     /**
166      * @brief Write blob size and data to memory buffer.
167      * @param cursorPtr The pointer of the buffer.
168      * @param bufferLeftSize The size of unused buffer.
169      * @return Return false if bufferLeftSize not enough, otherwise true.
170     */
171     bool WriteToBuffer(uint8_t *&cursorPtr, int &bufferLeftSize) const;
172 
173     /**
174      * @brief Read a blob from memory buffer.
175      * @param cursorPtr The pointer of the buffer.
176      * @param bufferLeftSize The available buffer.
177      * @return Return false if bufferLeftSize not enough, otherwise true.
178     */
179     bool ReadFromBuffer(const uint8_t *&cursorPtr, int &bufferLeftSize);
180 
181 private:
182     std::vector<uint8_t> blob_;
183 };
184 }  // namespace DistributedKv
185 }  // namespace OHOS
186 
187 #endif  // DISTRIBUTED_KV_BLOB_H
188