aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/hybrid/polyroot/driver.cxx
blob: a68bf16eb2f14cb50423fac01090ba632692bdae (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// file      : examples/cxx/hybrid/polyroot/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;
using xml_schema::ro_string;

// Customize the xml_schema::document object to handle a polymorphic
// root element. For more information see the polyroot and multiroot
// examples in the examples/cxx/parser/ directory.
//
class document_pimpl: public xml_schema::document_pimpl
{
public:
  // Passing the root element name to xml_schema::document_pimpl
  // constructor indicates that we are doing polymorphic parsing.
  // The root element name is used to automatically translate
  // substitutions to type information.
  //
  document_pimpl (person_paggr& paggr)
      : xml_schema::document_pimpl (person_paggr::root_name ()),
        paggr_ (paggr),
        parser_used_ (0)
  {
  }

  person*
  result ()
  {
    return result_.release ();
  }

protected:
  // This function is called to obtain the root element type parser.
  // If the returned pointer is 0 then the whole document content
  // is ignored. The type argument contains the XML Schema type
  // if xsi:type attribute or an element that substitutes the root
  // was specified and 0 otherwise.
  //
  virtual xml_schema::parser_base*
  start_root_element (const ro_string& ns,
                      const ro_string& name,
                      const char* type)
  {
    if (name != person_paggr::root_name () ||
        ns != person_paggr::root_namespace ())
    {
      // If the runtime and the generated code are built with
      // validation enabled then we can also set an XML Schema
      // error.
      //
#ifdef XSDE_PARSER_VALIDATION
      context_.schema_error (
        xml_schema::parser_schema_error::unexpected_element);
#endif
      return 0;
    }

    // Search for the parser. If type is 0 then there is no xsi:type and
    // static type should be used.
    //
    if (type == 0)
      parser_used_ = &paggr_.root_parser ();
    else
    {
      // The map returns a generic parser_base which we will cast to
      // person_pskel in order to call the pre() and post_person()
      // callbacks. If the runtime and the generated code are built
      // with the mixin parser reuse style then we have to use
      // dynamic_cast because of the virtual inheritance.
      //
      xml_schema::parser_base* p = paggr_.root_map ().find (type);

#ifdef XSDE_REUSE_STYLE_MIXIN
      parser_used_ = dynamic_cast<person_pskel*> (p);
#else
      parser_used_ = static_cast<person_pskel*> (p);
#endif
    }

    if (parser_used_ != 0)
      parser_used_->pre ();

    return parser_used_;
  }

  // This function is called to indicate the completion of document
  // parsing. The parser argument contains the pointer returned by
  // start_root_element.
  //
  virtual void
  end_root_element (const ro_string& /* ns */,
                    const ro_string& /* name */,
                    xml_schema::parser_base* /* parser */)
  {
    // Instead of caching the current parser in parser_used_, we
    // could also cast the parser argument to the person_pskel
    // type.
    //
    if (parser_used_ != 0)
      result_.reset (parser_used_->post_person ());
  }

public:
  // If we need to be able to reset and reuse the parser after
  // an error then we also need to override reset() and reset
  // the parser that was used last. Note that you always need
  // to call _reset() from the base.
  //
  virtual void
  reset ()
  {
    xml_schema::document_pimpl::reset ();

    if (parser_used_ != 0)
      parser_used_->_reset ();
  }

private:
  person_paggr& paggr_;
  person_pskel* parser_used_;
  std::auto_ptr<person> result_;
};

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.
    //
    person_paggr person_p;

    // Use our customized document parser. It automatically calls
    // pre() and post() on the chosen parser and stores the result.
    //
    document_pimpl doc_p (person_p);

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

    auto_ptr<person> p (doc_p.result ());

    // Print what we've got. 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.get ()))
    {
      cerr << s->name () << ", ";

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

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

    // Serialize.
    //
    person_saggr person_s;
    person_sskel* ps = 0;

    // Determine the root element serializer to use based on the object's
    // dynamic type.
    //
    const string& dt = p->_dynamic_type ();

    if (dt == person::_static_type ())
      ps = &person_s.root_serializer ();
    else
    {
      // The map returns a generic serializer_base which we will cast to
      // person_sskel in order to call the pre() and post() callbacks. If
      // the runtime and the generated code are built with the mixin
      // serializer reuse style then we have to use dynamic_cast because
      // of the virtual inheritance.
      //
      xml_schema::serializer_base* s =
        person_s.root_map ().find (dt.c_str ());

#ifdef XSDE_REUSE_STYLE_MIXIN
      ps = dynamic_cast<person_sskel*> (s);
#else
      ps = static_cast<person_sskel*> (s);
#endif
    }

    // Create a document serializer for this object. Note that we pass
    // true as the third argument to indicate polymorphic serialization
    // as well as the root element's static type as the last argument
    // which is necessary if the actual root type can differ from its
    // static type.
    //
    xml_schema::document_simpl doc_s (
      *ps, person_s.root_name (), true, person_sskel::_static_type ());

    doc_s.add_no_namespace_schema ("supermen.xsd");

    ps->pre (*p);
    doc_s.serialize (std::cout, xml_schema::document_simpl::pretty_print);
    ps->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