From c31fc72be48b037fff1cc2b46a812d20d4b601c4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 10 Mar 2011 11:07:43 +0200 Subject: Cache current context in static variable --- odb/context.hxx | 1 + odb/relational/context.cxx | 13 +++++++++++++ odb/relational/context.hxx | 23 ++++++++++++++--------- odb/relational/mysql/context.cxx | 13 +++++++++++++ odb/relational/mysql/context.hxx | 27 +++++++++++++++------------ 5 files changed, 56 insertions(+), 21 deletions(-) diff --git a/odb/context.hxx b/odb/context.hxx index 23eb457..d7bfa0d 100644 --- a/odb/context.hxx +++ b/odb/context.hxx @@ -424,6 +424,7 @@ public: semantics::unit&, options_type const&, data_ptr = data_ptr ()); + context (const context&); static context& diff --git a/odb/relational/context.cxx b/odb/relational/context.cxx index e7a4f21..f763b8d 100644 --- a/odb/relational/context.cxx +++ b/odb/relational/context.cxx @@ -3,12 +3,23 @@ // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v3; see accompanying LICENSE file +#include + #include using namespace std; namespace relational { + context* context::current_; + + context:: + ~context () + { + if (current_ == this) + current_ = 0; + } + context:: context () : data_ (current ().data_) @@ -19,6 +30,8 @@ namespace relational context (data* d) : data_ (d) { + assert (current_ == 0); + current_ = this; } bool context:: diff --git a/odb/relational/context.hxx b/odb/relational/context.hxx index fbd6059..c99b400 100644 --- a/odb/relational/context.hxx +++ b/odb/relational/context.hxx @@ -33,15 +33,6 @@ namespace relational string quote_id (string const&) const; - public: - context (); - - static context& - current () - { - return dynamic_cast (root_context::current ()); - } - protected: // The default implementation returns false. // @@ -61,12 +52,26 @@ namespace relational virtual string quote_id_impl (string const&) const; + public: + virtual + ~context (); + context (); + + static context& + current () + { + return *current_; + } + protected: struct data; typedef context base_context; context (data*); + private: + static context* current_; + protected: struct data: root_context::data { diff --git a/odb/relational/mysql/context.cxx b/odb/relational/mysql/context.cxx index 545f889..90f5766 100644 --- a/odb/relational/mysql/context.cxx +++ b/odb/relational/mysql/context.cxx @@ -3,6 +3,7 @@ // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : GNU GPL v3; see accompanying LICENSE file +#include #include #include @@ -53,12 +54,24 @@ namespace relational }; } + context* context::current_; + + context:: + ~context () + { + if (current_ == this) + current_ = 0; + } + context:: context (ostream& os, semantics::unit& u, options_type const& ops) : root_context (os, u, ops, data_ptr (new (shared) data (os))), base_context (static_cast (root_context::data_.get ())), data_ (static_cast (base_context::data_)) { + assert (current_ == 0); + current_ = this; + // Populate the C++ type to DB type map. // for (size_t i (0); i < sizeof (type_map) / sizeof (type_map_entry); ++i) diff --git a/odb/relational/mysql/context.hxx b/odb/relational/mysql/context.hxx index a54803d..159fe46 100644 --- a/odb/relational/mysql/context.hxx +++ b/odb/relational/mysql/context.hxx @@ -101,26 +101,29 @@ namespace relational string const& type, semantics::context&, column_type_flags); - private: - struct data: base_context::data - { - data (std::ostream& os): base_context::data (os) {} - }; - - private: - data* data_; public: + virtual + ~context (); + context (); + context (std::ostream&, semantics::unit&, options_type const&); + static context& current () { - return dynamic_cast (base_context::current ()); + return *current_; } - context (std::ostream&, semantics::unit&, options_type const&); + private: + static context* current_; - protected: - context (); + private: + struct data: base_context::data + { + data (std::ostream& os): base_context::data (os) {} + }; + + data* data_; }; } } -- cgit v1.1