From c225be5dea36eaaa3df7f24d33a9644a9158b6a7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 31 Aug 2012 10:03:46 +0200 Subject: Add support for virtual data members New test: common/virtual. --- common/makefile | 1 + common/virtual/driver.cxx | 128 ++++++++++++++++++++++++++++++++++++++++++ common/virtual/makefile | 113 +++++++++++++++++++++++++++++++++++++ common/virtual/test.hxx | 139 ++++++++++++++++++++++++++++++++++++++++++++++ common/virtual/test.std | 0 5 files changed, 381 insertions(+) create mode 100644 common/virtual/driver.cxx create mode 100644 common/virtual/makefile create mode 100644 common/virtual/test.hxx create mode 100644 common/virtual/test.std (limited to 'common') diff --git a/common/makefile b/common/makefile index 9965095..9356327 100644 --- a/common/makefile +++ b/common/makefile @@ -41,6 +41,7 @@ template \ transaction \ types \ view \ +virtual \ wrapper thread_tests := threads diff --git a/common/virtual/driver.cxx b/common/virtual/driver.cxx new file mode 100644 index 0000000..05acfcb --- /dev/null +++ b/common/virtual/driver.cxx @@ -0,0 +1,128 @@ +// file : common/virtual/driver.cxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test virtual data members. +// + +#include // std::auto_ptr +#include +#include + +#include +#include +#include + +#include + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr db (create_database (argc, argv)); + + // Test basic virtual data member functionality. + // + { + using namespace test1; + + object o; + o.i (123); + o.c1.i = 123; + o.c1.s = "abc"; + o.v1.push_back ("abc"); + o.v1.push_back ("abd"); + o.v1.push_back ("abe"); + o.p1 = new object; + + { + transaction t (db->begin ()); + db->persist (*o.p1); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p (db->load (o.id1.v)); + t.commit (); + + assert (o == *p); + } + } + + // Test pragma resolution to virtual data member. + // + { + using namespace test2; + + object1 o1 (1); + o1.o2 = new object2 (1); + o1.o2->o1 = &o1; + + { + transaction t (db->begin ()); + db->persist (*o1.o2); + o1.n1 = o1.o2->id; + db->persist (o1); + t.commit (); + } + + { + session s; + transaction t (db->begin ()); + auto_ptr p (db->load (o1.id)); + t.commit (); + + assert (p->o2->id == o1.o2->id); + } + + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + result r (db->query (query::object2::id == o1.o2->id)); + result::iterator i (r.begin ()); + assert (i != r.end () && i->i == o1.n1); + assert (++i == r.end ()); + t.commit (); + } + + { + typedef odb::query query; + typedef odb::result result; + + transaction t (db->begin ()); + result r (db->query (query::o2::id == o1.o2->id)); + result::iterator i (r.begin ()); + assert (i != r.end () && i->i == o1.n1); + assert (++i == r.end ()); + t.commit (); + } + + { + typedef odb::result result; + + transaction t (db->begin ()); + result r (db->query ()); + result::iterator i (r.begin ()); + assert (i != r.end () && i->i == o1.n1); + assert (++i == r.end ()); + t.commit (); + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/virtual/makefile b/common/virtual/makefile new file mode 100644 index 0000000..92ae1d6 --- /dev/null +++ b/common/virtual/makefile @@ -0,0 +1,113 @@ +# file : common/virtual/makefile +# copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := driver.cxx +odb_hdr := test.hxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o) $(odb_hdr:.hxx=-odb.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +common.l := $(out_root)/libcommon/common/common.l +common.l.cpp-options := $(out_root)/libcommon/common/common.l.cpp-options + +driver := $(out_base)/driver +dist := $(out_base)/.dist +test := $(out_base)/.test +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/odb/stub.make,\ + odb: odb,odb-rules: odb_rules) + +# Build. +# +$(driver): $(cxx_obj) $(common.l) +$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base) +$(cxx_obj) $(cxx_od): $(common.l.cpp-options) + +genf := $(addprefix $(odb_hdr:.hxx=-odb),.hxx .ixx .cxx) $(odb_hdr:.hxx=.sql) +gen := $(addprefix $(out_base)/,$(genf)) + +$(gen): $(odb) +$(gen): odb := $(odb) +$(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ +--generate-query --generate-session --table-prefix virtual_ +$(gen): cpp_options := -I$(src_base) +$(gen): $(common.l.cpp-options) + +$(call include-dep,$(cxx_od),$(cxx_obj),$(gen)) + +# Alias for default target. +# +$(out_base)/: $(driver) + +# Dist +# +name := $(subst /,-,$(subst $(src_root)/common/,,$(src_base))) + +$(dist): db_id := @database@ +$(dist): sources := $(cxx_tun) +$(dist): headers := $(odb_hdr) +$(dist): data_dist := test.std +$(dist): export name := $(name) +$(dist): export extra_dist := $(data_dist) $(call vc9projs,$(name)) \ +$(call vc10projs,$(name)) +$(dist): + $(call dist-data,$(sources) $(headers) $(data_dist)) + $(call meta-automake,../template/Makefile.am) + $(call meta-vc9projs,../template/template,$(name)) + $(call meta-vc10projs,../template/template,$(name)) + +# Test. +# +$(test): $(driver) $(src_base)/test.std + $(call schema) + $(call message,test $<,$< --options-file $(dcf_root)/db.options \ +>$(out_base)/test.out) + $(call message,,diff -u $(src_base)/test.std $(out_base)/test.out) + $(call message,,rm -f $(out_base)/test.out) + +# Clean. +# +$(clean): \ + $(driver).o.clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) \ + $(addprefix $(out_base)/,$(odb_hdr:.hxx=-odb.cxx.hxx.clean)) + $(call message,,rm -f $(out_base)/test.out) + +# Generated .gitignore. +# +ifeq ($(out_base),$(src_base)) +$(driver): | $(out_base)/.gitignore + +$(out_base)/.gitignore: files := driver $(genf) +$(clean): $(out_base)/.gitignore.clean + +$(call include,$(bld_root)/git/gitignore.make) +endif + +# How to. +# +$(call include,$(bld_root)/dist.make) +$(call include,$(bld_root)/meta/vc9proj.make) +$(call include,$(bld_root)/meta/vc10proj.make) +$(call include,$(bld_root)/meta/automake.make) + +$(call include,$(bld_root)/cxx/standard.make) # cxx_standard +ifdef cxx_standard +$(gen): odb_options += --std $(cxx_standard) +$(call include,$(odb_rules)) +endif + +$(call include,$(bld_root)/cxx/cxx-d.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/o-e.make) + +# Dependencies. +# +$(call import,$(src_root)/libcommon/makefile) diff --git a/common/virtual/test.hxx b/common/virtual/test.hxx new file mode 100644 index 0000000..3c0b0f1 --- /dev/null +++ b/common/virtual/test.hxx @@ -0,0 +1,139 @@ +// file : common/virtual/test.hxx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include +#include + +#include + +// Test basic virtual data member functionality. +// +#pragma db namespace table("t1_") +namespace test1 +{ + #pragma db value + struct comp + { + int i; + + #pragma db transient + std::string s; + #pragma db member(s_) virtual(std::string) access (s) + + bool operator== (const comp& v) const + { + return i == v.i && s == v.s; + } + }; + + #pragma db object transient + struct object + { + object (): p1 (0) {} + ~object () {delete p1;} + + struct {unsigned long v;} id1; + + #pragma db id + #pragma db member(id) get(id1.v) virtual(unsigned long) set(id1.v) + #pragma db member(id) auto + + int i () const {return i1;} + void i (int i) {i1 = i;} + int i1; + + comp c1; + #pragma db member(c) virtual(comp) access(c1) + + typedef std::vector strings; + strings v1; + #pragma db member(v) virtual(strings) access(v1) + + typedef object* object_ptr; + object_ptr p1; + #pragma db member(p) virtual(object_ptr) access(p1) + + bool operator== (const object& o) const + { + return id1.v == o.id1.v && + i1 == o.i1 && + c1 == o.c1 && + v1 == o.v1 && + (p1 != 0 ? o.p1 != 0 && *p1 == *o.p1 : o.p1 == 0); + } + }; + + #pragma db member(object::i) virtual(int) +} + +#pragma db member(test1::object::id) column("oid") + +// Test pragma resolution to virtual data member. +// +#pragma db namespace table("t2_") +namespace test2 +{ + struct object1; + struct object2; + + typedef object1* object1_ptr; + typedef object2* object2_ptr; + + #pragma db object + struct object2 + { + object2 (unsigned long i = 0): id (i) {} + + #pragma db id + unsigned long id; + + #pragma db inverse(o) + object1_ptr o1; + }; + + #pragma db object + struct object1 + { + object1 (unsigned long i = 0): id (i), o2 (0) {} + ~object1 () {delete o2;} + + #pragma db id + unsigned long id; + + #pragma db transient + object2_ptr o2; + #pragma db member(o) virtual(object2_ptr) access(o2) + + #pragma db transient + unsigned long n1; + #pragma db member(n) virtual(unsigned long) access(n1) + #pragma db index member(n) + }; + + #pragma db view object(object1) object(object2) + struct view1 + { + #pragma db column(object1::n) + unsigned long i; + }; + + #pragma db view object(object1 = o1) object(object2 = o2: o1::n == o2::id) + struct view2 + { + #pragma db column(o1::n) + unsigned long i; + }; + + #pragma db view object(object1: object1::n != 0) + struct view3 + { + #pragma db column(test2::object1::n) + unsigned long i; + }; +} + +#endif // TEST_HXX diff --git a/common/virtual/test.std b/common/virtual/test.std new file mode 100644 index 0000000..e69de29 -- cgit v1.1