1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
/*******************************************************************************
* shroudBNC - an object-oriented framework for IRC *
* Copyright (C) 2005-2014 Gunnar Beutner *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
*******************************************************************************/
#ifndef RESULT_H
#define RESULT_H
/**
* CResult<Type>
*
* A wrapper class for results and error codes.
*/
template<typename Type>
class CResult {
private:
Type m_Result; /**< the actual result */
unsigned int m_Code; /**< an error code, or 0 if no error occured */
const char *m_Description; /**< a human-readable description of the error */
public:
/**
* CResult
*
* Constructs a result object which represents a default result.
*/
CResult(void) {
memset(&m_Result, 0, sizeof(m_Result));
m_Code = 0;
m_Description = NULL;
}
/**
* CResult
*
* The copy-constructor for result objects.
*
* @param Source the source object.
*/
CResult(const CResult<Type> &Source) {
m_Code = Source.m_Code;
m_Result = Source.m_Result;
m_Description = Source.m_Description;
}
/**
* CResult
*
* Wraps a result value in a CResult object.
*
* @param Result the result value
*/
explicit CResult(const Type Result) {
m_Code = 0;
m_Description = NULL;
m_Result = Result;
}
/**
* CResult
*
* Constructs a new result object which represents an error.
*
* @param Code an error code
* @param Description a description for the error
*/
CResult(unsigned int Code, const char *Description) {
if (Code != 0) {
m_Code = Code;
} else {
m_Code = 1;
}
m_Description = Description;
memset(&m_Result, 0, sizeof(Type));
}
/**
* GetCode
*
* Returns the error code of a result object.
*/
unsigned int GetCode(void) const {
return m_Code;
}
/**
* GetDescription
*
* Returns the error description of the result object.
*/
const char *GetDescription(void) const {
return m_Description;
}
/**
* GetResult
*
* Returns a reference to the underlying result.
*/
Type &GetResult(void) {
return m_Result;
}
/**
* operator Type &
*
* Returns a reference to the underlying result.
*/
operator Type &(void) {
return m_Result;
}
};
/**
* IsError<Type>
*
* Checks whether a result object represents an error.
*
* @param Result the result object
*/
template<typename Type>
bool IsError(const CResult<Type> &Result) {
unsigned int Code;
Code = Result.GetCode();
return (Code != 0);
}
// some macros for using CResult objects
#define RESULT CResult
#define RETURN(Type, Result) do { CResult<Type> cResult(Result); return cResult; } while (0)
#define THROW(Type, Code, Description) do { CResult<Type> cResult(Code, Description); return cResult; } while (0)
#define THROWRESULT(Type, Result) do { assert(IsError(Result)); CResult<Type> cResult(Result.GetCode(), Result.GetDescription()); if (IsError(Result)) { return cResult; } } while (0)
#define THROWIFERROR(Type, Result) do { CResult<Type> cResult(Result.GetCode(), Result.GetDescription()); if (IsError(Result)) { return cResult; } } while (0)
#define ASSERTRESULT(Result) assert(!IsError(Result))
#define GETCODE(Result) Result.GetCode()
#define GETDESCRIPTION(Result) Result.GetDescription()
#define NEWERROR(Type, Code, Description) CResult<Type>(Code, Description)
/**
* generic_error_t
*
* Commonly used error codes.
*/
typedef enum generic_error_e {
Generic_OutOfMemory = 5000,
Generic_InvalidArgument,
Generic_QuotaExceeded,
Generic_Unknown
} generic_error_t;
#ifdef SWIGINTERFACE
%template(CResultCharP) CResult<char *>;
%template(CResultConstCharP) CResult<const char *>;
%template(CResultBool) CResult<bool>;
#endif /* SWIGINTERFACE */
#endif /* RESULT_H */
|