From 19a3dfd0bcb583e246f83a9c01f9218a8b41fd3c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 16 Aug 2012 12:23:25 +0200 Subject: Add support for automatically discovering accessor/modifier functions New options: --{accessor,modifier}-regex, --{accessor,modifier}-regex-trace. --- common/access/driver.cxx | 34 +++++++++++++- common/access/makefile | 3 +- common/access/test.hxx | 112 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 142 insertions(+), 7 deletions(-) (limited to 'common') diff --git a/common/access/driver.cxx b/common/access/driver.cxx index 3e3c96d..79c63bf 100644 --- a/common/access/driver.cxx +++ b/common/access/driver.cxx @@ -37,9 +37,10 @@ main (int argc, char* argv[]) o.i2 (223); o.i3 () = 323; o.i4 () = 423; - o.set_i5 (423); + o.set_i5 (523); o.s1 ("1bc"); memcpy (o.b1 (), "123456789012345", 16); + o.b2 ("123456789012345"); { transaction t (db->begin ()); @@ -219,6 +220,37 @@ main (int argc, char* argv[]) assert (o3.version_ == 2); } } + + // Test basic accessor/modifier functionality. + // + { + using namespace test7; + + object o (1); + o.i1 () = 123; + o.set_i2 (223); + o.setI3 (323); + o.seti4 (423); + o.i5 () = 523; + o.i6 () = 623; + o.SetI7 (723); + memcpy (o.b1 (), "123456789012345", 16); + o.b2 ("123456789012345"); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr p (db->load (o.id_)); + t.commit (); + + assert (o == *p); + } + } } catch (const odb::exception& e) { diff --git a/common/access/makefile b/common/access/makefile index 674128d..3cf2953 100644 --- a/common/access/makefile +++ b/common/access/makefile @@ -35,7 +35,8 @@ gen := $(addprefix $(out_base)/,$(genf)) $(gen): $(odb) $(gen): odb := $(odb) $(gen) $(dist): export odb_options += --database $(db_id) --generate-schema \ ---table-prefix access_ +--table-prefix access_ --accessor-regex "/(.+)/Get\u\1/" \ +--modifier-regex "/(.+)/Set\u\1/" $(gen): cpp_options := -I$(src_base) $(gen): $(common.l.cpp-options) diff --git a/common/access/test.hxx b/common/access/test.hxx index 7964edb..8e1c209 100644 --- a/common/access/test.hxx +++ b/common/access/test.hxx @@ -63,21 +63,21 @@ namespace test1 #pragma db access(i2) int i2_; - // Prefer reference accessor to modifier. + // Prefer reference modifier. // public: int i3 () const {return i3_;} int& i3 () {return i3_;} + void i3 (int i3); private: - void i3 (int i3) {i3_ = i3;} #pragma db access(i3) int i3_; - // Prefer reference accessor to modifier (reverse function order). + // Prefer reference modifier (reverse function order). // - void i4 (int i4) {i4_ = i4;} public: int i4 () const {return i4_;} + void i4 (int i4); int& i4 () {return i4_;} private: #pragma db access(i4) @@ -110,8 +110,11 @@ namespace test1 public: const char* s1 () const {return s1_.c_str ();} void s1 (const char* s1) {s1_ = s1;} + //std::string s1 () const {return s1_;} + //void s1 (std::string s1) {s1_ = s1;} private: #pragma db get(s1) set(s1((?).c_str ())) + //#pragma db access(s1) std::string s1_; // Array member via ref. @@ -127,7 +130,7 @@ namespace test1 // public: const char* b2 () const {return b2_;} - void b2 (char* b2) {std::memcpy (b2_, b2, sizeof (b2_));} + void b2 (const char* b2) {std::memcpy (b2_, b2, sizeof (b2_));} private: #pragma db type(BINARY16_TYPE) access(b2) char b2_[16]; @@ -498,4 +501,103 @@ namespace test6 }; } +// Test automatic discovery of accessor/modifier functions. +// +#pragma db namespace table("t7_") +namespace test7 +{ + #pragma db object + struct object + { + object () {} + object (unsigned long id): id_ (id) {} + + #pragma db id + unsigned long id_; + + public: + int i1 () const {return i1_;} + int& i1 () {return i1_;} + private: + int i1_; + + public: + const int& get_i2 () const {return i2_;} + void set_i2 (int i2) {i2_ = i2;} + private: + int i2_; + + public: + const int& getI3 () const {return i3_;} + void setI3 (const int& i3) {i3_ = i3;} + private: + int i3_; + + public: + int geti4 () const {return i4;} + int seti4 (int v) {int r (i4); i4 = v; return r;} + private: + int i4; + + // Prefer reference modifier. + // + public: + int i5 () const {return i5_;} + int& i5 () {return i5_;} + void i5 (int i5); + private: + int i5_; + + // Prefer reference modifier (reverse function order). + // + public: + int i6 () const {return i6_;} + void i6 (int i6); + int& i6 () {return i6_;} + private: + int i6_; + + // Custom accessor/modifier regex. + // + public: + int GetI7 () const {return i7_;} + void SetI7 (int i7) {i7_ = i7;} + private: + int i7_; + + // Array member via ref. + // + public: + const char* b1 () const {return b1_;} + char* b1 () {return b1_;} + private: + #pragma db type(BINARY16_TYPE) + char b1_[16]; + + // Array member via modifier. + // + public: + const char* b2 () const {return b2_;} + void b2 (const char* b2) {std::memcpy (b2_, b2, sizeof (b2_));} + private: + #pragma db type(BINARY16_TYPE) + char b2_[16]; + + public: + bool operator== (const object& o) const + { + return id_ == o.id_ && + i1_ == o.i1_ && + i2_ == o.i2_ && + i3_ == o.i3_ && + i4 == o.i4 && + i5_ == o.i5_ && + i6_ == o.i6_ && + i7_ == o.i7_ && + std::memcmp (b1_, o.b1_, sizeof (b1_)) == 0 && + std::memcmp (b2_, o.b2_, sizeof (b2_)) == 0; + } + }; +} + #endif // TEST_HXX -- cgit v1.1