MongoDB C++ Driver legacy-1.1.1
Loading...
Searching...
No Matches
status.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#include <iosfwd>
19#include <string>
20
21#include "mongo/base/error_codes.h"
22#include "mongo/client/export_macros.h"
23#include "mongo/platform/atomic_word.h"
24
25namespace mongo {
26
50class MONGO_CLIENT_API Status {
51public:
52 // Short-hand for returning an OK status.
53 static inline Status MONGO_CLIENT_FUNC OK();
54
60 Status(ErrorCodes::Error code, const std::string& reason, int location = 0);
61 Status(ErrorCodes::Error code, const char* reason, int location = 0);
62
63 inline Status(const Status& other);
64 inline Status& operator=(const Status& other);
65
66#if __cplusplus >= 201103L
67 inline Status(Status&& other) noexcept;
68 inline Status& operator=(Status&& other) noexcept;
69#endif // __cplusplus >= 201103L
70
71 inline ~Status();
72
77 bool compare(const Status& other) const;
78 bool operator==(const Status& other) const;
79 bool operator!=(const Status& other) const;
80
85 bool compareCode(const ErrorCodes::Error other) const;
86 bool operator==(const ErrorCodes::Error other) const;
87 bool operator!=(const ErrorCodes::Error other) const;
88
89 //
90 // accessors
91 //
92
93 inline bool isOK() const;
94
95 inline ErrorCodes::Error code() const;
96
97 inline std::string codeString() const;
98
99 inline std::string reason() const;
100
101 inline int location() const;
102
103 std::string toString() const;
104
105 //
106 // Below interface used for testing code only.
107 //
108
109 inline AtomicUInt32::WordType refCount() const;
110
111private:
112 inline Status();
113
114 struct ErrorInfo {
115 AtomicUInt32 refs; // reference counter
116 const ErrorCodes::Error code; // error code
117 const std::string reason; // description of error cause
118 const int location; // unique location of the triggering line in the code
119
120 static ErrorInfo* create(ErrorCodes::Error code, const StringData& reason, int location);
121
122 ErrorInfo(ErrorCodes::Error code, const StringData& reason, int location);
123 };
124
125 ErrorInfo* _error;
126
132 static inline void MONGO_CLIENT_FUNC ref(ErrorInfo* error);
133 static inline void MONGO_CLIENT_FUNC unref(ErrorInfo* error);
134};
135
136MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC
137operator==(const ErrorCodes::Error lhs, const Status& rhs);
138
139MONGO_CLIENT_API inline bool MONGO_CLIENT_FUNC
140operator!=(const ErrorCodes::Error lhs, const Status& rhs);
141
142//
143// Convenience method for unittest code. Please use accessors otherwise.
144//
145
146MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC operator<<(std::ostream& os, const Status& status);
147MONGO_CLIENT_API std::ostream& MONGO_CLIENT_FUNC operator<<(std::ostream& os, ErrorCodes::Error);
148
149} // namespace mongo
150
151#include "mongo/base/status-inl.h"
Status represents an error state or the absence thereof.
Definition status.h:50
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,...
bool compare(const Status &other) const
Returns true if 'other's error code and location are equal/different to this instance's.
bool compareCode(const ErrorCodes::Error other) const
Returns true if 'other's error code is equal/different to this instance's.
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents.
Definition string_data.h:43
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:32