From 644cba591ff6ec046ac4274b7c343dead847736e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 9 Nov 2015 18:14:37 +0200 Subject: Make database class move-constructible This means it can be returned by value from a function in C++11. --- odb/connection.cxx | 6 ++++-- odb/connection.hxx | 19 +++++++++++++++++-- odb/connection.ixx | 6 +++--- odb/database.cxx | 2 +- odb/database.hxx | 19 ++++++++++++++----- odb/database.ixx | 5 ++++- odb/details/unique-ptr.hxx | 16 ++++++++++++++++ 7 files changed, 59 insertions(+), 14 deletions(-) diff --git a/odb/connection.cxx b/odb/connection.cxx index 87f76d3..e3bc946 100644 --- a/odb/connection.cxx +++ b/odb/connection.cxx @@ -12,6 +12,8 @@ using namespace std; namespace odb { + // connection + // connection:: ~connection () { @@ -98,8 +100,8 @@ namespace odb { // Use a factory, if there is one. // - if (database_.call_query_factory (name, - const_cast (*this))) + if (factory_.database ().call_query_factory ( + name, const_cast (*this))) i = prepared_map_.find (name); } diff --git a/odb/connection.hxx b/odb/connection.hxx index 20cb285..f1f8c3b 100644 --- a/odb/connection.hxx +++ b/odb/connection.hxx @@ -26,6 +26,7 @@ namespace odb { class transaction_impl; + class connection_factory; class connection; typedef details::shared_ptr connection_ptr; @@ -130,7 +131,7 @@ namespace odb recycle (); protected: - connection (database_type&); + connection (connection_factory&); template diff --git a/odb/connection.ixx b/odb/connection.ixx index 573134d..e86f8d8 100644 --- a/odb/connection.ixx +++ b/odb/connection.ixx @@ -8,8 +8,8 @@ namespace odb { inline connection:: - connection (database_type& database) - : database_ (database), + connection (connection_factory& f) + : factory_ (f), tracer_ (0), results_ (0), prepared_queries_ (0), @@ -20,7 +20,7 @@ namespace odb inline connection::database_type& connection:: database () { - return database_; + return factory_.database (); } inline unsigned long long connection:: diff --git a/odb/database.cxx b/odb/database.cxx index 2ce15b2..7ea8313 100644 --- a/odb/database.cxx +++ b/odb/database.cxx @@ -27,7 +27,7 @@ namespace odb const database::schema_version_migration_type& database:: schema_version_migration (const string& name) const { - lock l (mutex_); // Prevents concurrent loading. + lock l (*mutex_); // Prevents concurrent loading. schema_version_map::const_iterator i (schema_version_map_.find (name)); return i != schema_version_map_.end () && i->second.version != 0 diff --git a/odb/database.hxx b/odb/database.hxx index 8ee5c55..510b28b 100644 --- a/odb/database.hxx +++ b/odb/database.hxx @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -45,6 +46,18 @@ namespace odb virtual ~database (); +#ifdef ODB_CXX11 + database (database&&) = default; +#endif + + private: + database (const database&); + database& operator= (const database&); + +#ifdef ODB_CXX11 + database& operator= (const database&&); +#endif + // Object persistence API. // public: @@ -504,10 +517,6 @@ namespace odb protected: database (database_id); - private: - database (const database&); - database& operator= (const database&); - protected: virtual connection_type* connection_ () = 0; @@ -621,7 +630,7 @@ namespace odb tracer_type* tracer_; query_factory_map query_factory_map_; - mutable details::mutex mutex_; + details::unique_ptr mutex_; // Dynamic for move support. mutable schema_version_map schema_version_map_; std::string schema_version_table_; unsigned int schema_version_seq_; diff --git a/odb/database.ixx b/odb/database.ixx index 2fd54c0..1ea7949 100644 --- a/odb/database.ixx +++ b/odb/database.ixx @@ -73,7 +73,10 @@ namespace odb inline database:: database (database_id id) - : id_ (id), tracer_ (0), schema_version_seq_ (1) + : id_ (id), + tracer_ (0), + mutex_ (new details::mutex), + schema_version_seq_ (1) { } diff --git a/odb/details/unique-ptr.hxx b/odb/details/unique-ptr.hxx index 35f4d68..547dd3b 100644 --- a/odb/details/unique-ptr.hxx +++ b/odb/details/unique-ptr.hxx @@ -7,6 +7,8 @@ #include +#include + namespace odb { namespace details @@ -20,6 +22,20 @@ namespace odb explicit unique_ptr (T* p = 0): p_ (p) {} ~unique_ptr () {delete p_;} +#ifdef ODB_CXX11 + unique_ptr (unique_ptr&& p): p_ (p.p_) {p.p_ = 0;} + unique_ptr& operator= (unique_ptr&& p) + { + if (this != &p) + { + delete p_; + p_ = p.p_; + p.p_ = 0; + } + return *this; + } +#endif + private: unique_ptr (const unique_ptr&); unique_ptr& operator= (const unique_ptr&); -- cgit v1.1