aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-03-10 11:07:43 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-03-21 15:40:00 +0200
commitc31fc72be48b037fff1cc2b46a812d20d4b601c4 (patch)
tree574161b9c1db69b88ca1e439e2ca8a44f405dc27
parenta98ab50a31b57d609588ad13c39e8819c3cd0f9e (diff)
Cache current context in static variable
-rw-r--r--odb/context.hxx1
-rw-r--r--odb/relational/context.cxx13
-rw-r--r--odb/relational/context.hxx23
-rw-r--r--odb/relational/mysql/context.cxx13
-rw-r--r--odb/relational/mysql/context.hxx27
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 <cassert>
+
#include <odb/relational/context.hxx>
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<context&> (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 <cassert>
#include <sstream>
#include <odb/sql-token.hxx>
@@ -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<data*> (root_context::data_.get ())),
data_ (static_cast<data*> (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<context&> (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_;
};
}
}