diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-19 10:47:41 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-10-21 20:05:46 +0200 |
commit | bceb06251cdb572850b2e6e4d15cfb2ac32da417 (patch) | |
tree | 2e6af635da493c51d5f19b81e2a5506db90353ca /common | |
parent | 2d10422daab4b63307f3c1d9a6c08ee2c1b7c72e (diff) |
Add support for const data members
Const data members are automatically treated as readonly. New test:
const-member.
Diffstat (limited to 'common')
-rw-r--r-- | common/const-member/driver.cxx | 119 | ||||
-rw-r--r-- | common/const-member/makefile | 109 | ||||
-rw-r--r-- | common/const-member/test.hxx | 111 | ||||
-rw-r--r-- | common/const-member/test.std | 0 | ||||
-rw-r--r-- | common/makefile | 2 | ||||
-rw-r--r-- | common/readonly/driver.cxx | 67 | ||||
-rw-r--r-- | common/readonly/test.hxx | 56 |
7 files changed, 451 insertions, 13 deletions
diff --git a/common/const-member/driver.cxx b/common/const-member/driver.cxx new file mode 100644 index 0000000..8cd815e --- /dev/null +++ b/common/const-member/driver.cxx @@ -0,0 +1,119 @@ +// file : common/const-member/driver.cxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +// Test const data members. The readonly test tests that const +// members are automatically treated as read-only. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> + +#include <common/common.hxx> + +#include "test.hxx" +#include "test-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv)); + + // Const ids. + // + { + const_id o (1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + db->load<const_id> (1, o); + t.commit (); + assert (o.id == 1); + } + } + + { + { + const_auto_id o; + transaction t (db->begin ()); + db->persist (o); + t.commit (); + assert (o.id == 1); + } + + { + transaction t (db->begin ()); + auto_ptr<const_auto_id> o (db->load<const_auto_id> (1)); + t.commit (); + assert (o->id == 1); + } + } + + // Container. + // + { + container o (1, 1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<container> o (db->load<container> (1)); + t.commit (); + + assert (o->ccom.vec.size () == 1 && o->ccom.vec[0] == 1 && + o->ccom.cvec.size () == 1 && o->ccom.cvec[0] == 1 && + o->cvec.size () == 1 && o->cvec[0] == 1); + } + } + + // Wrapper. + // + { + wrapper o (1, "abc", 1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<wrapper> o (db->load<wrapper> (1)); + t.commit (); + + assert (*o->str == "abc" && + o->com->str == "abc" && o->com->num == 1 && + o->com->vec.size () == 1 && o->com->vec[0] == 1 && + o->vec->size () == 1 && (*o->vec)[0] == 1); + } + } + + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/const-member/makefile b/common/const-member/makefile new file mode 100644 index 0000000..33d135b --- /dev/null +++ b/common/const-member/makefile @@ -0,0 +1,109 @@ +# file : common/const-member/makefile +# author : Boris Kolpackov <boris@codesynthesis.com> +# copyright : Copyright (c) 2009-2011 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 \ +--table-prefix common_const_member_ +$(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,$(odb_rules)) +$(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/const-member/test.hxx b/common/const-member/test.hxx new file mode 100644 index 0000000..c987cf8 --- /dev/null +++ b/common/const-member/test.hxx @@ -0,0 +1,111 @@ +// file : common/const-member/test.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST_HXX +#define TEST_HXX + +#include <vector> +#include <string> +#include <memory> // std::auto_ptr + +#include <odb/core.hxx> + +// Const ids. +// +#pragma db object +struct const_id +{ + const_id (unsigned long i): id (i) {} + const_id (): id (0) {} + + #pragma db id + const unsigned long id; +}; + +#pragma db object +struct const_auto_id +{ + const_auto_id (): id (0) {} + + #pragma db id auto + const unsigned long id; +}; + +// Container. +// +#pragma db value +struct container_value +{ + container_value (unsigned long x) + { + vec.push_back (x); + const_cast<std::vector<unsigned long>&> (cvec).push_back (x); + } + + container_value () {} + + std::vector<unsigned long> vec; + const std::vector<unsigned long> cvec; +}; + +#pragma db object +struct container +{ + container (unsigned long i, unsigned long x) + : id (i), ccom (x) + { + const_cast<std::vector<unsigned long>&> (cvec).push_back (x); + } + + container () {} + + #pragma db id + unsigned long id; + + const container_value ccom; + const std::vector<unsigned long> cvec; +}; + +// Wrapper. +// +#pragma db value +struct wrapped_value +{ + wrapped_value (const std::string& s, unsigned long n) + : str (s), num (n) + { + vec.push_back (n); + } + + wrapped_value () {} + + const std::string str; + unsigned long num; + std::vector<unsigned long> vec; +}; + +#pragma db object +struct wrapper +{ + wrapper (unsigned long i, const std::string& s, unsigned long n) + : id (i), + str (new std::string (s)), + com (new wrapped_value (s, n)), + vec (new std::vector<unsigned long>) + { + const_cast<std::vector<unsigned long>&> (*vec).push_back (n); + } + + wrapper () {} + + #pragma db id + unsigned long id; + + const std::auto_ptr<const std::string> str; + const std::auto_ptr<const wrapped_value> com; + const std::auto_ptr< const std::vector<unsigned long> > vec; +}; + +#endif // TEST_HXX diff --git a/common/const-member/test.std b/common/const-member/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/common/const-member/test.std diff --git a/common/makefile b/common/makefile index 547e764..e5065cd 100644 --- a/common/makefile +++ b/common/makefile @@ -10,6 +10,7 @@ auto \ callback \ composite \ const \ +const-member \ container \ ctor \ default \ @@ -35,6 +36,7 @@ thread_tests := threads ifeq ($(db_id),oracle) tests := \ auto \ +const-member \ container \ lazy-ptr \ lifecycle \ diff --git a/common/readonly/driver.cxx b/common/readonly/driver.cxx index 2ad40a1..0432c69 100644 --- a/common/readonly/driver.cxx +++ b/common/readonly/driver.cxx @@ -3,7 +3,9 @@ // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file -// Test readonly members/objects. +// Test readonly members/objects. Also test that const members are +// treated as readonly. For other const member tests, see the const- +// member test. // #include <memory> // std::auto_ptr @@ -40,7 +42,7 @@ main (int argc, char* argv[]) } o.ro++; - // const_cast<unsigned long&> (o.co)++; + const_cast<unsigned long&> (o.co)++; o.rw++; { @@ -55,7 +57,7 @@ main (int argc, char* argv[]) t.commit (); } - assert (o.ro == 1 && /*o.co == 1 &&*/ o.rw == 2); + assert (o.ro == 1 && o.co == 1 && o.rw == 2); } // Pointer. @@ -74,6 +76,7 @@ main (int argc, char* argv[]) delete p.ro; p.ro = p1.release (); + const_cast<pointer*&> (p.co) = p.ro; p.rw = p.ro; { @@ -87,7 +90,7 @@ main (int argc, char* argv[]) auto_ptr<pointer> p (db->load<pointer> (1)); t.commit (); - assert (p->ro->id == 2 && p->rw->id == 3); + assert (p->ro->id == 2 && p->co->id == 2 && p->rw->id == 3); } } @@ -104,10 +107,18 @@ main (int argc, char* argv[]) o.ro.v++; o.ro.ro++; + const_cast<unsigned long&> (o.ro.co)++; o.ro.rw++; + value& co (const_cast<value&> (o.co)); + co.v++; + co.ro++; + const_cast<unsigned long&> (co.co)++; + co.rw++; + o.rw.v++; o.rw.ro++; + const_cast<unsigned long&> (o.rw.co)++; o.rw.rw++; o.v.v++; @@ -126,10 +137,17 @@ main (int argc, char* argv[]) assert (o.ro.v == 1 && o.ro.ro == 1 && + o.ro.co == 1 && o.ro.rw == 1 && + o.co.v == 1 && + o.co.ro == 1 && + o.co.co == 1 && + o.co.rw == 1 && + o.rw.v == 1 && o.rw.ro == 1 && + o.rw.co == 1 && o.rw.rw == 2 && o.v.v == 1); @@ -138,11 +156,17 @@ main (int argc, char* argv[]) // Container. // { + typedef vector<unsigned long> ulongs; + container o (1); o.ro.push_back (1); o.ro.push_back (2); + ulongs& co (const_cast<ulongs&> (o.co)); + co.push_back (1); + co.push_back (2); + o.rw.push_back (1); o.rw.push_back (2); @@ -155,6 +179,9 @@ main (int argc, char* argv[]) o.ro[0]++; o.ro.pop_back (); + co[0]++; + co.pop_back (); + o.rw[0]++; o.rw.pop_back (); @@ -171,6 +198,7 @@ main (int argc, char* argv[]) } assert (o.ro.size () == 2 && o.ro[0] == 1 && o.ro[1] == 2 && + o.co.size () == 2 && o.co[0] == 1 && o.co[1] == 2 && o.rw.size () == 1 && o.rw[0] == 2); } @@ -221,6 +249,37 @@ main (int argc, char* argv[]) assert (rw_o.sv == 1 && rw_o.rw_sv == 2); } + + // Readonly object. + // + { + wrapper o (1, 1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + *o.pl = 2; + *o.cpl = 2; + o.pcl.reset (new unsigned long (2)); + const_cast<unsigned long&> (*o.cpcl) = 2; + + { + transaction t (db->begin ()); + db->update (o); + t.commit (); + } + + { + transaction t (db->begin ()); + db->load<wrapper> (1, o); + t.commit (); + } + + assert (*o.pl == 2 && *o.cpl == 2 && *o.pcl == 2 && *o.cpcl == 1); + } } catch (const odb::exception& e) { diff --git a/common/readonly/test.hxx b/common/readonly/test.hxx index a4cc55b..3a0095f 100644 --- a/common/readonly/test.hxx +++ b/common/readonly/test.hxx @@ -7,6 +7,7 @@ #define TEST_HXX #include <vector> +#include <memory> // std::auto_ptr #include <odb/core.hxx> @@ -16,11 +17,11 @@ struct simple { simple (unsigned long i, unsigned long x) - : id (i), ro (x), /*co (x),*/ rw (x) + : id (i), ro (x), co (x), rw (x) { } - simple ()/*: co (0)*/ {} + simple (): co (0) {} #pragma db id unsigned long id; @@ -28,7 +29,7 @@ struct simple #pragma db readonly unsigned long ro; - //const unsigned long co; + const unsigned long co; unsigned long rw; }; @@ -38,14 +39,17 @@ struct simple #pragma db object struct pointer { - pointer (unsigned long i, pointer* p = 0): id (i), ro (p), rw (p) {} - pointer (): ro (0), rw (0) {} + pointer (unsigned long i, pointer* p = 0): id (i), ro (p), co (p), rw (p) {} + pointer (): ro (0), co (0), rw (0) {} ~pointer () { delete ro; - if (ro != rw) + if (co != ro) + delete co; + + if (rw != ro && rw != co) delete rw; } @@ -55,6 +59,8 @@ struct pointer #pragma db readonly pointer* ro; + pointer* const co; + pointer* rw; }; @@ -73,12 +79,14 @@ struct ro_value #pragma db value struct value: ro_value { - value () {} - value (unsigned long x): ro_value (x), ro (x), rw (x) {} + value (): co (0) {} + value (unsigned long x): ro_value (x), ro (x), co (x), rw (x) {} #pragma db readonly unsigned long ro; + const unsigned long co; + unsigned long rw; }; @@ -86,7 +94,7 @@ struct value: ro_value struct composite { composite (unsigned long i, unsigned long x) - : id (i), ro (x), rw (x), v (x) + : id (i), ro (x), co (x), rw (x), v (x) { } @@ -98,6 +106,8 @@ struct composite #pragma db readonly value ro; + const value co; + value rw; ro_value v; }; @@ -116,6 +126,8 @@ struct container #pragma db readonly std::vector<unsigned long> ro; + const std::vector<unsigned long> co; + std::vector<unsigned long> rw; }; @@ -160,4 +172,30 @@ struct rw_object: ro_object unsigned long rw_sv; }; +// Readonly wrappers. Here we make sure that only const wrappers with +// const wrapped types are automatically treated as readonly. +// +#pragma db object +struct wrapper +{ + wrapper (unsigned long i, unsigned long x) + : id (i), + pl (new unsigned long (x)), + cpl (new unsigned long (x)), + pcl (new unsigned long (x)), + cpcl (new unsigned long (x)) + { + } + + wrapper () {} + + #pragma db id + unsigned long id; + + std::auto_ptr<unsigned long> pl; + const std::auto_ptr<unsigned long> cpl; + std::auto_ptr<const unsigned long> pcl; + const std::auto_ptr<const unsigned long> cpcl; +}; + #endif // TEST_HXX |