1 /*
2 * Copyright (c) 2022 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 #include "blob.h"
17
18 #include <securec.h>
19
20 namespace OHOS {
21 namespace DistributedKv {
Blob()22 Blob::Blob()
23 {}
24
Blob(const Blob & blob)25 Blob::Blob(const Blob &blob)
26 {
27 blob_ = blob.Data();
28 }
29
Blob(Blob && blob)30 Blob::Blob(Blob &&blob)
31 {
32 blob_.swap(blob.blob_);
33 }
34
operator =(const Blob & blob)35 Blob &Blob::operator=(const Blob &blob)
36 {
37 // Self-assignment detection
38 if (&blob == this) {
39 return *this;
40 }
41
42 blob_ = blob.Data();
43
44 return *this;
45 }
46
operator =(Blob && blob)47 Blob &Blob::operator=(Blob &&blob)
48 {
49 // Self-assignment detection
50 if (&blob == this) {
51 return *this;
52 }
53
54 blob_.swap(blob.blob_);
55
56 return *this;
57 }
58
Blob(const char * str,size_t n)59 Blob::Blob(const char *str, size_t n) : blob_()
60 {
61 if (str != nullptr) {
62 blob_ = std::vector<uint8_t>(str, str + n);
63 }
64 }
65
Blob(const std::string & str)66 Blob::Blob(const std::string &str) : blob_(str.begin(), str.end())
67 {}
68
Blob(const char * str)69 Blob::Blob(const char *str) : blob_()
70 {
71 if (str != nullptr) {
72 blob_ = std::vector<uint8_t>(str, str + strlen(str));
73 }
74 }
75
Blob(const std::vector<uint8_t> & bytes)76 Blob::Blob(const std::vector<uint8_t> &bytes) : blob_(bytes)
77 {}
78
Blob(std::vector<uint8_t> && bytes)79 Blob::Blob(std::vector<uint8_t> &&bytes) : blob_(std::move(bytes))
80 {}
81
Data() const82 const std::vector<uint8_t> &Blob::Data() const
83 {
84 return blob_;
85 }
86
ToString() const87 std::string Blob::ToString() const
88 {
89 std::string str(blob_.begin(), blob_.end());
90 return str;
91 }
92
93 } // namespace DistributedKv
94 } // namespace OHOS