RMQ - RabbitMQ C++ Library
rmqt_result.h
1// Copyright 2020-2023 Bloomberg Finance L.P.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16// rmqt_result.h
17#ifndef INCLUDED_RMQT_RESULT
18#define INCLUDED_RMQT_RESULT
19
20#include <bsl_functional.h>
21#include <bsl_memory.h>
22#include <bsl_ostream.h>
23#include <bsl_string.h>
24
25namespace BloombergLP {
26namespace rmqt {
27
28typedef enum { TIMEOUT = 408 } ReturnCodes;
29
35
36template <typename T = void>
37class Result {
38 private:
41 typedef void (Result::*BoolType)() const;
42 void thisTypeDoesNotSupportComparisons() const {}
43
44 public:
45 // CREATORS
49 explicit Result(const bsl::string& error, int resultCode = -1)
50 : d_value()
51 , d_error(error)
52 , d_resultCode(resultCode)
53 {
54 }
55
60 explicit Result(const bsl::shared_ptr<T>& value,
61 const bsl::string& error = bsl::string(),
62 int resultCode = 0)
63 : d_value(value)
64 , d_error(error)
65 , d_resultCode(resultCode)
66 {
67 }
68
71 const bsl::shared_ptr<T>& value() const { return d_value; }
72
75 const bsl::string& error() const { return d_error; }
76
79 int returnCode() const { return d_resultCode; }
80
83 operator BoolType() const
84 {
85 return d_resultCode == 0 ? &Result::thisTypeDoesNotSupportComparisons
86 : 0;
87 }
88
89 bsl::ostream& operator<<(bsl::ostream& os)
90 {
91 os << "VALUE: " << (d_value ? d_value : "null") << "\n";
92 if (!*this) {
93 os << "ERROR: " << error();
94 }
95 return os;
96 }
97
98 private:
99 bsl::shared_ptr<T> d_value;
100 bsl::string d_error;
101
103 int d_resultCode;
104
105}; // class Result
106
107template <>
108class Result<void> {
109 private:
110 typedef void (Result::*BoolType)() const;
111 void thisTypeDoesNotSupportComparisons() const {}
112
113 public:
114 Result()
115 : d_error()
116 , d_resultCode(0)
117 {
118 }
119
120 explicit Result(const bsl::string& error, int resultCode = -1)
121 : d_error(error)
122 , d_resultCode(resultCode)
123 {
124 }
125
126 const bsl::string& error() const { return d_error; }
127 int returnCode() const { return d_resultCode; }
128
129 operator BoolType() const
130 {
131 return d_resultCode == 0 ? &Result::thisTypeDoesNotSupportComparisons
132 : 0;
133 }
134
135 bsl::ostream& operator<<(bsl::ostream& os) const
136 {
137 if (!*this) {
138 os << "ERROR: " << error();
139 }
140 return os;
141 }
142
143 private:
144 bsl::string d_error;
146 int d_resultCode;
147};
148
149template <typename T, typename U>
150bool operator!=(const Result<T>& lhs, const U&)
151{
152 lhs.thisTypeDoesNotSupportComparisons();
153 return false;
154}
155
156template <typename T, typename U>
157bool operator==(const Result<T>& lhs, const U&)
158{
159 lhs.typeTypeDoesNotSupportComparisons();
160 return false;
161}
162
163typedef bsl::function<void(const bsl::string& errorText, int errorCode)>
164 ErrorCallback;
169
170} // namespace rmqt
171} // namespace BloombergLP
172
173#endif // ! INCLUDED_RMQT_RESULT
A result of an operation.
Definition: rmqt_result.h:37
Result(const bsl::shared_ptr< T > &value, const bsl::string &error=bsl::string(), int resultCode=0)
Definition: rmqt_result.h:60
const bsl::shared_ptr< T > & value() const
Definition: rmqt_result.h:71
const bsl::string & error() const
Definition: rmqt_result.h:75
Result(const bsl::string &error, int resultCode=-1)
Constructs an error result.
Definition: rmqt_result.h:49
int returnCode() const
Definition: rmqt_result.h:79