Status.h 1.7 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

X
Xu Peng 已提交
12 13
#pragma once

S
starlord 已提交
14
#include "utils/Error.h"
X
Xu Peng 已提交
15

S
starlord 已提交
16
#include <string>
17

J
jinhai 已提交
18
namespace milvus {
X
Xu Peng 已提交
19

C
Cai Yudong 已提交
20 21 22 23 24 25 26 27 28
class Status;
#define STATUS_CHECK(func) \
    do {                   \
        Status s = func;   \
        if (!s.ok()) {     \
            return s;      \
        }                  \
    } while (false)

S
starlord 已提交
29 30
using StatusCode = ErrorCode;

X
Xu Peng 已提交
31
class Status {
32
 public:
S
starlord 已提交
33
    Status(StatusCode code, const std::string& msg);
C
Cai Yudong 已提交
34 35
    Status() = default;
    virtual ~Status();
X
Xu Peng 已提交
36

S
starlord 已提交
37
    Status(const Status& s);
J
jinhai 已提交
38

C
Cai Yudong 已提交
39
    Status(Status&& s) noexcept;
C
Cai Yudong 已提交
40

S
starlord 已提交
41 42
    Status&
    operator=(const Status& s);
X
Xu Peng 已提交
43

S
starlord 已提交
44
    Status&
C
Cai Yudong 已提交
45
    operator=(Status&& s) noexcept;
J
jinhai 已提交
46 47

    static Status
S
starlord 已提交
48 49 50
    OK() {
        return Status();
    }
J
jinhai 已提交
51

S
starlord 已提交
52
    bool
S
starlord 已提交
53 54 55
    ok() const {
        return state_ == nullptr || code() == 0;
    }
X
Xu Peng 已提交
56

S
starlord 已提交
57
    StatusCode
S
starlord 已提交
58
    code() const {
S
starlord 已提交
59
        return (state_ == nullptr) ? 0 : *(StatusCode*)(state_);
60 61
    }

S
starlord 已提交
62 63 64 65 66 67
    std::string
    message() const;

    std::string
    ToString() const;

S
starlord 已提交
68
 private:
S
starlord 已提交
69
    inline void
S
starlord 已提交
70
    CopyFrom(const Status& s);
X
Xu Peng 已提交
71

S
starlord 已提交
72
    inline void
S
starlord 已提交
73
    MoveFrom(Status& s);
X
Xu Peng 已提交
74

S
starlord 已提交
75
 private:
S
starlord 已提交
76
    char* state_ = nullptr;
S
starlord 已提交
77
};  // Status
X
Xu Peng 已提交
78

S
starlord 已提交
79
}  // namespace milvus