// file : brep/shared-database.cxx -*- C++ -*- // copyright : Copyright (c) 2014-2015 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file #include #include // weak_ptr, shared_ptr, make_shared() #include // runtime_error #include using namespace std; namespace brep { shared_ptr shared_database (const string& h, unsigned int p) { 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 (h != d->host () || p != d->port ()) throw runtime_error ("shared database host/port mismatch"); return d; } else { d = make_shared ("", "", "brep", h, p); db = d; return d; } } }