From c715379c625935bd4b28bebb35f34721342cc7f3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 22 Dec 2015 19:29:04 +0200 Subject: Source file names fixup --- brep/database.cxx | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 brep/database.cxx (limited to 'brep/database.cxx') diff --git a/brep/database.cxx b/brep/database.cxx new file mode 100644 index 0000000..4b7494e --- /dev/null +++ b/brep/database.cxx @@ -0,0 +1,48 @@ +// file : brep/database.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include // runtime_error + +#include + +#include +#include + +namespace brep +{ + shared_ptr + shared_database (const options::db& o) + { + using odb::pgsql::database; + static weak_ptr db; + + // In C++11, function-static variable initialization is guaranteed to be + // thread-safe, thought this doesn't seem to be enough in our case + // (because we are re-initializing the weak pointer). + // + if (shared_ptr d = db.lock ()) + { + if (o.db_user () != d->user () || + o.db_password () != d->password () || + o.db_name () != d->db () || + o.db_host () != d->host () || + o.db_port () != d->port ()) + throw std::runtime_error ("shared database options mismatch"); + + return d; + } + else + { + d = make_shared (o.db_user (), + o.db_password (), + o.db_name (), + o.db_host (), + o.db_port ()); + db = d; + return d; + } + } +} -- cgit v1.1