aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/serializer/polymorphism/supermen-simpl-mixin.cxx
blob: e0c2f0ff8f7c8a52459e300f32f78cff9657996f (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
// file      : examples/cxx/serializer/polymorphism/supermen-simpl-mixin.cxx
// copyright : not copyrighted - public domain

#include "supermen-simpl-mixin.hxx"

//
//
void person_simpl::
pre (const person& p)
{
  person_ = &p;
}

std::string person_simpl::
name ()
{
  return person_->name ();
}

//
//
bool superman_simpl::
can_fly ()
{
  return static_cast<const superman*> (person_)->can_fly ();
}

//
//
unsigned int batman_simpl::
wing_span ()
{
  return static_cast<const batman*> (person_)->wing_span ();
}

//
//
void supermen_simpl::
pre (const supermen& s)
{
  supermen_ = &s;
  i_ = s.begin ();
}

bool supermen_simpl::
person_next ()
{
  return i_ != supermen_->end ();
}

const person& supermen_simpl::
person ()
{
  const ::person& p = **i_++;

  // Map type id to serializer type. We could have also done this in a
  // custom serializer_map implementation in which case we could simply
  // pass a pointer to the person instance as type id.
  //
  xml_schema::serializer_context& ctx = _context ();

  switch (p.type ())
  {
  case person_type:
    {
      ctx.type_id (person_sskel::_static_type ());
      break;
    }
  case superman_type:
    {
      ctx.type_id (superman_sskel::_static_type ());
      break;
    }
  case batman_type:
    {
      ctx.type_id (batman_sskel::_static_type ());
      break;
    }
  }

  return p;
}