|
|
Home | Loader API | Interfaces | File List | Index | |
Now that you have learned how to build the examples, lets go through the first example (loader-1) which shows you how to load and use XJR components. The source of this example can be found in the examples/loader/loader-1 directory under you XJR installation directory. The entire source can be viewed without commentary here: ex-loader.cpp
In this example, we will initialize the p6Loader, create an instance of a p6IRegex interface, use the interface, and then free the ineterface and shutdown the loader.
Make sure to include p6xjr.h which will pull in all the includes needed for XJR. All of XJR's C++ interfaces and definitions are contained in the P6R namespace, so we add a using statement so we don't need to qualify the namespace everywhere.XJR uses COM interfaces, so the next step is to define the interface and component IDs we will need.
The p6Loader can take a sta stream which it can use to log debug and error messages. This is a simple implementation of a data stream that outputs everything to the console. This will be passed to loader when we initialize it.
The testRegex() function loads the regex interface using p6CreateInstance(), initializes the interface for use, and then compiles and executes a regular expression. Notice that the interface pointer is stored in the p6ComPtr<> smart pointer which will take care of calling release() on the interface when the function is exited. We recommend using p6ComPtr<> for all interface pointers when possible. It makes coding easier and guards against resource leaks.
Here is the example's main(). Again we use a smart pointer to store the data stream interface pointer. First we create the data stream and then initialize the component loader by calling p6LoaderInitialize(). Once the loader is successfully initialized, you can create any number of interface instances by calling p6CreateInstance(). The Loader API reference is available from top nav bar.
Once the loader successfully initialized, we call our regex function, then cleanup by calling p6CleanupLoader(). This will unload any of XJR's shared libraries that have been loaded and free all the resources it was using. Take care to ensure that you have called release() on any component interfaced you were using (the p6ComPtr<> does this for you when it goes out of scope).