From 1896d36996ab48ed7271e855d7e32b4e61f64896 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 23 Apr 2012 16:48:00 +0200 Subject: Polymorphic inheritance support --- odb/polymorphic-info.hxx | 118 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 odb/polymorphic-info.hxx (limited to 'odb/polymorphic-info.hxx') diff --git a/odb/polymorphic-info.hxx b/odb/polymorphic-info.hxx new file mode 100644 index 0000000..349f062 --- /dev/null +++ b/odb/polymorphic-info.hxx @@ -0,0 +1,118 @@ +// file : odb/polymorphic-info.hxx +// copyright : Copyright (c) 2005-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_POLYMORPHIC_INFO_HXX +#define ODB_POLYMORPHIC_INFO_HXX + +#include + +#include // database +#include + +namespace odb +{ + template + struct polymorphic_abstract_info + { + polymorphic_abstract_info (const std::type_info& t, + const polymorphic_abstract_info* b) + : type (t), base (b) {} + + bool + derived (const polymorphic_abstract_info& b) const + { + for (const polymorphic_abstract_info* p (base); p != 0; p = p->base) + if (&b == p) + return true; + + return false; + } + + public: + const std::type_info& type; + const polymorphic_abstract_info* base; + }; + + template + struct polymorphic_concrete_info: polymorphic_abstract_info + { + typedef R root_type; + typedef object_traits root_traits; + typedef typename root_traits::id_type id_type; + typedef typename root_traits::pointer_type pointer_type; + typedef typename root_traits::discriminator_type discriminator_type; + + enum call_type + { + call_callback, // arg points to callback event. + call_persist, // arg is not used. + call_update, // arg is not used. + call_find, // arg points to object id. + call_reload, // arg is not used. + call_load, // arg points to depth. + call_erase // arg points to object id. + }; + + typedef pointer_type (*create_function) (); + typedef bool (*dispatch_function) ( + call_type, database&, const root_type*, const void* arg); + typedef void (*delayed_loader_function) ( + database&, const id_type&, root_type&); + + public: + polymorphic_concrete_info (const std::type_info& t, + const polymorphic_abstract_info* b, + const discriminator_type& d, + create_function cf, + dispatch_function df, + delayed_loader_function dlf) + : polymorphic_abstract_info (t, b), + discriminator (d), + create (cf), dispatch (df), delayed_loader (dlf) + { + } + + public: + discriminator_type discriminator; + create_function create; + dispatch_function dispatch; + delayed_loader_function delayed_loader; + }; + + // Register concrete type T in the root's map. + // + template + struct polymorphic_entry + { + typedef T object_type; + typedef odb::object_traits object_traits; + + typedef typename object_traits::root_type root_type; + typedef odb::object_traits root_traits; + + polymorphic_entry (); + ~polymorphic_entry (); + }; + + // Helper functions that either return the concrete info or NULL + // depending on what kind of info we pass (used in query support). + // + template + inline const polymorphic_concrete_info* + polymorphic_info (const polymorphic_concrete_info& i) + { + return &i; + } + + template + inline const polymorphic_concrete_info* + polymorphic_info (const polymorphic_abstract_info&) + { + return 0; + } +} + +#include + +#endif // ODB_POLYMORPHIC_INFO_HXX -- cgit v1.1