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/const-member | |
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/const-member')
-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 |
4 files changed, 339 insertions, 0 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 |