Example: Create a Custom C++ Library

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Functions > Referencing Java, C# and C++ Libraries Manually >

Example: Create a Custom C++ Library

This topic describes how to create a sample C++ library and configure a .mff file for it so that the library appears in the Libraries window of MapForce.

 

1.Create a header (.h) file for your class library. The following code listing illustrates a sample header file called Greetings.h.

 

  #ifndef HELLOWORLDLIBRARY_GREETINGS_H_INCLUDED
  #define HELLOWORLDLIBRARY_GREETINGS_H_INCLUDED
 
  #if _MSC_VER > 1000
    #pragma once
  #endif // _MSC_VER > 1000
 
  using namespace altova;
 
  namespace helloworld {
 
  class ALTOVA_DECLSPECIFIER Greetings
  {
  public:
    static string_type   HelloFunctionResponse(bool greetingType);
  };
 
  } // namespace HelloWorldLibrary
 
  #endif // HELLOWORLDLIBRARY_GREETINGS_H_INCLUDED

 

Notice that the function has been declared as static, and that the namespace altova is imported. Remember to write ALTOVA_DECLSPECIFIER in front of the class name, this ensures that your classes will compile correctly—whether you use dynamic or static linkage in subsequently generated code.

 

2.Create a .cpp file with the same name as the header file. The .cpp file must be in the same directory as the .h file. The following code listing illustrates a sample .cpp file called Greetings.cpp that includes the Greetings.h file created previously:

 

  #include "StdAfx.h"
  #include "../Altova/Altova.h"
  #include "../Altova/AltovaException.h"
  #include "../Altova/SchemaTypes.h"
 
  #include "Greetings.h"
 
  namespace helloworld {
 
    string_type   Greetings::HelloFunctionResponse(bool greetingType)
    {
        if( greetingType )        
          return _T("Hello World!");
        return _T("Hello User!");
    }
 
  }

 

Notice the lines that import the StdAfx.h and several Altova libraries. These lines must be left unchanged. The paths to the Altova libraries is correct; in the generated code, these paths will point to the respective files.

 

In contrast to Java or C#, you do not need to compile your source C++ files. They will be copied to the generated code, and are compiled with the rest of the generated mapping code.

 

3.Using an XML editor, create a new .mff file and validate it against the ..\Program Files\MapForceLibraries\mff.xsd folder. Make sure that the text highlighted below points to the directory of the header and cpp files created previously. Remember that the namespace and function names and data types defined here must correspond to those in the C++ code, as described in Configuring the .mff File. For information about data type support, see Data Type Mapping.

 

<?xml version="1.0" encoding="UTF-8"?>
<mapping version="9" library="helloworld" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mff.xsd">
  <implementations>
    <implementation language="cpp">
        <setting name="namespace" value="helloworld"/>
        <setting name="class" value="Greetings"/>
        <setting name="path" value="C:\Projects\HelloWorld"/>
        <setting name="include" value="Greetings.h"/>
        <setting name="source" value="Greetings.cpp"/>
    </implementation>
  </implementations>
  <group name="Greetings">
    <component name="HelloFunctionResponse">
        <sources>
          <datapoint name="greeting_type" type="xs:boolean"/>
        </sources>
        <targets>
          <datapoint name="result" type="xs:string"/>
        </targets>
        <implementations>
          <implementation language="cpp">
              <function name="HelloFunctionResponse"/>
          </implementation>
        </implementations>
        <description>
          <short>result = hello(greeting_type)</short>
          <long>Returns a greeting according to the given greeting type.</long>
        </description>
    </component>
  </group>
</mapping>

 

You have now finished creating a custom library and the .mff file which adapts it to MapForce. The custom .mff file can now be used in MapForce (see Importing the .mff File Into MapForce). Remember that, in order to execute mappings that use native C++ libraries, you will need to generate C++ code and run the mapping from your C++ code or application.

 

Resolving C++ compile errors

If you get a compiler error at the line shown below, modify the project properties to include a reference to the msado15.dll file.

 

 #import "msado15.dll" rename("EOF", "EndOfFile")

 

In Visual Studio 2008:

 

1.On the Tools menu, click Options.
2.Expand Projects and Solutions > VC++ Directories.
3.Under "Show directories for", select Include files, and add a new entry that points to the directory where msado15.dll file is located (usually, C:\Program Files\Common Files\System\ADO).
4.Build the project.

© 2019 Altova GmbH