MongoDB C++ Driver  legacy-1.1.2
status-inl.h
1 /* Copyright 2012 10gen Inc.
2  *
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 #pragma once
17 
18 namespace mongo {
19 
20 inline Status Status::OK() {
21  return Status();
22 }
23 
24 inline Status::Status(const Status& other) : _error(other._error) {
25  ref(_error);
26 }
27 
28 inline Status& Status::operator=(const Status& other) {
29  ref(other._error);
30  unref(_error);
31  _error = other._error;
32  return *this;
33 }
34 
35 #if __cplusplus >= 201103L
36 inline Status::Status(Status&& other) noexcept : _error(other._error) {
37  other._error = nullptr;
38 }
39 
40 inline Status& Status::operator=(Status&& other) noexcept {
41  unref(_error);
42  _error = other._error;
43  other._error = nullptr;
44  return *this;
45 }
46 #endif // __cplusplus >= 201103L
47 
48 inline Status::~Status() {
49  unref(_error);
50 }
51 
52 inline bool Status::isOK() const {
53  return code() == ErrorCodes::OK;
54 }
55 
56 inline ErrorCodes::Error Status::code() const {
57  return _error ? _error->code : ErrorCodes::OK;
58 }
59 
60 inline std::string Status::codeString() const {
61  return ErrorCodes::errorString(code());
62 }
63 
64 inline std::string Status::reason() const {
65  return _error ? _error->reason : std::string();
66 }
67 
68 inline int Status::location() const {
69  return _error ? _error->location : 0;
70 }
71 
72 inline AtomicUInt32::WordType Status::refCount() const {
73  return _error ? _error->refs.load() : 0;
74 }
75 
76 inline Status::Status() : _error(NULL) {}
77 
78 inline void Status::ref(ErrorInfo* error) {
79  if (error)
80  error->refs.fetchAndAdd(1);
81 }
82 
83 inline void Status::unref(ErrorInfo* error) {
84  if (error && (error->refs.subtractAndFetch(1) == 0))
85  delete error;
86 }
87 
88 inline bool MONGO_CLIENT_FUNC operator==(const ErrorCodes::Error lhs, const Status& rhs) {
89  return rhs == lhs;
90 }
91 
92 inline bool MONGO_CLIENT_FUNC operator!=(const ErrorCodes::Error lhs, const Status& rhs) {
93  return rhs != lhs;
94 }
95 
96 } // namespace mongo
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
Status(ErrorCodes::Error code, const std::string &reason, int location=0)
Builds an error status given the error code, a textual description of what caused the error...
MONGO_CLIENT_API Status(MONGO_CLIENT_FUNC *saslClientAuthenticate)(DBClientWithCommands *client
Attempts to authenticate "client" using the SASL protocol.