A C++ Developer's Guide to Interfacing with the XLOPER Data Structure

Introduction

There are a variety of generic classes available to C++ developers that provide better ways for working with arrays of data. These classes automatically handle memory management as items are added or removed from arrays. Each instance also stores bound information and can automatically check bounds.

One of the most standard examples of this is the std::vector class that is included as part of the Standard Template Library (STL). The STL is an ANSI C++ compliant set of general-purpose algorithms and data structures that are available for use on both Windows and Unix platforms. Two dimensional arrays can be implemented as vectors of vectors: std::vector>. There are also various Open Source or third-party libraries available that provide matrix or multi-dimensional functionality for C++ developers.

First steps

The purpose of this article is to provide one example of how C++ developers who work with array data using generic C++ classes and methods can write a container class class xl that provides methods for converting arrays from the std::vector type to the XLOPER data structure that is used for passing arrays to the FINCAD math library. This is a good starting point for integrating FINCAD Developer into your C++ applications.

Sample Application

The sample application below reads a holiday list and a bond from a file, and given a yield, calculates the clean bond price. It also gives examples of using the STL and object-oriented methods to encapsulate behaviours and data (we will discuss important lines in order):

Creating the holiday list (STL)

Using the istream_iterator convenience object, we effortlessly read a series of doubles from an input stream, file_hl, into an STL container. We then use the STL's sort algorithm to ensure our dates are in ascending order i.e., that our input table is valid.

Creating the XLOPER wrapper

Using our newly sorted holiday list, we now create our XLOPER wrapper: xl hl(hlc); The constructor (below) for the xl class takes in a STL container and wraps the data in a way that is easy for the FINCAD API to consume (but the user is never concerned with this detail):

Here, the xlimpl class is an implementation class. To keep the interface of the xl class simple, we used the Pimpl idiom (see references). The xlimpl class does all the hard work of indexing and memory management.

Valuing the bond

Seems pretty simple doesn't it? Well it is. In fact, because we have encapsulated the data in our Bond object, all we really need to tell the priceGivenYield function is the yield and the holiday list. The rest of the parameters have default values. Here is the actual code:


The wrapper

But where are all the .val.nums and where are all the .val.arrays? The answer is: they have been encapsulated into the xl class so that you can use this class as a zero-based vector, just like every other array in C/C++. While the details can be found in the provided source (see attachments), here is the interface for this class:


Notice that you are spared knowing about the private details because of the Pimpl implementation pattern. This class defines many overloaded operators that use implicit and explicit conversions (see Appendix) to the benefit of the programmer i.e., using implicit conversions where they help and forcing explicit conversions where it is ambiguous.

Conclusion

We saw that it is fairly easy to wrap a one-dimensional XLOPER data structure to allow for conversion to and from STL containers (in this case, a vector type). The main goal for the FINCAD Developer API's interaction with C++ is likely to be a container which defines iterators with STL behaviour. This would allow for the development and consumption of generic libraries such as the STL which contains facilities for sorting, querying, statistics gathering and much more. There are many ways to give STL behaviours to a general container, but there are always tradeoffs. For more information please see the references, specifically the reference by Josuttis and the C++ reference by Stroustrup.

References

  • The Pimpl idiom
  • The C++ Standard Library: A Tutorial and Reference - Nicolai M. Josuttis
  • The C++ Programming Language - Bjarne Stroustrup
  • C++ FAQ Lite

Appendix

Explicit
A constructor taking a single argument is by default an implicit conversion operator. This can have undesirable effects such as an assignment when an index was meant to be used (xl_instance = 5, instead of xl_instance[0] = 5). See the DevX article for more information

Attachments
xlcpp.zip - All the source files required to run the example (providing you have FINCAD Developer installed) - extract to C:\

Disclaimer

Your use of the information in this article is at your own risk. The information in this article is provided on an "as is" basis and without any representation, obligation, or warranty from FINCAD of any kind, whether express or implied. We hope that such information will assist you, but it should not be used or relied upon as a substitute for your own independent research.

» For more information or a customized demonstration of the software, contact a FINCAD Representative.

Request a Free Trial