diff options
-rw-r--r-- | common/polymorphism/driver.cxx | 32 | ||||
-rw-r--r-- | common/polymorphism/test.hxx | 67 |
2 files changed, 98 insertions, 1 deletions
diff --git a/common/polymorphism/driver.cxx b/common/polymorphism/driver.cxx index efda095..0c9bf1e 100644 --- a/common/polymorphism/driver.cxx +++ b/common/polymorphism/driver.cxx @@ -1821,6 +1821,38 @@ main (int argc, char* argv[]) t.commit (); } } + + // Test 12: polymorphic objects with auto id. + // + { + using namespace test12; + + base b (1); + derived d (2); + + unsigned long id1, id2; + + // Persist. + // + { + transaction t (db->begin ()); + id1 = db->persist (b); + id2 = db->persist (static_cast<root&> (d)); + t.commit (); + } + + // Load. + // + { + transaction t (db->begin ()); + auto_ptr<root> pb (db->load<root> (id1)); + auto_ptr<root> pd (db->load<root> (id2)); + t.commit (); + + assert (*pb == b); + assert (*pd == d); + } + } } catch (const odb::exception& e) { diff --git a/common/polymorphism/test.hxx b/common/polymorphism/test.hxx index 5e72b77..99711b5 100644 --- a/common/polymorphism/test.hxx +++ b/common/polymorphism/test.hxx @@ -887,7 +887,7 @@ namespace test10 struct base: root { base () {} - base (unsigned long i, unsigned long n) : root (i), num (n) {} + base (unsigned long i, unsigned long n): root (i), num (n) {} unsigned long num; @@ -984,4 +984,69 @@ namespace test11 }; } +// Test polymorphic classes with auto id. +// +#pragma db namespace table("t12_") +namespace test12 +{ + #pragma db object polymorphic + struct root + { + virtual ~root () = 0; // Auto-abstract. + + #pragma db id auto + unsigned long id; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (root)) + return false; + + return id == r.id; + } + }; + + inline root:: + ~root () {} + + inline bool + operator== (const root& x, const root& y) {return x.compare (y);} + + #pragma db object + struct base: root + { + base () {} + base (unsigned long n): num (n) {} + + unsigned long num; + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (base)) + return false; + + const base& b (static_cast<const base&> (r)); + return root::compare (r, false) && num == b.num; + } + }; + + #pragma db object + struct derived: base + { + derived () {} + derived (unsigned long n): base (n) {} + + virtual bool + compare (const root& r, bool tc = true) const + { + if (tc && typeid (r) != typeid (derived)) + return false; + + return base::compare (r, false); + } + }; +} + #endif // TEST_HXX |