aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/serializer/validating/inheritance-map.cxx
blob: 2c239fac19fa63cc048decb9b3d154bdafcafe20 (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
// file      : xsde/cxx/serializer/validating/inheritance-map.cxx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/config.hxx>

#include <string.h> // strcmp

#ifndef XSDE_EXCEPTIONS
#  include <assert.h> // assert
#  include <stdlib.h> // exit
#endif

#ifdef XSDE_CUSTOM_ALLOCATOR
#  include <xsde/cxx/allocator.hxx>
#endif

#include <xsde/cxx/serializer/validating/inheritance-map.hxx>
#include <xsde/cxx/serializer/validating/inheritance-map-load.hxx>

namespace xsde
{
  namespace cxx
  {
    namespace serializer
    {
      namespace validating
      {
        inheritance_map* inheritance_map_init::map = 0;
        size_t inheritance_map_init::count = 0;

        bool inheritance_map::
        check (const char* derived, const char* base) const
        {
          if (strcmp (derived, base) == 0)
            return true;

          const void* p = find (derived);

          if (p)
          {
            const char* b = *static_cast<const char* const*> (p);
            return strcmp (base, b) == 0 ? true : check (b, base);
          }

          return false;
        }

        // inheritance_map_init
        //
        inheritance_map_init::
        inheritance_map_init ()
        {
          if (count == 0)
          {
#ifndef XSDE_CUSTOM_ALLOCATOR
            map = new inheritance_map (XSDE_SERIALIZER_IMAP_BUCKETS);
#else
            map = static_cast<inheritance_map*> (
              alloc (sizeof (inheritance_map)));

#ifdef XSDE_EXCEPTIONS
            alloc_guard mg (map);
            new (map) inheritance_map (XSDE_SERIALIZER_IMAP_BUCKETS);
            mg.release ();
#else
            if (map)
              new (map) inheritance_map (XSDE_SERIALIZER_IMAP_BUCKETS);
#endif
#endif

#ifndef XSDE_EXCEPTIONS
            if (map == 0 || map->_error () != inheritance_map::error_none)
            {
              // This is static initialization so there is nothing we can do.
              // The best thing is to fail fast. abort() would have probably
              // been the better choice here but it is not available on some
              // platforms (notably, WinCE).
              //
              assert (false);
              exit (1);
            }
#endif
          }

          ++count;
        }

        inheritance_map_init::
        ~inheritance_map_init ()
        {
          if (--count == 0)
          {
#ifndef XSDE_CUSTOM_ALLOCATOR
            delete map;
#else
            map->~inheritance_map ();
            cxx::free (map);
#endif
          }
        }

        // inheritance_map_entry
        //
        inheritance_map_entry::
        inheritance_map_entry (const char* derived, const char* base)
        {
          inheritance_map& m = inheritance_map_instance ();
          m.insert (derived, base);

#ifndef XSDE_EXCEPTIONS
          if (m._error () != inheritance_map::error_none)
          {
            // This is static initialization so there is nothing we can do.
            // The best thing is to fail fast. abort() would have probably
            // been the better choice here but it is not available on some
            // platforms (notably, WinCE).
            //
            assert (false);
            exit (1);
          }
#endif
        }

        //
        //
        size_t
        serializer_imap_elements ()
        {
          return inheritance_map_instance ().size ();
        }
      }
    }
  }
}