Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:20:11

0001 /**
0002  *  @file   PandoraSDK/include/Pandora/StatusCodes.h
0003  * 
0004  *  @brief  Header file defining status codes and relevant preprocessor macros
0005  * 
0006  *  $Log: $
0007  */
0008 #ifndef PANDORA_STATUS_CODES_H
0009 #define PANDORA_STATUS_CODES_H 1
0010 
0011 #include <exception>
0012 #include <string>
0013 
0014 #if defined(__GNUC__) && defined(BACKTRACE)
0015     #include <cstdlib>
0016     #include <execinfo.h>
0017 #endif
0018 
0019 #define PANDORA_RETURN(StatusCode)                                                                      \
0020 {                                                                                                       \
0021     std::cout << "    in function: " << __FUNCTION__ << std::endl;                                      \
0022     std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;                \
0023     return StatusCode;                                                                                  \
0024 }
0025 
0026 #define PANDORA_RETURN_IF(StatusCode, Condition)                                                        \
0027 {                                                                                                       \
0028     if (Condition)                                                                                      \
0029     {                                                                                                   \
0030         std::cout << #Condition << " return " << StatusCodeToString(StatusCode) << std::endl;           \
0031         std::cout << "    in function: " << __FUNCTION__ << std::endl;                                  \
0032         std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;            \
0033         return StatusCode;                                                                              \
0034     }                                                                                                   \
0035 }
0036 
0037 #define PANDORA_RETURN_RESULT_IF(StatusCode1, Operator, Command)                                        \
0038 {                                                                                                       \
0039     const pandora::StatusCode statusCode(Command);                                                      \
0040     if (statusCode Operator StatusCode1)                                                                \
0041     {                                                                                                   \
0042         std::cout << #Command << " return " << StatusCodeToString(statusCode) << std::endl;             \
0043         std::cout << "    in function: " << __FUNCTION__ << std::endl;                                  \
0044         std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;            \
0045         return statusCode;                                                                              \
0046     }                                                                                                   \
0047 }
0048 
0049 #define PANDORA_RETURN_RESULT_IF_AND_IF(StatusCode1, StatusCode2, Operator, Command)                    \
0050 {                                                                                                       \
0051     const pandora::StatusCode statusCode(Command);                                                      \
0052     if ((statusCode Operator StatusCode1) && (statusCode Operator StatusCode2))                         \
0053     {                                                                                                   \
0054         std::cout << #Command << " return " << StatusCodeToString(statusCode) << std::endl;             \
0055         std::cout << "    in function: " << __FUNCTION__ << std::endl;                                  \
0056         std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;            \
0057         return statusCode;                                                                              \
0058     }                                                                                                   \
0059 }
0060 
0061 #define PANDORA_THROW(StatusCode)                                                                       \
0062 {                                                                                                       \
0063     std::cout << "    in function: " << __FUNCTION__ << std::endl;                                      \
0064     std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;                \
0065     throw pandora::StatusCodeException(StatusCode);                                                     \
0066 }
0067 
0068 #define PANDORA_THROW_IF(StatusCode, Condition)                                                         \
0069 {                                                                                                       \
0070     if (Condition)                                                                                      \
0071     {                                                                                                   \
0072         std::cout << #Condition << " throw " << StatusCodeToString(StatusCode) << std::endl;            \
0073         std::cout << "    in function: " << __FUNCTION__ << std::endl;                                  \
0074         std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;            \
0075         throw pandora::StatusCodeException(StatusCode);                                                 \
0076     }                                                                                                   \
0077 }
0078 
0079 #define PANDORA_THROW_RESULT_IF(StatusCode1, Operator, Command)                                         \
0080 {                                                                                                       \
0081     const pandora::StatusCode statusCode(Command);                                                      \
0082     if (statusCode Operator StatusCode1)                                                                \
0083     {                                                                                                   \
0084         std::cout << #Command << " throw " << StatusCodeToString(statusCode) << std::endl;              \
0085         std::cout << "    in function: " << __FUNCTION__ << std::endl;                                  \
0086         std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;            \
0087         throw pandora::StatusCodeException(statusCode);                                                 \
0088     }                                                                                                   \
0089 }
0090 
0091 #define PANDORA_THROW_RESULT_IF_AND_IF(StatusCode1, StatusCode2, Operator, Command)                     \
0092 {                                                                                                       \
0093     const pandora::StatusCode statusCode(Command);                                                      \
0094     if ((statusCode Operator StatusCode1) && (statusCode Operator StatusCode2))                         \
0095     {                                                                                                   \
0096         std::cout << #Command << " throw " << StatusCodeToString(statusCode) << std::endl;              \
0097         std::cout << "    in function: " << __FUNCTION__ << std::endl;                                  \
0098         std::cout << "    in file:     " << __FILE__ << " line#: " << __LINE__ << std::endl;            \
0099         throw pandora::StatusCodeException(statusCode);                                                 \
0100     }                                                                                                   \
0101 }
0102 
0103 //------------------------------------------------------------------------------------------------------------------------------------------
0104 
0105 namespace pandora
0106 {
0107 
0108 #define STATUS_CODE_TABLE(d)                                                                            \
0109     d(STATUS_CODE_SUCCESS,                  "STATUS_CODE_SUCCESS"                   )                   \
0110     d(STATUS_CODE_FAILURE,                  "STATUS_CODE_FAILURE"                   )                   \
0111     d(STATUS_CODE_NOT_FOUND,                "STATUS_CODE_NOT_FOUND"                 )                   \
0112     d(STATUS_CODE_NOT_INITIALIZED,          "STATUS_CODE_NOT_INITIALIZED"           )                   \
0113     d(STATUS_CODE_ALREADY_INITIALIZED,      "STATUS_CODE_ALREADY_INITIALIZED"       )                   \
0114     d(STATUS_CODE_ALREADY_PRESENT,          "STATUS_CODE_ALREADY_PRESENT"           )                   \
0115     d(STATUS_CODE_OUT_OF_RANGE,             "STATUS_CODE_OUT_OF_RANGE"              )                   \
0116     d(STATUS_CODE_NOT_ALLOWED,              "STATUS_CODE_NOT_ALLOWED"               )                   \
0117     d(STATUS_CODE_INVALID_PARAMETER,        "STATUS_CODE_INVALID_PARAMETER"         )                   \
0118     d(STATUS_CODE_UNCHANGED,                "STATUS_CODE_UNCHANGED"                 )
0119 
0120 /**
0121  *  @brief  The status code enum entry macro
0122  */
0123 #define GET_STATUS_CODE_ENUM_ENTRY(a, b)                                                                \
0124     a,
0125 
0126 /**
0127  *  @brief  The status code name switch statement macro
0128  */
0129 #define GET_STATUS_CODE_NAME_SWITCH(a, b)                                                               \
0130     case a : return b;
0131 
0132 /**
0133  *  @brief  The StatusCode enum
0134  */
0135 enum StatusCode
0136 {
0137     STATUS_CODE_TABLE(GET_STATUS_CODE_ENUM_ENTRY)
0138     NUMBER_OF_STATUS_CODES
0139 };
0140 
0141 /**
0142  *  @brief  Get status code as a string
0143  * 
0144  *  @return The status code string
0145  */
0146 std::string StatusCodeToString(const StatusCode statusCode);
0147 
0148 //------------------------------------------------------------------------------------------------------------------------------------------
0149 //------------------------------------------------------------------------------------------------------------------------------------------
0150 
0151 /**
0152  *  @brief  StatusCodeException class
0153  */
0154 class StatusCodeException
0155 {
0156 public:
0157     /**
0158      *  @brief  Constructor
0159      * 
0160      *  @param  statusCode the status code
0161      */
0162     StatusCodeException(const StatusCode statusCode);
0163 
0164     /**
0165      *  @brief  Destructor
0166      */
0167     ~StatusCodeException() noexcept = default;
0168 
0169     /**
0170      *  @brief  Get status code
0171      * 
0172      *  @return the status code
0173      */
0174     StatusCode GetStatusCode() const;
0175 
0176     /**
0177      *  @brief  Get status code as a string
0178      * 
0179      *  @return The status code string
0180      */
0181     std::string ToString() const;
0182 
0183     /**
0184      *  @brief  Get back trace at point of exception construction (gcc only)
0185      * 
0186      *  @return The back trace
0187      */
0188     const std::string &GetBackTrace() const;
0189 
0190 private:
0191     const StatusCode    m_statusCode;   ///< The status code
0192     std::string         m_backTrace;    ///< The back trace
0193 };
0194 
0195 //------------------------------------------------------------------------------------------------------------------------------------------
0196 
0197 inline StatusCodeException::StatusCodeException(const StatusCode statusCode) :
0198     m_statusCode(statusCode)
0199 {
0200 #if defined(__GNUC__) && defined(BACKTRACE)
0201     const size_t maxDepth = 100;
0202     void *stackAddresses[maxDepth];
0203 
0204     size_t stackDepth = backtrace(stackAddresses, maxDepth);
0205     char **stackStrings = backtrace_symbols(stackAddresses, stackDepth);
0206 
0207     m_backTrace = "\nBackTrace\n    ";
0208 
0209     for (size_t i = 0; i < stackDepth; ++i)
0210     {
0211         m_backTrace += stackStrings[i];
0212         m_backTrace += "\n    ";
0213     }
0214 
0215     free(stackStrings); // malloc()ed by backtrace_symbols
0216 #endif
0217 }
0218 
0219 //------------------------------------------------------------------------------------------------------------------------------------------
0220 
0221 inline StatusCode StatusCodeException::GetStatusCode() const
0222 {
0223     return m_statusCode;
0224 }
0225 
0226 //------------------------------------------------------------------------------------------------------------------------------------------
0227 
0228 inline std::string StatusCodeException::ToString() const
0229 {
0230     return StatusCodeToString(m_statusCode);
0231 }
0232 
0233 //------------------------------------------------------------------------------------------------------------------------------------------
0234 
0235 inline const std::string &StatusCodeException::GetBackTrace() const
0236 {
0237     return m_backTrace;
0238 }
0239 
0240 //------------------------------------------------------------------------------------------------------------------------------------------
0241 //------------------------------------------------------------------------------------------------------------------------------------------
0242 
0243 inline std::string StatusCodeToString(const StatusCode statusCode)
0244 {
0245     switch (statusCode)
0246     {
0247         STATUS_CODE_TABLE(GET_STATUS_CODE_NAME_SWITCH)
0248         default : return "UNKNOWN";
0249     }
0250 }
0251 
0252 } // namespace pandora
0253 
0254 #endif // #ifndef PANDORA_STATUS_CODES_H