summaryrefslogtreecommitdiff
path: root/examples/cxx/tree/polymorphism/driver.cxx
blob: 582e2dd1c41a9cfe6886f501c8e37d768b5dda6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// file      : examples/cxx/tree/polymorphism/driver.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : not copyrighted - public domain

#include <memory>   // std::auto_ptr
#include <iostream>

#include "supermen.hxx"

using std::cerr;
using std::endl;
using std::auto_ptr;

int
main (int argc, char* argv[])
{
  if (argc != 2)
  {
    cerr << "usage: " << argv[0] << " supermen.xml" << endl;
    return 1;
  }

  try
  {
    auto_ptr<supermen> sm (supermen_ (argv[1]));

    supermen copy (*sm); // Dynamic types are preserved in copies.

    // Print what we've got.
    //
    for (supermen::person_const_iterator i (copy.person ().begin ());
         i != copy.person ().end ();
         ++i)
    {
      cerr << i->name ();

      if (const superman* s = dynamic_cast<const superman*> (&*i))
      {
        if (s->can_fly ())
          cerr << ", flying superman";
        else
          cerr << ", superman";
      }

      cerr << endl;
    }

    // Serialize back to XML.
    //
    xml_schema::namespace_infomap map;
    map[""].schema = "supermen.xsd";

    supermen_ (std::cout, copy, map);
  }
  catch (const xml_schema::exception& e)
  {
    cerr << e << endl;
    return 1;
  }
}