1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 namespace mongo {
34 
OK()35 inline Status Status::OK() {
36     return Status();
37 }
38 
Status(const Status & other)39 inline Status::Status(const Status& other) : _error(other._error) {
40     ref(_error);
41 }
42 
43 inline Status& Status::operator=(const Status& other) {
44     ref(other._error);
45     unref(_error);
46     _error = other._error;
47     return *this;
48 }
49 
Status(Status && other)50 inline Status::Status(Status&& other) noexcept : _error(other._error) {
51     other._error = nullptr;
52 }
53 
54 inline Status& Status::operator=(Status&& other) noexcept {
55     unref(_error);
56     _error = other._error;
57     other._error = nullptr;
58     return *this;
59 }
60 
~Status()61 inline Status::~Status() {
62     unref(_error);
63 }
64 
isOK()65 inline bool Status::isOK() const {
66     return !_error;
67 }
68 
code()69 inline ErrorCodes::Error Status::code() const {
70     return _error ? _error->code : ErrorCodes::OK;
71 }
72 
codeString()73 inline std::string Status::codeString() const {
74     return ErrorCodes::errorString(code());
75 }
76 
refCount()77 inline AtomicUInt32::WordType Status::refCount() const {
78     return _error ? _error->refs.load() : 0;
79 }
80 
Status()81 inline Status::Status() : _error(NULL) {}
82 
ref(ErrorInfo * error)83 inline void Status::ref(ErrorInfo* error) {
84     if (error)
85         error->refs.fetchAndAdd(1);
86 }
87 
unref(ErrorInfo * error)88 inline void Status::unref(ErrorInfo* error) {
89     if (error && (error->refs.subtractAndFetch(1) == 0))
90         delete error;
91 }
92 
93 inline bool operator==(const ErrorCodes::Error lhs, const Status& rhs) {
94     return rhs == lhs;
95 }
96 
97 inline bool operator!=(const ErrorCodes::Error lhs, const Status& rhs) {
98     return rhs != lhs;
99 }
100 
101 }  // namespace mongo
102