aboutsummaryrefslogtreecommitdiff
path: root/odb/header.cxx
blob: 67e4cc05dfba87253bb9abff193713d4c22d2bca (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// file      : odb/header.cxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/common.hxx>
#include <odb/context.hxx>
#include <odb/generate.hxx>

using namespace std;

namespace header
{
  struct class_: traversal::class_, virtual context
  {
    virtual void
    traverse (type& c)
    {
      if (!options.at_once () && class_file (c) != unit.file ())
        return;

      if (object (c))
        traverse_object (c);
      else if (view (c))
        traverse_view (c);
    }

    void
    traverse_object (type&);

    void
    traverse_view (type&);

    void
    traverse_composite (type&);
  };
}

void header::class_::
traverse_object (type& c)
{
  using semantics::data_member;

  data_member* id (id_member (c));
  bool auto_id (id && auto_ (*id));
  bool base_id (id && &id->scope () != &c); // Comes from base.

  data_member* optimistic (context::optimistic (c));

  type* poly_root (polymorphic (c));
  bool poly (poly_root != 0);
  bool poly_derived (poly && poly_root != &c);
  type* poly_base (poly_derived ? &polymorphic_base (c) : 0);
  data_member* discriminator (poly ? context::discriminator (*poly_root) : 0);

  bool abst (abstract (c));
  bool reuse_abst (abst && !poly);

  string const& type (class_fq_name (c));

  os << "// " << class_name (c) << endl
     << "//" << endl;

  // class_traits
  //
  os << "template <>" << endl
     << "struct class_traits< " << type << " >"
     << "{"
     << "static const class_kind kind = class_object;"
     << "};";

  // object_traits
  //
  os << "template <>" << endl
     << "class access::object_traits< " << type << " >"
     << "{"
     << "public:" << endl;

  // object_type & pointer_type
  //
  os << "typedef " << type << " object_type;"
     << "typedef " << c.get<string> ("object-pointer") << " pointer_type;"
     << "typedef odb::pointer_traits<pointer_type> pointer_traits;"
     << endl;

  // polymorphic, root_type, base_type, etc.
  //
  os << "static const bool polymorphic = " << (poly ? "true" : "false") << ";"
     << endl;

  if (poly)
  {
    os << "typedef " << class_fq_name (*poly_root) << " root_type;";

    if (poly_derived)
    {
      os << "typedef " << class_fq_name (*poly_base) << " base_type;"
         << "typedef object_traits<root_type>::discriminator_type " <<
        "discriminator_type;"
         << "typedef polymorphic_concrete_info<root_type> info_type;";

      if (abst)
        os << "typedef polymorphic_abstract_info<root_type> " <<
          "abstract_info_type;";

      // Calculate our hierarchy depth (number of classes).
      //
      size_t depth (polymorphic_depth (c));

      os << endl
         << "static const std::size_t depth = " << depth << "UL;";
    }
    else
    {
      semantics::names* hint;
      semantics::type& t (utype (*discriminator, hint));

      os << "typedef " << t.fq_name (hint) << " discriminator_type;"
         << "typedef polymorphic_map<object_type> map_type;"
         << "typedef polymorphic_concrete_info<object_type> info_type;";

      if (abst)
        os << "typedef polymorphic_abstract_info<object_type> " <<
          "abstract_info_type;";

      os << endl
         << "static const std::size_t depth = 1UL;";
    }

    os << endl;
  }

  // id_type, version_type, etc.
  //
  if (id != 0)
  {
    if (base_id)
    {
      semantics::class_& b (
        dynamic_cast<semantics::class_&> (id->scope ()));
      string const& type (class_fq_name (b));

      os << "typedef object_traits< " << type << " >::id_type id_type;";

      if (optimistic != 0)
        os << "typedef object_traits< " << type << " >::version_type " <<
          "version_type;";

      os << endl;

      if (poly_derived)
        os << "static const bool auto_id = false;";
      else
        os << "static const bool auto_id = object_traits< " << type <<
          " >::auto_id;";
    }
    else
    {
      {
        semantics::names* hint;
        semantics::type& t (utype (*id, hint));
        os << "typedef " << t.fq_name (hint) << " id_type;";
      }

      if (optimistic != 0)
      {
        semantics::names* hint;
        semantics::type& t (utype (*optimistic, hint));
        os << "typedef " << t.fq_name (hint) << " version_type;";
      }

      os << endl
         << "static const bool auto_id = " << (auto_id ? "true;" : "false;");
    }

    os << endl;
  }
  else if (!reuse_abst)
  {
    // Object without id.
    //
    os << "typedef void id_type;"
       << endl
       << "static const bool auto_id = false;"
       << endl;
  }

  // abstract
  //
  os << "static const bool abstract = " << (abst ? "true" : "false") << ";"
     << endl;

  // id ()
  //
  if (id != 0 || !reuse_abst)
  {
    // We want to generate a dummy void id() accessor even if this
    // object has no id to help us in the runtime. This way we can
    // write generic code that will work for both void and non-void
    // ids.
    //
    os << "static id_type" << endl
       << "id (const object_type&);"
       << endl;
  }

  if (!reuse_abst)
  {
    // Cache traits typedefs.
    //
    if (id == 0)
    {
      os << "typedef" << endl
         << "no_id_pointer_cache_traits<pointer_type>" << endl
         << "pointer_cache_traits;"
         << endl
         << "typedef" << endl
         << "no_id_reference_cache_traits<object_type>" << endl
         << "reference_cache_traits;"
         << endl;
    }
    else
    {
      char const* p (session (c) ? "odb::" : "no_op_");

      if (poly_derived)
      {
        os << "typedef" << endl
           << p << "pointer_cache_traits<" <<
          "object_traits<root_type>::pointer_type>" << endl
           << "pointer_cache_traits;"
           << endl
           << "typedef" << endl
           << p << "reference_cache_traits<root_type>" << endl
           << "reference_cache_traits;"
           << endl;
      }
      else
      {
        os << "typedef" << endl
           << p << "pointer_cache_traits<pointer_type>" << endl
           << "pointer_cache_traits;"
           << endl
           << "typedef" << endl
           << p << "reference_cache_traits<object_type>" << endl
           << "reference_cache_traits;"
           << endl;
      }
    }

    // callback ()
    //
    os << "static void" << endl
       << "callback (database&, object_type&, callback_event);"
       <<  endl;

    os << "static void" << endl
       << "callback (database&, const object_type&, callback_event);"
       <<  endl;
  }

  os << "};";
}

void header::class_::
traverse_view (type& c)
{
  string const& type (class_fq_name (c));

  os << "// " << class_name (c) << endl
     << "//" << endl;

  // class_traits
  //
  os << "template <>" << endl
     << "struct class_traits< " << type << " >"
     << "{"
     << "static const class_kind kind = class_view;"
     << "};";

  // view_traits
  //
  os << "template <>" << endl
     << "class access::view_traits< " << type << " >"
     << "{"
     << "public:" << endl;

  // view_type & pointer_type
  //
  os << "typedef " << type << " view_type;"
     << "typedef " << c.get<string> ("object-pointer") << " pointer_type;"
     << endl;

  // callback ()
  //
  os << "static void" << endl
     << "callback (database&, view_type&, callback_event);"
     <<  endl;

  os << "};";
}

namespace header
{
  void
  generate ()
  {
    context ctx;
    ostream& os (ctx.os);

    os << "#include <memory>" << endl
       << "#include <cstddef>" << endl; // std::size_t

    if (ctx.features.polymorphic_object)
      os << "#include <string>" << endl; // For discriminator.

    os << endl;

    os << "#include <odb/core.hxx>" << endl
       << "#include <odb/traits.hxx>" << endl
       << "#include <odb/callback.hxx>" << endl
       << "#include <odb/wrapper-traits.hxx>" << endl
       << "#include <odb/pointer-traits.hxx>" << endl;

    // In case of a boost TR1 implementation, we cannot distinguish
    // between the boost::shared_ptr and std::tr1::shared_ptr usage since
    // the latter is just a using-declaration for the former. To resolve
    // this we will include TR1 traits if the Boost TR1 header is included.
    //
    if (ctx.features.tr1_pointer)
    {
      os << "#include <odb/tr1/wrapper-traits.hxx>" << endl
         << "#include <odb/tr1/pointer-traits.hxx>" << endl;
    }
    else if (ctx.features.boost_pointer)
    {
      os << "#ifdef BOOST_TR1_MEMORY_HPP_INCLUDED" << endl
         << "#  include <odb/tr1/wrapper-traits.hxx>" << endl
         << "#  include <odb/tr1/pointer-traits.hxx>" << endl
         << "#endif" << endl;
    }

    os << "#include <odb/container-traits.hxx>" << endl;

    if (ctx.features.session_object)
      os << "#include <odb/cache-traits.hxx>" << endl;
    else
      os << "#include <odb/no-op-cache-traits.hxx>" << endl;

    if (ctx.features.polymorphic_object)
      os << "#include <odb/polymorphic-info.hxx>" << endl;

    if (ctx.options.generate_query ())
    {
      if (ctx.options.generate_prepared ())
        os << "#include <odb/prepared-query.hxx>" << endl;

      os << "#include <odb/result.hxx>" << endl;

      if (ctx.features.simple_object)
        os << "#include <odb/simple-object-result.hxx>" << endl;

      if (ctx.features.polymorphic_object)
        os << "#include <odb/polymorphic-object-result.hxx>" << endl;

      if (ctx.features.no_id_object)
        os << "#include <odb/no-id-object-result.hxx>" << endl;

      if (ctx.features.view)
        os << "#include <odb/view-result.hxx>" << endl;
    }

    os << endl
       << "#include <odb/details/unused.hxx>" << endl
       << endl;

    // Generate common code.
    //
    traversal::unit unit;
    traversal::defines unit_defines;
    typedefs unit_typedefs (false);
    traversal::namespace_ ns;
    class_ c;

    unit >> unit_defines >> ns;
    unit_defines >> c;
    unit >> unit_typedefs >> c;

    traversal::defines ns_defines;
    typedefs ns_typedefs (false);

    ns >> ns_defines >> ns;
    ns_defines >> c;
    ns >> ns_typedefs >> c;

    os << "namespace odb"
       << "{";

    unit.dispatch (ctx.unit);

    os << "}";
  }
}