Back

#pragma once

////////////////////
// Exception codes;
//
#define EX_NOEXCEPTION     0  // No exception;

#define EX_MIN          1  // Minimum exception code;

#define EX_GENERALFAILURE  1  // Generic or Unknown RunTime error;
#define EX_INDEXOUTOFRANGE 2  // Object index is outside the range of array bounds;
#define EX_NOTIMPLEMENTED  3  // This feature is not supported in this version of ProSysLib;
#define EX_LOWMEMORY    4  // Out of memory;
#define EX_NOACCESS        5  // Operation failed because of limited access rights or privileges;
#define EX_INVALIDPARAMETER   6  // Invalid Parameter;
#define EX_WMIERROR        7  // WMI-specific error;

#define EX_MAX          7  // Maximum exception code;

#define COM_ERROR_BASE     0x200 // Custom error code base as recommended by Microsoft;

#define DECLARE_EXCEPTION(A) const int E_##A = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, COM_ERROR_BASE + EX_##A);

///////////////////////////////////////
// Declaring all supported exceptions;
//
DECLARE_EXCEPTION(GENERALFAILURE)
DECLARE_EXCEPTION(INDEXOUTOFRANGE)
DECLARE_EXCEPTION(NOTIMPLEMENTED)
DECLARE_EXCEPTION(LOWMEMORY)
DECLARE_EXCEPTION(NOACCESS)
DECLARE_EXCEPTION(INVALIDPARAMETER)
DECLARE_EXCEPTION(WMIERROR)

HRESULT GetGlobalExitCode(long exCode, const CLSID & clsID, const IID ID);

template<const CLSID * clsID, const IID * ID>
class CPSLException: public ISupportErrorInfo
{
public:

   CPSLException():m_exCode(0){}

protected:

   STDMETHODIMP InterfaceSupportsErrorInfo(REFIID riid)
   {
      static const IID* arr[] = {ID};
      for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
      {
         if(InlineIsEqualGUID(*arr[i], riid))
            return S_OK;
      }
      return S_FALSE;
   }

   void SetException(long exCode)
   {
      m_exCode = exCode;
   }

   HRESULT GetExitCode()
   {
      return GetGlobalExitCode(m_exCode, *clsID, *ID);
   }

   HRESULT MakeException(long exCode)
   {
      m_exCode = exCode;
      return GetGlobalExitCode(exCode, *clsID, *ID);
   }

private:

   long m_exCode; // Exception Code;
};

#define PSL_BEGIN SetException(EX_NOEXCEPTION);try{
#define PSL_END   }catch(...){SetException(EX_GENERALFAILURE);}return GetExitCode();

Top