diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-04-18 18:50:04 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-04-18 18:50:04 +0200 |
commit | e305857bd0bd406ab91d31aaea0c1491d6e36b50 (patch) | |
tree | 6110c686f0baa08ddc970d18903d91de595aa475 | |
parent | 2f0038a2aed44ce04200c09229921ae21861ccaa (diff) |
Implement automatic mapping for C++ enums
-rw-r--r-- | common/enum/driver.cxx | 69 | ||||
-rw-r--r-- | common/enum/makefile | 109 | ||||
-rw-r--r-- | common/enum/test.hxx | 33 | ||||
-rw-r--r-- | common/enum/test.std | 0 | ||||
-rw-r--r-- | common/makefile | 1 | ||||
-rw-r--r-- | mysql/types/driver.cxx | 3 | ||||
-rw-r--r-- | mysql/types/test.hxx | 13 |
7 files changed, 224 insertions, 4 deletions
diff --git a/common/enum/driver.cxx b/common/enum/driver.cxx new file mode 100644 index 0000000..833eebb --- /dev/null +++ b/common/enum/driver.cxx @@ -0,0 +1,69 @@ +// file : common/enum/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 automatic C++ enum mapping. +// + +#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 + { + typedef odb::query<object> query; + typedef odb::result<object> result; + + auto_ptr<database> db (create_database (argc, argv)); + + object o; + o.color_ = green; + o.taste_ = object::sweet; + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> o1 (db->load<object> (o.id_)); + t.commit (); + + assert (o == *o1); + } + + { + transaction t (db->begin ()); + + result r1 (db->query<object> (query::color == blue)); + result r2 (db->query<object> (query::taste == object::sweet)); + + assert (r1.empty ()); + assert (!r2.empty ()); + + t.commit (); + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/common/enum/makefile b/common/enum/makefile new file mode 100644 index 0000000..a6b16e2 --- /dev/null +++ b/common/enum/makefile @@ -0,0 +1,109 @@ +# file : common/enum/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 \ +--generate-query +$(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/enum/test.hxx b/common/enum/test.hxx new file mode 100644 index 0000000..bb264d5 --- /dev/null +++ b/common/enum/test.hxx @@ -0,0 +1,33 @@ +// file : common/enum/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 <odb/core.hxx> + +enum color {red, green, blue}; + +#pragma db object +struct object +{ + #pragma db id auto + unsigned long id_; + + color color_; + enum taste {bitter, sweet, sour}; + taste taste_; +}; + +inline bool +operator == (const object& x, const object& y) +{ + return + x.id_ == y.id_ && + x.color_ == y.color_ && + x.taste_ == y.taste_; +} + +#endif // TEST_HXX diff --git a/common/enum/test.std b/common/enum/test.std new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/common/enum/test.std diff --git a/common/makefile b/common/makefile index c3f02bf..317c49e 100644 --- a/common/makefile +++ b/common/makefile @@ -11,6 +11,7 @@ composite \ const \ container \ ctor \ +enum \ inverse \ lazy-ptr \ lifecycle \ diff --git a/mysql/types/driver.cxx b/mysql/types/driver.cxx index b675655..8a51dea 100644 --- a/mysql/types/driver.cxx +++ b/mysql/types/driver.cxx @@ -77,7 +77,8 @@ main (int argc, char* argv[]) o.bit_.c = 0; o.bit_.d = 1; - o.enum_ = "green"; + o.enum_ = green; + o.enum_str_ = "green"; o.set_.insert ("green"); o.set_.insert ("red"); o.set_.insert ("blue"); diff --git a/mysql/types/test.hxx b/mysql/types/test.hxx index 207d8b3..3039a88 100644 --- a/mysql/types/test.hxx +++ b/mysql/types/test.hxx @@ -82,6 +82,8 @@ operator== (bitfield x, bitfield y) typedef std::set<std::string> set; typedef std::auto_ptr<std::string> string_ptr; +enum color {red, green, blue}; + #pragma db object struct object { @@ -203,10 +205,14 @@ struct object // #pragma db type ("BIT(4) NOT NULL") - assigned by #pragma db value bitfield bit_; - #pragma db type ("ENUM('red', 'green', 'blue') NOT NULL") - std::string enum_; + // Test ENUM representations (integer and string). + // + color enum_; + + #pragma db type ("ENUM ('red', 'green', 'blue') NOT NULL") + std::string enum_str_; - #pragma db type ("SET('red', 'green', 'blue') NOT NULL") + #pragma db type ("SET ('red', 'green', 'blue') NOT NULL") set set_; // Test NULL value. @@ -252,6 +258,7 @@ struct object longblob_ == y.longblob_ && bit_ == y.bit_ && enum_ == y.enum_ && + enum_str_ == y.enum_str_ && set_ == y.set_ && ((null_.get () == 0 && y.null_.get () == 0) || *null_ == *y.null_); } |