From cae4da2e3e061ec9cb3e244908db36816085e2fe Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 2 Jul 2015 11:44:57 +0200 Subject: C++ type mapping support for container elements --- common/as/driver.cxx | 49 ++++++++++++++++++++++++++++++++------------ common/as/test.hxx | 57 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 84 insertions(+), 22 deletions(-) diff --git a/common/as/driver.cxx b/common/as/driver.cxx index ff65344..6305922 100644 --- a/common/as/driver.cxx +++ b/common/as/driver.cxx @@ -33,13 +33,17 @@ main (int argc, char* argv[]) { using namespace test1; - object o1 (true, 123, 234); + object o1 (true, green, 123, 234); o1.m[false] = 123; o1.v.push_back (o1.ip); + o1.cv.push_back (red); + o1.cv.push_back (green); - object o2 (false, 234, 456); + object o2 (false, blue, 234, 456); o2.m[true] = 234; o2.v.push_back (o2.ip); + o2.cv.push_back (green); + o2.cv.push_back (blue); { transaction t (db->begin ()); @@ -59,18 +63,23 @@ main (int argc, char* argv[]) } o1.b = false; + o1.c = blue; o1.ip.first++; o1.ip.second--; o1.m[false]++; o1.m[true] = 234; o1.v.back () = o1.ip; + o1.cv.modify_front () = green; + o1.cv.push_back (red); o2.b = true; + o2.c = red; o2.ip.first--; o2.ip.second++; o2.m[true]--; o2.m[false] = 345; o2.v.push_back (o2.ip); + o2.cv.pop_back (); { transaction t (db->begin ()); @@ -96,8 +105,13 @@ main (int argc, char* argv[]) using namespace test2; object o1; + o1.v.push_back (null_bool ()); + o1.v.push_back (false); + object o2; o2.b = true; + o2.v.push_back (true); + o2.v.push_back (null_bool ()); { transaction t (db->begin ()); @@ -112,12 +126,16 @@ main (int argc, char* argv[]) auto_ptr p2 (db->load (o2.id)); t.commit (); - assert (p1->b == o1.b); - assert (p2->b == o2.b); + assert (*p1 == o1); + assert (*p2 == o2); } o1.b = false; + o1.v[0] = true; + o1.v[1].reset (); + o2.b.reset (); + o2.v.push_back (false); { transaction t (db->begin ()); @@ -132,8 +150,8 @@ main (int argc, char* argv[]) auto_ptr p2 (db->load (o2.id)); t.commit (); - assert (p1->b == o1.b); - assert (p2->b == o2.b); + assert (*p1 == o1); + assert (*p2 == o2); } } @@ -144,10 +162,14 @@ main (int argc, char* argv[]) object o1; o1.ip = intp (0, 0); // NULL + o1.npv.push_back (o1.np); + o1.ipv.push_back (o1.ip); object o2; o2.np = intp (123, 234); o1.ip = intp (234, 123); + o2.npv.push_back (o2.np); + o2.ipv.push_back (o2.ip); { transaction t (db->begin ()); @@ -162,15 +184,16 @@ main (int argc, char* argv[]) auto_ptr p2 (db->load (o2.id)); t.commit (); - assert (p1->np == o1.np && p1->ip == o1.ip); - assert (p2->np == o2.np && p2->ip == o2.ip); + assert (*p1 == o1); + assert (*p2 == o2); } - o1.np = intp (234, 456); - o1.ip = intp (456, 234); + o1.np = o1.npv[0] = intp (234, 456); + o1.ip = o1.ipv.modify_at (0) = intp (456, 234); o2.np.reset (); - o2.ip = intp (0, 0); // NULL + o2.npv[0].reset (); + o2.ip = o2.ipv.modify_at (0) = intp (0, 0); // NULL { transaction t (db->begin ()); @@ -185,8 +208,8 @@ main (int argc, char* argv[]) auto_ptr p2 (db->load (o2.id)); t.commit (); - assert (p1->np == o1.np && p1->ip == o1.ip); - assert (p2->np == o2.np && p2->ip == o2.ip); + assert (*p1 == o1); + assert (*p2 == o2); } } } diff --git a/common/as/test.hxx b/common/as/test.hxx index 4839f18..676f599 100644 --- a/common/as/test.hxx +++ b/common/as/test.hxx @@ -11,6 +11,7 @@ #include // pair #include +#include #include // Test basic type mapping functionality. @@ -18,6 +19,24 @@ #pragma db namespace table("t1_") namespace test1 { + enum color {red, green, blue}; + + inline const char* + color_to_string (color c) + { + return c == red ? "RED" : (c == green ? "GREEN" : "BLUE"); + } + + inline color + string_to_color (const std::string& s) + { + return s == "RED" ? red : (s == "GREEN" ? green : blue); + } + + #pragma db map type(color) as(std::string) \ + to(test1::color_to_string (?)) \ + from(test1::string_to_color (?)) + typedef std::pair intp; #pragma db value @@ -40,29 +59,34 @@ namespace test1 // Class-scope mapping. // #pragma db map type(bool) as(std::string) \ - to((?) ? "true" : "false") \ - from((?) == "true") + to((?) ? "true" : "false") \ + from((?) == "true") #pragma db id auto unsigned long id; bool b; + color c; intp ip; - #pragma db transient std::map m; - - #pragma db transient std::vector v; + odb::vector cv; object () {} - object (bool b_, int n1, int n2): b (b_), ip (n1, n2) {} + object (bool b_, color c_, int n1, int n2): b (b_), c (c_), ip (n1, n2) {} }; inline bool operator== (const object& x, const object y) { - return x.b == y.b && x.ip == y.ip /*&& x.m == y.m && x.v == y.v*/; + return + x.b == y.b && + x.c == y.c && + x.ip == y.ip && + x.m == y.m && + x.v == y.v && + x.cv == y.cv; } } @@ -101,7 +125,14 @@ namespace test2 unsigned long id; odb::nullable b; + std::vector > v; }; + + inline bool + operator== (const object& x, const object y) + { + return x.b == y.b && x.v == y.v; + } } // Test wrapped simple type mapping. @@ -150,10 +181,18 @@ namespace test3 odb::nullable np; intp ip; + + std::vector > npv; + odb::vector ipv; }; + + inline bool + operator== (const object& x, const object y) + { + return x.np == y.np && x.ip == y.ip && x.npv == y.npv && x.ipv == y.ipv; + } } -//@@ Test wrapped id and version. -//@@ Containers. +//@@ Test wrapped id and version. With container, obj-ptr. #endif // TEST_HXX -- cgit v1.1