From 163bb9d59de08b02074395fbb2a9fde5f8cc14d9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 29 Mar 2011 17:53:00 +0200 Subject: Add single_connection_factory --- odb/sqlite/connection-factory.cxx | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'odb/sqlite/connection-factory.cxx') diff --git a/odb/sqlite/connection-factory.cxx b/odb/sqlite/connection-factory.cxx index 11184bc..fc9fefc 100644 --- a/odb/sqlite/connection-factory.cxx +++ b/odb/sqlite/connection-factory.cxx @@ -26,6 +26,67 @@ namespace odb } // + // single_connection_factory + // + + single_connection_factory:: + ~single_connection_factory () + { + // If the connection is currently in use, wait for it to return to + // the factory. + // + lock l (mutex_); + } + + shared_ptr single_connection_factory:: + connect () + { + mutex_.lock (); + connection_->factory_ = this; + shared_ptr r (connection_); + connection_.reset (); + return r; + } + + void single_connection_factory:: + database (database_type& db) + { + db_ = &db; + connection_.reset (new (shared) single_connection (*db_, 0, 0)); + } + + bool single_connection_factory:: + release (single_connection* c) + { + c->factory_ = 0; + connection_.reset (inc_ref (c)); + mutex_.unlock (); + return false; + } + + // + // single_connection_factory::single_connection + // + + single_connection_factory::single_connection:: + single_connection (database_type& db, + int extra_flags, + single_connection_factory* factory) + : connection (db, extra_flags), factory_ (factory) + { + callback_.arg = this; + callback_.zero_counter = &zero_counter; + shared_base::callback_ = &callback_; + } + + bool single_connection_factory::single_connection:: + zero_counter (void* arg) + { + single_connection* c (static_cast (arg)); + return c->factory_ ? c->factory_->release (c) : true; + } + + // // new_connection_factory // -- cgit v1.1