Phoenix
Object-oriented orthogonally persistent operating system
RetCode.h
Go to the documentation of this file.
00001 /*
00002  * /phoenix/include/RetCode.h
00003  *
00004  * This file is a part of Phoenix operating system.
00005  * Copyright (c) 2011-2012, Artyom Lebedev <artyom.lebedev@gmail.com>
00006  * All rights reserved.
00007  * See COPYING file for copyright details.
00008  */
00009 
00015 #ifndef RETCODE_H_
00016 #define RETCODE_H_
00017 
00019 class RetCode {
00020 public:
00022     enum Code {
00023         SUCCESS, 
00024         FAIL, 
00025         INV_PARAM, 
00026         NOT_FOUND, 
00027         NO_MEMORY, 
00028         NO_RESOURCES, 
00029     };
00030 
00036     inline RetCode(Code retCode = SUCCESS) {
00037         code = retCode;
00038     }
00039 
00041     inline operator Code() { return code; }
00042 
00044     inline bool IsOk() { return code == SUCCESS; }
00045 
00047     inline bool IsFailed() { return code != SUCCESS; }
00048 
00049     const char *GetName() {
00050         switch (code) {
00051         case SUCCESS:
00052             return "SUCCESS";
00053         case FAIL:
00054             return "FAIL";
00055         case INV_PARAM:
00056             return "INV_PARAM";
00057         case NOT_FOUND:
00058             return "NOT_FOUND";
00059         case NO_MEMORY:
00060             return "NO_MEMORY";
00061         case NO_RESOURCES:
00062             return "NO_RESOURCES";
00063         }
00064         return "UNKNOWN";
00065     }
00066 
00067     inline operator const char *() {
00068         return GetName();
00069     }
00070 
00071     Code code;
00072 };
00073 
00074 #ifdef DEBUG
00075 
00082 #define RC(__code)      ({ \
00083     if (RetCode::__code != RetCode::SUCCESS) { \
00084         TRACE("Function '%s' at %s:%d failed: %s", \
00085               __func__, __FILE__, __LINE__, __STR(__code)); \
00086     } \
00087     RetCode::__code; \
00088 })
00089 
00091 #define OK(__rc)        ({ \
00092     RetCode __Xrc = __rc; \
00093     if ((__Xrc).IsFailed()) { \
00094         TRACE("Failed return code received in '%s' at %s:%d: %s", \
00095               __func__, __FILE__, __LINE__, (__Xrc).GetName()); \
00096     } \
00097     (__Xrc).IsOk(); \
00098 })
00099 
00101 #define NOK(__rc)       ({ \
00102     RetCode __Xrc = __rc; \
00103     if ((__Xrc).IsFailed()) { \
00104         TRACE("Failed return code received in '%s' at %s:%d: %s", \
00105               __func__, __FILE__, __LINE__, (__Xrc).GetName()); \
00106     } \
00107     (__Xrc).IsFailed(); \
00108 })
00109 
00110 #else /* DEBUG */
00111 
00112 #define RC(__code)      RetCode(RetCode::__code)
00113 
00114 #define OK(__rc)        (LIKELY((__rc).IsOk()))
00115 
00116 #define NOK(__rc)       (UNLIKELY((__rc).IsFailed()))
00117 
00118 #endif /* DEBUG */
00119 
00120 #endif /* RETCODE_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines