Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cerrorhandler.cpp
#include "cerrorhandler.h"
using namespace P6R;
using namespace P6EXAMPLES;
P6DECLARE_IID( IErrorHandlerInit );
namespace P6EXAMPLES {
CErrorHandler::CErrorHandler() : m_bInitialized( P6FALSE ),
m_cpConsole(),
m_errorState( 0 )
{ }
CErrorHandler::~CErrorHandler()
{ }
//
// P6COM Helper macro to provide the implementation
// of standard COM Methods:
//
// createInstance() - Used to easily create instance of this component.
// Using this method to create components is preferred
// because it returns an interface which provides
// a well defined API for this component and prevents
// access to the classes internals.
// queryInterface() - Used to query the component for interfaces
// addref() - Increased the interfaces reference count
// release() - Decreases the interfaces reference count
// and destroys the component when the count
// reaches zero.
//
// These helper macros are provided for convenience and currently support
// up to 17 interfaces (this example exposes 2 interfaces). The implementation
// provided by these macros are threadsafe. You can find the defintions
// in p6comhlpr.h. If you need something more that what they provide,
// you are always free implement your own methods.
//
// See CConsoleStream in this example (in ex-load-jsn.cpp) for a non-threadsafe
// example of implementing these methods.
//
// Please note that these macros may only be used in code that is called
// after p6InitializeLoader() has been called.
//
P6_IMPLEMENT_ICOM2(CErrorHandler,IErrorHandlerInit,p6IJSONErrorHandler);
//
P6COMMETHODIMPL CErrorHandler::warning( P6ERR errorCode, p6IJSONLocator* pObject )
{
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject) return eInvalidArg;
P6AI_ERR(&args[0],errorCode);
m_cpConsole->writeStdout("CErrorHandler - warning: error %1$\n",&args[0],1,NULL);
return eOk;
}
P6COMMETHODIMPL CErrorHandler::warningEx( P6ERR errorCode, p6IJSONLocator* pObject, const P6CHAR* pDisplay )
{
P6ARG args[2];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject || NULL == pDisplay) return eInvalidArg;
P6AI_ERR(&args[0],errorCode);
P6AI_CHARPTR(&args[1],pDisplay);
m_cpConsole->writeStdout("CErrorHandler - warningEx: error %1$ [%2$]\n",&args[0],2,NULL);
return eOk;
}
//
P6COMMETHODIMPL CErrorHandler::error( P6ERR errorCode, p6IJSONLocator* pObject )
{
P6ARG args[1];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject) return eInvalidArg;
P6AI_ERR(&args[0],errorCode);
m_cpConsole->writeStdout("CErrorHandler - error: error %1$\n",&args[0],1,NULL);
return eOk;
}
P6COMMETHODIMPL CErrorHandler::errorEx( P6ERR errorCode, p6IJSONLocator* pObject, const P6CHAR* pDisplay )
{
P6ARG args[2];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject || NULL == pDisplay) return eInvalidArg;
P6AI_ERR(&args[0],errorCode);
P6AI_CHARPTR(&args[1],pDisplay);
m_cpConsole->writeStdout("CErrorHandler - errorEx: error %1$ [%2$]\n",&args[0],2,NULL);
return eOk;
}
// Notice how the p6IJSONLocator can be used to identify to the user what JSON is in error.
//
P6COMMETHODIMPL CErrorHandler::fatalError( P6ERR errorCode, p6IJSONLocator* pObject )
{
P6UINT32 lineNumber = 0;
P6UINT32 columnNumber = 0;
P6ARG args[3];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject) return eInvalidArg;
pObject->getLineNumber( &lineNumber );
pObject->getColumnNumber( &columnNumber );
P6AI_ERR(&args[0],errorCode);
P6AI_UINT32(&args[1],lineNumber);
P6AI_UINT32(&args[2],columnNumber);
m_cpConsole->writeStdout("CErrorHandler - fatalError: error [ %1$ ] line:%2$ column:%3$\n",&args[0],3,NULL);
switch( m_errorState ) {
case 0:
// -> in one of our examples we have purposely placed a parsing error to show what values are passed to this function
if (eArrayCloseMismatch != errorCode) {
P6AI_ERR(&args[0],eArrayCloseMismatch);
P6AI_ERR(&args[2],errorCode);
m_cpConsole->writeStdout("Expected %1$ but got %2$\n",&args[0],2,NULL);
}
if (6 != lineNumber || 50 != columnNumber) {
P6AI_UINT32(&args[0],lineNumber);
P6AI_UINT32(&args[1],columnNumber);
m_cpConsole->writeStdout( "Expected 6,50 but got %1$,%2$\n",&args[0],2,NULL);
}
break;
default:
P6AI_UINT32(&args[0],m_errorState);
m_cpConsole->writeStdout( "out of range got %1$",&args[0],1,NULL);
break;
}
m_errorState++;
return eOk;
}
P6COMMETHODIMPL CErrorHandler::fatalErrorEx( P6ERR errorCode, p6IJSONLocator* pObject, const P6CHAR* pDisplay )
{
P6ARG args [2];
if (!m_bInitialized) return eNotInitialized;
if (NULL == pObject || NULL == pDisplay) return eInvalidArg;
P6AI_ERR(&args[0],errorCode);
P6AI_CHARPTR(&args[1],pDisplay);
m_cpConsole->writeStdout("CErrorHandler - fatalErrorEx: error %1$ [%2$]\n",&args[0],2,NULL);
return eOk;
}
//
P6COMMETHODIMPL CErrorHandler::initialize( P6R::p6IConsole *pConsole )
{
if (m_bInitialized) return eAlreadyInitialized;
if (!pConsole) return eInvalidArg;
m_cpConsole = pConsole;
m_bInitialized = P6TRUE;
return eOk;
}
} // namespace