[xsd-users] Customize sequence and optionals

Boris Kolpackov boris at codesynthesis.com
Thu Jan 29 04:06:51 EST 2009


Hi Stefan,

WEBER Stefan <stefan.weber at efgfp.com> writes:

> Isn't there a way to access the optionals containing type given the
> optional one? Such that I could do something like:
> 
> template<class Optional>
> void createIfNotPresent(Optional& optionalType) {
>     if(!optionalType) {
>         optionalType.set( std::auto_ptr<Optional::type>(new
> Optional::type()));
>     }
> }

You can do it like this:

template <typename X>
struct DefValueFactory;

template <typename T>
struct DefValueFactory<xsd::cxx::tree::optional<T, false> >
{
  typedef xsd::cxx::tree::optional<T, false> Optional;

  void set (Optional& x)
  {
    if (!x)
      x.set (std::auto_ptr<T> (new T ());
  }
};

template <typename T>
struct DefValueFactory<xsd::cxx::tree::optional<T, true> >
{
  typedef xsd::cxx::tree::optional<T, true> Optional;

  void set (Optional& x)
  {
    if (!x)
      x.set (T ());
  }
};

template<class Optional>
void createIfNotPresent (Optional& optionalType) 
{
  DefValueFactory<Optional>::set (optionalType);
}

The second template argument to tree::optional indicates whether T
is a fundamental C++ type (e.g., int, double, etc.). For these
types there is no auto_ptr version in the optional container.

Boris




More information about the xsd-users mailing list