From eef022894e7d6f1f371da432722a733b98fd8709 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 4 Jun 2010 16:36:21 +0200 Subject: Initial implementation --- odb/tracer/database.cxx | 40 +++++++++++++++++++++++++++++ odb/tracer/database.hxx | 31 ++++++++++++++++++++++ odb/tracer/makefile | 57 +++++++++++++++++++++++++++++++++++++++++ odb/tracer/transaction-impl.cxx | 53 ++++++++++++++++++++++++++++++++++++++ odb/tracer/transaction-impl.hxx | 44 +++++++++++++++++++++++++++++++ odb/tracer/transaction.cxx | 26 +++++++++++++++++++ odb/tracer/transaction.hxx | 46 +++++++++++++++++++++++++++++++++ odb/tracer/transaction.ixx | 35 +++++++++++++++++++++++++ 8 files changed, 332 insertions(+) create mode 100644 odb/tracer/database.cxx create mode 100644 odb/tracer/database.hxx create mode 100644 odb/tracer/makefile create mode 100644 odb/tracer/transaction-impl.cxx create mode 100644 odb/tracer/transaction-impl.hxx create mode 100644 odb/tracer/transaction.cxx create mode 100644 odb/tracer/transaction.hxx create mode 100644 odb/tracer/transaction.ixx (limited to 'odb') diff --git a/odb/tracer/database.cxx b/odb/tracer/database.cxx new file mode 100644 index 0000000..5d59c16 --- /dev/null +++ b/odb/tracer/database.cxx @@ -0,0 +1,40 @@ +// file : odb/tracer/database.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include +#include +#include + +namespace odb +{ + namespace tracer + { + database:: + ~database () + { + } + + transaction_impl* database:: + begin_transaction () + { + if (odb::transaction::has_current ()) + throw already_in_transaction (); + + if (session::has_current ()) + return new transaction_impl (*this, session::current ()); + else + return new transaction_impl (*this); + } + + transaction_impl* database:: + begin_transaction (session& s) + { + if (odb::transaction::has_current ()) + throw already_in_transaction (); + + return new transaction_impl (*this, s); + } + } +} diff --git a/odb/tracer/database.hxx b/odb/tracer/database.hxx new file mode 100644 index 0000000..983f866 --- /dev/null +++ b/odb/tracer/database.hxx @@ -0,0 +1,31 @@ +// file : odb/tracer/database.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TRACER_DATABASE_HXX +#define ODB_TRACER_DATABASE_HXX + +#include +#include + +namespace odb +{ + namespace tracer + { + class database: public odb::database + { + public: + virtual + ~database (); + + virtual transaction_impl* + begin_transaction (); + + virtual transaction_impl* + begin_transaction (session&); + }; + } +} + +#endif // ODB_TRACER_DATABASE_HXX diff --git a/odb/tracer/makefile b/odb/tracer/makefile new file mode 100644 index 0000000..aacde61 --- /dev/null +++ b/odb/tracer/makefile @@ -0,0 +1,57 @@ +# file : odb/tracer/makefile +# author : Boris Kolpackov +# copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +# license : GNU GPL v2; see accompanying LICENSE file + +include $(dir $(lastword $(MAKEFILE_LIST)))../../build/bootstrap.make + +cxx_tun := database.cxx transaction.cxx transaction-impl.cxx +cxx_obj := $(addprefix $(out_base)/,$(cxx_tun:.cxx=.o)) +cxx_od := $(cxx_obj:.o=.o.d) + +odb_tracer.l := $(out_base)/odb-tracer.l +odb_tracer.l.cpp-options := $(out_base)/odb-tracer.l.cpp-options + +default := $(out_base)/ +install := $(out_base)/.install +clean := $(out_base)/.clean + +# Import. +# +$(call import,\ + $(scf_root)/import/libodb/stub.make,\ + l: odb.l,cpp-options: odb.l.cpp-options) + +# Build. +# +$(odb_tracer.l): $(cxx_obj) $(odb.l) +$(odb_tracer.l.cpp-options): value := -I$(src_root) +$(odb_tracer.l.cpp-options): $(odb.l.cpp-options) + +$(cxx_obj) $(cxx_od): $(odb_tracer.l.cpp-options) + +$(call include-dep,$(cxx_od)) + +# Convenience alias for default target. +# +$(out_base)/: $(odb_tracer.l) + +# Install. +# +$(install): $(odb_tracer.l) + $(call install-lib,$<,$(install_lib_dir)/$(ld_lib_prefix)odb-tracer$(ld_lib_suffix)) + $(call install-dir,$(src_base),$(install_inc_dir)/odb/tracer,\ +'(' -name '*.hxx' -o -name '*.ixx' -o -name '*.txx' ')') + +# Clean. +# +$(clean): $(odb_tracer.l).o.clean \ + $(odb_tracer.l.cpp-options).clean \ + $(addsuffix .cxx.clean,$(cxx_obj)) \ + $(addsuffix .cxx.clean,$(cxx_od)) + +# How to. +# +$(call include,$(bld_root)/cxx/o-l.make) +$(call include,$(bld_root)/cxx/cxx-o.make) +$(call include,$(bld_root)/cxx/cxx-d.make) diff --git a/odb/tracer/transaction-impl.cxx b/odb/tracer/transaction-impl.cxx new file mode 100644 index 0000000..248ee61 --- /dev/null +++ b/odb/tracer/transaction-impl.cxx @@ -0,0 +1,53 @@ +// file : odb/tracer/transaction-impl.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +#include +#include + +using std::cout; +using std::endl; + +namespace odb +{ + namespace tracer + { + transaction_impl:: + transaction_impl (database_type& db) + : odb::transaction_impl (db), finilized_ (false) + { + cout << "begin transaction" << endl; + } + + transaction_impl:: + transaction_impl (database_type& db, session_type& s) + : odb::transaction_impl (db, s), finilized_ (false) + { + cout << "begin transaction" << endl; + } + + transaction_impl:: + ~transaction_impl () + { + if (!finilized_) + cout << "end transaction without commit/rollback" << endl; + } + + void transaction_impl:: + commit () + { + cout << "commit transaction" << endl; + finilized_ = true; + } + + void transaction_impl:: + rollback () + { + cout << "rollback transaction" << endl; + finilized_ = true; + } + } +} diff --git a/odb/tracer/transaction-impl.hxx b/odb/tracer/transaction-impl.hxx new file mode 100644 index 0000000..06fe849 --- /dev/null +++ b/odb/tracer/transaction-impl.hxx @@ -0,0 +1,44 @@ +// file : odb/tracer/transaction-impl.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TRACER_TRANSACTION_IMPL_HXX +#define ODB_TRACER_TRANSACTION_IMPL_HXX + +#include + +namespace odb +{ + namespace tracer + { + class database; + class transaction; + + class transaction_impl: public odb::transaction_impl + { + protected: + friend class database; + friend class transaction; + + typedef tracer::database database_type; + + transaction_impl (database_type&); + transaction_impl (database_type&, session_type&); + + virtual + ~transaction_impl (); + + virtual void + commit (); + + virtual void + rollback (); + + private: + bool finilized_; + }; + } +} + +#endif // ODB_TRACER_TRANSACTION_IMPL_HXX diff --git a/odb/tracer/transaction.cxx b/odb/tracer/transaction.cxx new file mode 100644 index 0000000..46309de --- /dev/null +++ b/odb/tracer/transaction.cxx @@ -0,0 +1,26 @@ +// file : odb/tracer/transaction.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace tracer + { + transaction& transaction:: + current () + { + // While the impl type can be of the concrete type, the transaction + // object can be created as either odb:: or odb::tracer:: type. To + // work around that we are going to hard-cast one two the other + // relying on the fact that they have the same representation and + // no virtual functions. The former is checked in the tests. + // + odb::transaction& b (odb::transaction::current ()); + dynamic_cast (b.implementation ()); + return reinterpret_cast (b); + } + } +} diff --git a/odb/tracer/transaction.hxx b/odb/tracer/transaction.hxx new file mode 100644 index 0000000..2bcfa07 --- /dev/null +++ b/odb/tracer/transaction.hxx @@ -0,0 +1,46 @@ +// file : odb/tracer/transaction.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_TRACER_TRANSACTION_HXX +#define ODB_TRACER_TRANSACTION_HXX + +#include + +namespace odb +{ + namespace tracer + { + class database; + class transaction_impl; + + class transaction: public odb::transaction + { + public: + typedef odb::database database_type; + + explicit + transaction (transaction_impl*); + + // Return the database this transaction is on. + // + database_type& + database (); + + // Return current transaction or throw if there is no transaction + // in effect. + // + static transaction& + current (); + + public: + transaction_impl& + implementation (); + }; + } +} + +#include + +#endif // ODB_TRACER_TRANSACTION_HXX diff --git a/odb/tracer/transaction.ixx b/odb/tracer/transaction.ixx new file mode 100644 index 0000000..35a2d78 --- /dev/null +++ b/odb/tracer/transaction.ixx @@ -0,0 +1,35 @@ +// file : odb/tracer/transaction.ixx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include +#include + +namespace odb +{ + namespace tracer + { + inline transaction:: + transaction (transaction_impl* impl) + : odb::transaction (impl) + { + } + + inline transaction::database_type& transaction:: + database () + { + return static_cast (odb::transaction::database ()); + } + + inline transaction_impl& transaction:: + implementation () + { + // We can use static_cast here since we have an instance of + // tracer::transaction. + // + return static_cast ( + odb::transaction::implementation ()); + } + } +} -- cgit v1.1