Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Basic example of loading a component library

This is a simple example of loading and using a P6R component library.

All components are loaded in a similar fashion so this example should give you a clear understanding of how to load any of our component libraries.

#include "p6loader.h"
#include "cconsolestream.h"
#include "p6regex.h"
using namespace P6R;
using namespace P6EXAMPLES;
P6DECLARE_CID(p6Regex);
namespace {
void testRegex(p6IConsole *pConsole, p6IDataStream *pStream)
{
P6ERR err;
P6ARG args[1];
if(P6SUCCEEDED(err = p6CreateInstance(NULL,CID_p6Regex,VALIDATECOMPTR(p6IRegex,cpRegex)))) {
if(P6SUCCEEDED(err = cpRegex->initialize(P6REGEX_NOFLAGS,P6REGEX_PERL))) {
if(P6SUCCEEDED(err = cpRegex->compile("\\s*\\w+,?\\s+(\\d\\d)\\s(\\w+)\\s+(\\d\\d\\d\\d)\\s+(\\d\\d):(\\d\\d):(\\d\\d)\\s+([+\\-]?\\d\\d\\d\\d|\\w+)\\s*", P6MODIFIER_NULL ))) {
P6UINT32 offset = 0;
P6UINT32 strLength = 0;
if(P6SUCCEEDED(err = cpRegex->search( "Wed, 02 Oct 2002 13:00:00 +0800",
P6MODIFIER_NULL,
&offset,
&strLength ))) {
if(0 == offset && 31 == strLength) {
pConsole->writeStdout("Regex [ PASS ]\n",NULL,0,NULL);
}
else pConsole->writeStdout("Error: Offset or length wrong\n",NULL,0,NULL);
}
else pConsole->writeStdout("Error: match failed\n",NULL,0,NULL);
}
else {
P6AI_ERR(&args[0],err);
pConsole->writeStdout("Error: compile failed [ %1$ ]\n",&args[0],1,NULL);
}
}
else {
P6AI_ERR(&args[0],err);
pConsole->writeStdout("Error: Initialize failed [ %1$ ]\n",&args[0],1,NULL);
}
}
else {
P6AI_ERR(&args[0],err);
pConsole->writeStdout("Error: CreateInstance failed [ %1$ ]\n",&args[0],1,NULL);
}
} // end
} // namespace
int main(int argc,char *argv[])
{
P6ERR err;
P6SCLF fFlags = P6SCLF_NOFLAGS;
for(P6INT32 i=1;i<argc;i++) {
if('-' == argv[i][0] || '/' == argv[i][0]) {
switch(argv[i][1]) {
case 'd':
case 'D': {
fFlags |= P6SCLF_ALLLOG;
break;
}
default: {
break;
}
}
}
}
//
// Create and initialize an instance of a p6IDataStream to log output to the console.
// The SKC loader uses this stream to output error and debug information (depending on the
// flags passed into p6InitializeLoader(). p6IDataStream is an interface that the
// CConsoleStream class (in this case implements). A different class implementing this
// inertace could just as easily output the log data to a file or socket.
//
// CConsoleStream is an implementation of the p6IDataStream interface, however is is not
// an SKC component which is important because it needs to have a longer lifetime.
// Its source is included in the examples/common directory.
//
if(P6SUCCEEDED(err = CConsoleStream::createInstance(NULL,VALIDATECOMPTR(p6IDataStream,cpDataStream)))) {
//
// If you would like to see verbose information passed to the provided data stream,
// you could pass P6SCLF_ALLLOG to see all output. This will tell the loader
// to output very verbose debugging information. By default, only errors are passed
// to the data stream. See p6loader.h for information on the flags which can be
// passed to p6IniitalizeLoader(). We use P6SCLF_ALLLOG in this example so you can
// see all the information.
//
if(P6SUCCEEDED(err = p6InitializeLoader(cpDataStream,9,fFlags))) {
//
// The loader provides a console interface that our application code can use to output
// information to the console if needed. This console interface is retrieved using
// p6GetRuntimeIface() and is ready to use without calling an initialize() method.
//
//
// If you would like to see verbose information passed to the provided data stream,
// you could pass P6SCLF_ALLLOG to see all output. This will tell the loader
// to output very verbose debugging information. By default, only errors are passed
// to the data stream. See p6loader.h for information on the flags which can be
// passed to p6IniitalizeLoader().
//
testRegex(cpConsole, cpDataStream);
//
// Make sure to release the console interface
// before calling p6CleanupLoader() !! In this
// instance we don't let the smart pointer
// handle the cleanup because is lifetime
// outlives the lifetime of SKC loader. All
// SKC components must be release prior to
// calling p6CleanupLoader();
//
cpConsole = NULL;
}
}
else printf("ERROR: Failed to initialize the loader [ %x ]\n",err);
}
else printf("ERROR: Failed to create CConsoleStream [ %x ]\n",err);
cpDataStream = NULL;
if(P6SUCCEEDED(err = CConsoleStream::createInstance(NULL,VALIDATECOMPTR(p6IDataStream,cpDataStream)))) {
//
// If you would like to see verbose information passed to the provided data stream,
// you could pass P6SCLF_ALLLOG to see all output. This will tell the loader
// to output very verbose debugging information. By default, only errors are passed
// to the data stream. See p6loader.h for information on the flags which can be
// passed to p6IniitalizeLoader(). We use P6SCLF_ALLLOG in this example so you can
// see all the information.
//
if(P6SUCCEEDED(err = p6InitializeLoader(cpDataStream,9,P6SCLF_ALLLOG))) {
//
// The loader provides a console interface that our application code can use to output
// information to the console if needed. This console interface is retrieved using
// p6GetRuntimeIface() and is ready to use without calling an initialize() method.
//
//
// If you would like to see verbose information passed to the provided data stream,
// you could pass P6SCLF_ALLLOG to see all output. This will tell the loader
// to output very verbose debugging information. By default, only errors are passed
// to the data stream. See p6loader.h for information on the flags which can be
// passed to p6IniitalizeLoader().
//
testRegex(cpConsole, cpDataStream);
//
// Make sure to release the console interface
// before calling p6CleanupLoader() !! In this
// instance we don't let the smart pointer
// handle the cleanup because is lifetime
// outlives the lifetime of SKC loader. All
// SKC components must be release prior to
// calling p6CleanupLoader();
//
cpConsole = NULL;
}
}
else printf("ERROR: Failed to initialize the loader [ %x ]\n",err);
}
else printf("ERROR: Failed to create CConsoleStream [ %x ]\n",err);
return err;
} //end