| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2019 |
| 4 |
* Author: Georgios Bitzes <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <status/DavixStatus.hpp> |
| 23 |
|
| 24 |
namespace Davix { |
| 25 |
|
| 26 |
//------------------------------------------------------------------------------ |
| 27 |
// Empty constructor - OK status, with empty error message, and empty scope |
| 28 |
//------------------------------------------------------------------------------ |
| 29 |
Status::Status() { |
| 30 |
d_ptr = NULL; |
| 31 |
} |
| 32 |
|
| 33 |
//------------------------------------------------------------------------------ |
| 34 |
// Constructor - specify scope, errno, and error message |
| 35 |
//------------------------------------------------------------------------------ |
| 36 |
Status::Status(const std::string &scope, StatusCode::Code errcode, const std::string& errmsg) { |
| 37 |
d_ptr = new DavixErrorInternal(scope, errcode, errmsg); |
| 38 |
} |
| 39 |
|
| 40 |
//------------------------------------------------------------------------------ |
| 41 |
// Constructor from DavixError** |
| 42 |
//------------------------------------------------------------------------------ |
| 43 |
Status::Status(DavixError** err) { |
| 44 |
if(err && *err) { |
| 45 |
d_ptr = new DavixErrorInternal( (*err)->getErrScope(), (*err)->getStatus(), (*err)->getErrMsg()); |
| 46 |
} |
| 47 |
else { |
| 48 |
d_ptr = NULL; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
//------------------------------------------------------------------------------ |
| 53 |
// Destructor |
| 54 |
//------------------------------------------------------------------------------ |
| 55 |
Status::~Status() { |
| 56 |
clear(); |
| 57 |
} |
| 58 |
|
| 59 |
//------------------------------------------------------------------------------ |
| 60 |
// Copy constructor |
| 61 |
//------------------------------------------------------------------------------ |
| 62 |
Status::Status(const Status &other) { |
| 63 |
d_ptr = NULL; |
| 64 |
*this = other; |
| 65 |
} |
| 66 |
|
| 67 |
//------------------------------------------------------------------------------ |
| 68 |
// Copy assignment operator |
| 69 |
//------------------------------------------------------------------------------ |
| 70 |
Status& Status::operator=(const Status &other) { |
| 71 |
clear(); |
| 72 |
|
| 73 |
if(other.d_ptr) { |
| 74 |
d_ptr = new DavixErrorInternal(*other.d_ptr); |
| 75 |
} |
| 76 |
else { |
| 77 |
d_ptr = NULL; |
| 78 |
} |
| 79 |
|
| 80 |
return *this; |
| 81 |
} |
| 82 |
|
| 83 |
//------------------------------------------------------------------------------ |
| 84 |
// Check if status indicates any error |
| 85 |
//------------------------------------------------------------------------------ |
| 86 |
bool Status::ok() const { |
| 87 |
return getCode() == StatusCode::OK; |
| 88 |
} |
| 89 |
|
| 90 |
//------------------------------------------------------------------------------ |
| 91 |
// Check if status indicates any error - as integer, 0 for OK, -1 for not-ok |
| 92 |
//------------------------------------------------------------------------------ |
| 93 |
bool Status::okAsInt() const { |
| 94 |
if(ok()) { |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
//------------------------------------------------------------------------------ |
| 102 |
// Get status code |
| 103 |
//------------------------------------------------------------------------------ |
| 104 |
StatusCode::Code Status::getCode() const { |
| 105 |
if(!d_ptr) { |
| 106 |
return StatusCode::OK; |
| 107 |
} |
| 108 |
else { |
| 109 |
return d_ptr->_code; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
//------------------------------------------------------------------------------ |
| 114 |
// Get error message |
| 115 |
//------------------------------------------------------------------------------ |
| 116 |
std::string Status::getErrorMessage() const { |
| 117 |
if(!d_ptr) { |
| 118 |
return std::string(); |
| 119 |
} |
| 120 |
else { |
| 121 |
return d_ptr->_errMsg; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
//------------------------------------------------------------------------------ |
| 126 |
// Get scope |
| 127 |
//------------------------------------------------------------------------------ |
| 128 |
std::string Status::getScope() const { |
| 129 |
if(!d_ptr) { |
| 130 |
return std::string(); |
| 131 |
} |
| 132 |
else { |
| 133 |
return d_ptr->_scope; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
//------------------------------------------------------------------------------ |
| 138 |
// Clear contents |
| 139 |
//------------------------------------------------------------------------------ |
| 140 |
void Status::clear() { |
| 141 |
if(d_ptr) { |
| 142 |
delete d_ptr; |
| 143 |
d_ptr = NULL; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
//------------------------------------------------------------------------------ |
| 148 |
// To DavixError (ugh) |
| 149 |
//------------------------------------------------------------------------------ |
| 150 |
int Status::toDavixError(DavixError **err) const { |
| 151 |
if(d_ptr) { |
| 152 |
DavixError::setupError(err, d_ptr->_scope, d_ptr->_code, d_ptr->_errMsg); |
| 153 |
return 1; |
| 154 |
} |
| 155 |
else { |
| 156 |
DavixError::clearError(err); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
} |