Tree/Customization guide

From Code Synthesis Wiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 14:04, 25 September 2006
Boris (Talk | contribs)
(Start the customization guide)
← Previous diff
Revision as of 14:46, 25 September 2006
Boris (Talk | contribs)
(Introduction)
Next diff →
Line 66: Line 66:
typedef ::templates::names<char> names; typedef ::templates::names<char> names;
} }
 +
 +Now what if all we want to do is add a simple function to the <code>names</code> type? It would be too much work if we had to implement all the code that gets generated ourselves. Fortunately we don't have to. We can ask the compiler to generate the standard mapping with a different name and then inherit our custom type from the generated one:
 +
 + --custom-type names=/names_base
 +
 +This will result in the following C++ code:
 +
 + namespace hello
 + {
 + class names_base;
 + class names;
 +
 + class names_base
 + {
 + ...
 + };
 + }
 +
 +In our implementation of the <code>names</code> class we can use <code>names_base</code> as the base. Here is another example:
 +
 + --custom-type names=::templates::names<names_base>/names_base
 +
 +This results in the following C++ code being generated:
 +
 + namespace hello
 + {
 + class names_base;
 + class ::templates::names<names_base> names;
 +
 + class names_base
 + {
 + ...
 + };
 + }
 +
 +While this example may seem a bit far-fetched, this technique is actually used in some special cases as will be shown later.

Revision as of 14:46, 25 September 2006

Introduction

XSD provides you with mechanisms to customize the generated type system for the C++/Tree mapping. Common customization examples include:

  • using a different type for one of the built-in types (e.g., boost::gregorian::date from the Boost libraries for xsd:date)
  • adding a member function or data member to one or more generated types (e.g., add a print() function)
  • adding virtual functions to the base type of a type hierarchy and implementing them in the derived types (e.g., when using xsi:type dynamic typing and/or substitution groups)

XSD provided two command-line options, --custom-type and --custom-type-regex, that allow you to specify which types should be customized and how these types will be customized. The format for the --custom-type option is as follows:

 --custom-type name[=type[/base]]

The name component specifies the name of a type as defined in XML Schema. The name is unqualified since the target namespace of the schema being compiled is always assumed. The optional type component is a C++ type name that should be used instead of the generated type. If type is empty or not specified then the same name as the XML Schema type name is assumed. Finally, if the optional base component is provided then the standard mapping for the type is still generated but with this name. A few examples should make all this clear. Suppose we have the following schema:

<schema xmlns="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.example.com/hello">

  <complexType name="names">
    <sequence>
      <element name="name" type="string"/>
    </sequence>
  </complexType>

</schema>

Compiling this schema without specifying any options will result in the following C++ code:

namespace hello
{
  class names
  {
    ...
  };
}

Now we compile the above schema with the following option:

--custom-type names

The generated code will look like this:

namespace hello
{
  class names;
}

The compiler simply generated a forward declaration for names expecting you to provide the implementation. Let's say we want to use the my_names type instead. Then we can use the following option:

--custom-type names=my_names

The generated code will look like this:

namespace hello
{
  typedef my_names names;
}

What if we wanted to use class template names which is defined in namespace templates? Then we could use the following option:

--custom-type names=::templates::names<char>

The resulting code would look like this:

namespace hello
{
  typedef ::templates::names<char> names;
}

Now what if all we want to do is add a simple function to the names type? It would be too much work if we had to implement all the code that gets generated ourselves. Fortunately we don't have to. We can ask the compiler to generate the standard mapping with a different name and then inherit our custom type from the generated one:

--custom-type names=/names_base

This will result in the following C++ code:

namespace hello
{
  class names_base;
  class names;

  class names_base
  {
    ...
  };
}

In our implementation of the names class we can use names_base as the base. Here is another example:

--custom-type names=::templates::names<names_base>/names_base

This results in the following C++ code being generated:

namespace hello
{
  class names_base;
  class ::templates::names<names_base> names;

  class names_base
  {
    ...
  };
}

While this example may seem a bit far-fetched, this technique is actually used in some special cases as will be shown later.

Personal tools