aboutsummaryrefslogtreecommitdiff
path: root/brep/database.cxx
blob: 9177b55f3750506e650091adeca13cc4a7d28b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// file      : brep/database.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <brep/database>

#include <odb/pgsql/database.hxx>

namespace brep
{
  shared_ptr<odb::database>
  shared_database (const options::db& o)
  {
    using odb::pgsql::database;
    static weak_ptr<database> 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<database> 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 runtime_error ("shared database options mismatch");

      return d;
    }
    else
    {
      d = make_shared<database> (o.db_user (),
                                 o.db_password (),
                                 o.db_name (),
                                 o.db_host (),
                                 o.db_port ());
      db = d;
      return d;
    }
  }
}