Status.cpp 2.5 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

K
kun yu 已提交
18
#include "Status.h"
K
kun yu 已提交
19

S
starlord 已提交
20 21
#include <cstring>

K
kun yu 已提交
22 23
namespace milvus {

S
starlord 已提交
24
constexpr int CODE_WIDTH = sizeof(StatusCode);
S
starlord 已提交
25

S
starlord 已提交
26 27 28 29 30
Status::Status(StatusCode code, const std::string& msg) {
    // 4 bytes store code
    // 4 bytes store message length
    // the left bytes store message string
    const uint32_t length = (uint32_t)msg.size();
S
starlord 已提交
31
    auto result = new char[length + sizeof(length) + CODE_WIDTH];
S
starlord 已提交
32 33 34 35 36
    memcpy(result, &code, CODE_WIDTH);
    memcpy(result + CODE_WIDTH, &length, sizeof(length));
    memcpy(result + sizeof(length) + CODE_WIDTH, msg.data(), length);

    state_ = result;
K
kun yu 已提交
37 38
}

S
starlord 已提交
39
Status::Status() : state_(nullptr) {
K
kun yu 已提交
40 41
}

S
starlord 已提交
42
Status::~Status() {
K
kun yu 已提交
43 44 45
    delete state_;
}

S
starlord 已提交
46
Status::Status(const Status& s) : state_(nullptr) {
S
starlord 已提交
47
    CopyFrom(s);
Y
Yu Kun 已提交
48 49
}

S
starlord 已提交
50 51
Status&
Status::operator=(const Status& s) {
S
starlord 已提交
52
    CopyFrom(s);
K
kun yu 已提交
53 54 55
    return *this;
}

S
starlord 已提交
56
Status::Status(Status&& s) : state_(nullptr) {
K
kun yu 已提交
57 58 59
    MoveFrom(s);
}

S
starlord 已提交
60 61
Status&
Status::operator=(Status&& s) {
S
starlord 已提交
62
    MoveFrom(s);
K
kun yu 已提交
63 64 65
    return *this;
}

S
starlord 已提交
66
void
S
starlord 已提交
67
Status::CopyFrom(const Status& s) {
S
starlord 已提交
68 69
    delete state_;
    state_ = nullptr;
S
starlord 已提交
70
    if (s.state_ == nullptr) {
S
starlord 已提交
71
        return;
K
kun yu 已提交
72 73
    }

S
starlord 已提交
74
    uint32_t length = 0;
S
starlord 已提交
75
    memcpy(&length, s.state_ + CODE_WIDTH, sizeof(length));
S
starlord 已提交
76 77
    int buff_len = length + sizeof(length) + CODE_WIDTH;
    state_ = new char[buff_len];
S
starlord 已提交
78
    memcpy(state_, s.state_, buff_len);
K
kun yu 已提交
79 80
}

S
starlord 已提交
81
void
S
starlord 已提交
82
Status::MoveFrom(Status& s) {
K
kun yu 已提交
83
    delete state_;
S
starlord 已提交
84 85
    state_ = s.state_;
    s.state_ = nullptr;
K
kun yu 已提交
86 87
}

S
starlord 已提交
88 89
std::string
Status::message() const {
K
kun yu 已提交
90
    if (state_ == nullptr) {
S
starlord 已提交
91
        return "";
K
kun yu 已提交
92 93
    }

S
starlord 已提交
94
    std::string msg;
S
starlord 已提交
95 96
    uint32_t length = 0;
    memcpy(&length, state_ + CODE_WIDTH, sizeof(length));
S
starlord 已提交
97
    if (length > 0) {
S
starlord 已提交
98
        msg.append(state_ + sizeof(length) + CODE_WIDTH, length);
K
kun yu 已提交
99
    }
S
starlord 已提交
100

S
starlord 已提交
101
    return msg;
K
kun yu 已提交
102 103
}

S
starlord 已提交
104
}  // namespace milvus