Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
polymorphic-info.hxx
Go to the documentation of this file.
1 // file : odb/polymorphic-info.hxx
2 // copyright : Copyright (c) 2005-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_POLYMORPHIC_INFO_HXX
6 #define ODB_POLYMORPHIC_INFO_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <cstddef> // std::size_t
11 #include <typeinfo>
12 
13 #include <odb/forward.hxx> // database, connection
14 #include <odb/schema-version.hxx>
15 #include <odb/traits.hxx>
16 
17 namespace odb
18 {
19  template <typename R>
21  {
22  typedef void (*section_load) (odb::connection&, R&, bool top);
23  typedef void (*section_update) (odb::connection&, const R&);
24 
26  {
29  };
30 
31  struct section_list
32  {
33  std::size_t count;
35  };
36 
37  public:
38  polymorphic_abstract_info (const std::type_info& t,
40  const section_list* s)
41  : type (t), base (b), sections (s) {}
42 
43  bool
45  {
46  for (const polymorphic_abstract_info* p (base); p != 0; p = p->base)
47  if (&b == p)
48  return true;
49 
50  return false;
51  }
52 
53  // Find the "most overridden" section functions.
54  //
56  find_section_load (std::size_t index) const
57  {
58  for (const polymorphic_abstract_info* b (this); b != 0; b = b->base)
59  if (b->sections != 0 &&
60  index < b->sections->count &&
61  b->sections->functions[index].load != 0)
62  return b->sections->functions[index].load;
63 
64  return 0;
65  }
66 
68  find_section_update (std::size_t index) const
69  {
70  for (const polymorphic_abstract_info* b (this); b != 0; b = b->base)
71  if (b->sections != 0 &&
72  index < b->sections->count &&
73  b->sections->functions[index].update != 0)
74  return b->sections->functions[index].update;
75 
76  return 0;
77  }
78 
79  bool
81  std::size_t index) const
82  {
83  return i.sections != 0 &&
84  index < i.sections->count &&
85  i.sections->functions[index].update != 0 &&
86  i.sections->functions[index].update == find_section_update (index);
87  }
88 
89  public:
90  const std::type_info& type;
92 
93  // Sections.
94  //
95  // There could be "concrete" (i.e., not overridden) section in an
96  // abstract class. Which means the section table has to be in
97  // abstract_info.
98  //
100  };
101 
102  template <typename R>
104  {
105  // Have to use access::object_traits directly because of VC10.
106  //
107  typedef R root_type;
109  typedef typename root_traits::id_type id_type;
110  typedef typename root_traits::pointer_type pointer_type;
111  typedef typename root_traits::discriminator_type discriminator_type;
112 
114 
116  {
117  call_callback, // arg points to callback event.
118  call_persist, // arg is not used.
119  call_update, // arg is not used.
120  call_find, // arg points to object id.
121  call_reload, // arg is not used.
122  call_load, // arg points to depth.
123  call_erase // arg points to object id.
124  };
125 
127  typedef bool (*dispatch_function) (
128  call_type, odb::database&, const root_type*, const void* arg);
129  typedef void (*delayed_loader_function) (
130  odb::database&,
131  const id_type&,
132  root_type&,
133  const schema_version_migration*);
134 
135  public:
136  polymorphic_concrete_info (const std::type_info& t,
138  const section_list* s,
139  const discriminator_type& d,
140  create_function cf,
143  : polymorphic_abstract_info<R> (t, b, s),
144  discriminator (d),
145  create (cf), dispatch (df), delayed_loader (dlf)
146  {
147  }
148 
149  public:
154  };
155 
156  // Register concrete type T in the root's map.
157  //
158  template <typename T, database_id DB>
160  {
161  typedef T object_type;
163  typedef typename object_traits::root_type root_type;
164 
167  };
168 
169  // Helper functions that either return the concrete info or NULL
170  // depending on what kind of info we pass (used in query support).
171  //
172  template <typename R>
173  inline const polymorphic_concrete_info<R>*
175  {
176  return &i;
177  }
178 
179  template <typename R>
180  inline const polymorphic_concrete_info<R>*
182  {
183  return 0;
184  }
185 }
186 
187 #include <odb/post.hxx>
188 
189 #endif // ODB_POLYMORPHIC_INFO_HXX
object_traits_impl< object_type, DB > object_traits
Definition: polymorphic-info.hxx:162
Definition: polymorphic-info.hxx:103
delayed_loader_function delayed_loader
Definition: polymorphic-info.hxx:153
bool derived(const polymorphic_abstract_info &b) const
Definition: polymorphic-info.hxx:44
polymorphic_entry()
Definition: polymorphic-info.hxx:120
const section_functions * functions
Definition: polymorphic-info.hxx:34
polymorphic_abstract_info(const std::type_info &t, const polymorphic_abstract_info *b, const section_list *s)
Definition: polymorphic-info.hxx:38
const section_list * sections
Definition: polymorphic-info.hxx:99
const std::type_info & type
Definition: polymorphic-info.hxx:90
R root_type
Definition: polymorphic-info.hxx:107
Definition: polymorphic-info.hxx:159
discriminator_type discriminator
Definition: polymorphic-info.hxx:150
Definition: polymorphic-info.hxx:123
section_load find_section_load(std::size_t index) const
Definition: polymorphic-info.hxx:56
T object_type
Definition: polymorphic-info.hxx:161
root_traits::id_type id_type
Definition: polymorphic-info.hxx:109
Definition: polymorphic-info.hxx:122
call_type
Definition: polymorphic-info.hxx:115
Definition: polymorphic-info.hxx:119
Definition: polymorphic-info.hxx:117
section_load load
Definition: polymorphic-info.hxx:27
polymorphic_concrete_info(const std::type_info &t, const polymorphic_abstract_info< R > *b, const section_list *s, const discriminator_type &d, create_function cf, dispatch_function df, delayed_loader_function dlf)
Definition: polymorphic-info.hxx:136
create_function create
Definition: polymorphic-info.hxx:151
root_traits::discriminator_type discriminator_type
Definition: polymorphic-info.hxx:111
std::size_t count
Definition: polymorphic-info.hxx:33
~polymorphic_entry()
Definition: forward.hxx:126
root_traits::pointer_type pointer_type
Definition: polymorphic-info.hxx:110
Definition: polymorphic-info.hxx:121
object_traits::root_type root_type
Definition: polymorphic-info.hxx:163
Definition: polymorphic-info.hxx:31
pointer_type(* create_function)()
Definition: polymorphic-info.hxx:126
const polymorphic_abstract_info * base
Definition: polymorphic-info.hxx:91
Definition: database.hxx:38
Definition: connection.hxx:33
section_update find_section_update(std::size_t index) const
Definition: polymorphic-info.hxx:68
Definition: polymorphic-info.hxx:118
dispatch_function dispatch
Definition: polymorphic-info.hxx:152
const polymorphic_concrete_info< R > * polymorphic_info(const polymorphic_concrete_info< R > &i)
Definition: polymorphic-info.hxx:174
void(* section_load)(odb::connection &, R &, bool top)
Definition: polymorphic-info.hxx:22
Definition: schema-version.hxx:15
bool(* dispatch_function)(call_type, odb::database &, const root_type *, const void *arg)
Definition: polymorphic-info.hxx:127
bool final_section_update(const polymorphic_abstract_info &i, std::size_t index) const
Definition: polymorphic-info.hxx:80
Definition: polymorphic-info.hxx:20
void(* delayed_loader_function)(odb::database &, const id_type &, root_type &, const schema_version_migration *)
Definition: polymorphic-info.hxx:129
void(* section_update)(odb::connection &, const R &)
Definition: polymorphic-info.hxx:23
section_update update
Definition: polymorphic-info.hxx:28
Definition: polymorphic-info.hxx:25
access::object_traits< root_type > root_traits
Definition: polymorphic-info.hxx:108
polymorphic_abstract_info< R >::section_list section_list
Definition: polymorphic-info.hxx:113
Definition: forward.hxx:95