Table of Contents
The process for the creation of the source files from C++ headers is split into two parts:
The first application mockpp2xml
parses the
C++ header file and writes the constructors and methods you need to an intermediate
XML file.
In the second step xml2mockpp
reads the
XML file and creates a C++ header and the according implementation files.
Upon request a third file is created where user defined constructors are located.
Splitting the process into two parts enables postprocessing the XML file with
simple methods. Otherwise a rather complicated C++ parser based on less known
tools like yacc
and lex
would have to
be changed.
Additionally one could create a different application for the second step. Based on the XML file it could create totally different user defined C++ sources.
If you integrate the two applications into your build process, your mock object files are automatically kept up to date with the production sources. You simply make the mock object files depend on the according header file. If you need specialized constuctors you will place them in the user defined third file. This file is never updated automatically. Instead a template file is written from where you can copy-and-paste what you need and adjust the rest manually.
In the first step mockpp2xml
parses the header file
and outputs the
according XML data stream to standard output. If you want to have an XML file you
must redirect standard outout to the desired file. The only possible parameter is the name
of the C++ header file. All the classes in the header file are written to the
XML stream.
The parser needs markers whch methods and constructors shall be written to the XML stream. This is done by appending a pseudo keyword after one of the access modifiers public, protected or private before the definition.
The parser distinguishes between constructors and methods. Each of them uses a different keyword:
This keyword includes the directly following constructor (and only this one) in the XML stream.
All the methods after this keyword are written to the XML stream. The inclusion stops at the next access modifier unless it contains this keyword again.
Destructors are never included even if they have a starting marker. To make these keywords invisible for the compiler you define these two with empty content.
A sample class could then look like this:
#define mockpp_constructor #define mockpp_methods class MockppIdlTest : public IdlInterface { public mockpp_constructor: MockppIdlTest(int i, const std::string &s) : IdlInterface("name") { } private mockpp_methods: virtual std::string * url4() = 0; };
The following line shows how to invoke the C++ parser and write the XML stream to a file:
mockpp2xml header.h >header-data.xml
If you are familiar with with development around KDE and QT you are probably familiar with this approach.
It might also be interesting to know that the basis for this parser was taken from the KDE Project. For that reason it is able to parse (in the meaning of to ignore) the special keywords related to DCOP and QOBJECT like k_dcop or signals.
The Difficulty of Parsing C++ | |
---|---|
Parsing even valid C++ source code is is a difficult task.
The test files In case you have a file which is not parsed correctly but you think this should be the case since it looks trivial: please post a bug report. |