aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/polymorphism/driver.cxx
blob: c355fc1526bb5cda4eef97a0d7fe341972db7a63 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// file      : examples/cxx/hybrid/polymorphism/driver.cxx
// copyright : not copyrighted - public domain

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

#include "supermen.hxx"
#include "supermen-pimpl.hxx"
#include "supermen-simpl.hxx"

using namespace std;

void
check_load (); // Defined after main().

int
main (int argc, char* argv[])
{
  // Check that the load in substitution and inheritance hashmaps
  // is not too high.
  //
#ifndef NDEBUG
  check_load ();
#endif

  const char* input;

  if (argc < 2)
  {
    input = "STDIN";
    cerr << "XML file not specified, reading from STDIN" << endl;
  }
  else
    input = argv[1];

  try
  {
    // Parse.
    //
    supermen_paggr supermen_p;

    // The last argument to the document's constructor indicates that we
    // are parsing polymorphic XML documents.
    //
    xml_schema::document_pimpl doc_p (
      supermen_p.root_parser (),
      supermen_p.root_name (),
      true);

    supermen_p.pre ();

    if (argc < 2)
      doc_p.parse (cin);
    else
      doc_p.parse (argv[1]);

    auto_ptr<supermen> sm (supermen_p.post ());

    // Print what we've got.
    //
    for (supermen::person_iterator i = sm->person ().begin ();
         i != sm->person ().end ();
         ++i)
    {
      person& p = *i;

      // You can use the standard C++ RTTI or custom type information
      // provided by the object model (--generate-typeinfo option) to
      // detect the object's actual (dynamic) type.
      //
      if (p._dynamic_type () == batman::_static_type ())
      {
        batman& b = static_cast<batman&> (p);
        cerr << b.name () << ", batman, wing span " << b.wing_span () << endl;
      }
      else if (superman* s = dynamic_cast<superman*> (&p))
      {
        cerr << s->name () << ", ";

        if (s->can_fly ())
          cerr << "flying ";

        cerr << "superman" << endl;
      }
      else
      {
        cerr << p.name () << ", ordinary person" << endl;
      }
    }

    // Add another superman entry.
    //
    auto_ptr<superman> s (new superman);
#ifdef XSDE_STL
    s->name ("Clark Kent");
#else
    s->name (xml_schema::strdupx ("Clark Kent"));
#endif
    s->can_fly (true);
    sm->person ().push_back (s.release ());

    // Serialize.
    //
    supermen_saggr supermen_s;

    // The last argument to the document's constructor indicates that we
    // are serializing polymorphic XML documents.
    //
    xml_schema::document_simpl doc_s (
      supermen_s.root_serializer (),
      supermen_s.root_name (),
      true);

    doc_s.add_no_namespace_schema ("supermen.xsd");

    supermen_s.pre (*sm);
    doc_s.serialize (cout, xml_schema::document_simpl::pretty_print);
    supermen_s.post ();
  }
  catch (const xml_schema::parser_exception& e)
  {
    cerr << input << ":" << e.line () << ":" << e.column () << ": "
         << e.text () << endl;
    return 1;
  }
  catch (const xml_schema::serializer_exception& e)
  {
    cerr << "error: " << e.text () << endl;
    return 1;
  }
  catch (const std::ios_base::failure&)
  {
    cerr << input << ": unable to open or read/write failure" << endl;
    return 1;
  }

  return 0;
}

#ifndef NDEBUG
// Check that the load in substitution and inheritance hashmaps is not
// too high. See the C++/Parser and C++/Serializer Mappings Getting
// Started Guides for details.
//
void
check_load ()
{
  // Parser.
  //
  float load = (float) xml_schema::parser_smap_elements ();
  load /= xml_schema::parser_smap_buckets ();

  if (load > 0.8)
  {
    cerr << "substitution hashmap load is " << load << endl;
    cerr << "time to increase XSDE_PARSER_SMAP_BUCKETS" << endl;
  }

#ifdef XSDE_PARSER_VALIDATION
  load = (float) xml_schema::parser_imap_elements ();
  load /= xml_schema::parser_imap_buckets ();

  if (load > 0.8)
  {
    cerr << "inheritance hashmap load is " << load << endl;
    cerr << "time to increase XSDE_PARSER_IMAP_BUCKETS" << endl;
  }
#endif

  // Serializer.
  //
  load = (float) xml_schema::serializer_smap_elements ();
  load /= xml_schema::serializer_smap_buckets ();

  if (load > 0.8)
  {
    cerr << "substitution hashmap load is " << load << endl;
    cerr << "time to increase XSDE_SERIALIZER_SMAP_BUCKETS" << endl;
  }

  load = (float) xml_schema::serializer_smap_bucket_elements ();
  load /= xml_schema::serializer_smap_bucket_buckets ();

  if (load > 0.8)
  {
    cerr << "substitution inner hashmap load is " << load << endl;
    cerr << "time to increase XSDE_SERIALIZER_SMAP_BUCKET_BUCKETS" << endl;
  }

#ifdef XSDE_SERIALIZER_VALIDATION
  load = (float) xml_schema::serializer_imap_elements ();
  load /= xml_schema::serializer_imap_buckets ();

  if (load > 0.8)
  {
    cerr << "inheritance hashmap load is " << load << endl;
    cerr << "time to increase XSDE_SERIALIZER_IMAP_BUCKETS" << endl;
  }
#endif
}
#endif