[glom] ConnectionPool: Correct curious uses of std::shared_ptr<>.



commit 0b47b9ae50cb52c2698b7f56590f01901d2e9da0
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Feb 4 22:55:23 2016 +0100

    ConnectionPool: Correct curious uses of std::shared_ptr<>.

 glom/dialog_connection.cc           |    6 ++--
 glom/frame_glom.cc                  |    4 +-
 glom/glom_create_from_example.cc    |    3 +-
 glom/glom_test_connection.cc        |    6 ++--
 glom/libglom/connectionpool.cc      |   54 +++++++++++++++++------------------
 glom/libglom/connectionpool.h       |   11 ++----
 glom/libglom/db_utils.cc            |    8 ++---
 glom/libglom/test_connectionpool.cc |    6 ++--
 8 files changed, 45 insertions(+), 53 deletions(-)
---
diff --git a/glom/dialog_connection.cc b/glom/dialog_connection.cc
index e825484..9c69f7f 100644
--- a/glom/dialog_connection.cc
+++ b/glom/dialog_connection.cc
@@ -78,7 +78,7 @@ std::shared_ptr<SharedConnection> Dialog_Connection::connect_to_server_with_conn
   if(document->get_hosting_mode() == Document::HostingMode::POSTGRES_CENTRAL)
   {
     auto backend = connection_pool->get_backend();
-    auto central = dynamic_cast<ConnectionPoolBackends::PostgresCentralHosted*>(backend);
+    auto central = std::dynamic_pointer_cast<ConnectionPoolBackends::PostgresCentralHosted>(backend);
     g_assert(central);
 
     central->set_host(m_entry_host->get_text());
@@ -100,7 +100,7 @@ std::shared_ptr<SharedConnection> Dialog_Connection::connect_to_server_with_conn
   if(document->get_hosting_mode() == Document::HostingMode::POSTGRES_CENTRAL)
   {
     auto backend = connection_pool->get_backend();
-    auto central = dynamic_cast<ConnectionPoolBackends::PostgresCentralHosted*>(backend);
+    auto central = std::dynamic_pointer_cast<ConnectionPoolBackends::PostgresCentralHosted>(backend);
     g_assert(central);
 
     unconst->set_connection_port(central->get_port() );
@@ -113,7 +113,7 @@ std::shared_ptr<SharedConnection> Dialog_Connection::connect_to_server_with_conn
   else if(document->get_hosting_mode() == Document::HostingMode::POSTGRES_SELF)
   {
     auto backend = connection_pool->get_backend();
-    auto self = dynamic_cast<ConnectionPoolBackends::PostgresSelfHosted*>(backend);
+    auto self = std::dynamic_pointer_cast<ConnectionPoolBackends::PostgresSelfHosted>(backend);
     g_assert(self);
 
     unconst->set_connection_port(self->get_port() );
diff --git a/glom/frame_glom.cc b/glom/frame_glom.cc
index 245909e..3cc1b8f 100644
--- a/glom/frame_glom.cc
+++ b/glom/frame_glom.cc
@@ -2143,7 +2143,7 @@ bool Frame_Glom::connection_request_password_and_choose_new_database_name()
   if(document->get_hosting_mode() == Document::HostingMode::POSTGRES_CENTRAL)
   {
     auto backend = connection_pool->get_backend();
-    auto central = dynamic_cast<ConnectionPoolBackends::PostgresCentralHosted*>(backend);
+    auto central = std::dynamic_pointer_cast<ConnectionPoolBackends::PostgresCentralHosted>(backend);
     g_assert(central);
 
     document->set_connection_server(central->get_host());
@@ -2160,7 +2160,7 @@ bool Frame_Glom::connection_request_password_and_choose_new_database_name()
   else if(document->get_hosting_mode() == Document::HostingMode::POSTGRES_SELF)
   {
     auto backend = connection_pool->get_backend();
-    auto self = dynamic_cast<ConnectionPoolBackends::PostgresSelfHosted*>(backend);
+    auto self = std::dynamic_pointer_cast<ConnectionPoolBackends::PostgresSelfHosted>(backend);
     g_assert(self);
 
     document->set_connection_port(self->get_port());
diff --git a/glom/glom_create_from_example.cc b/glom/glom_create_from_example.cc
index d29d0ee..d09d91a 100644
--- a/glom/glom_create_from_example.cc
+++ b/glom/glom_create_from_example.cc
@@ -469,8 +469,7 @@ int main(int argc, char* argv[])
     connection_pool->set_password(password); //TODO: Take this from stdin instead.
     
     auto backend = connection_pool->get_backend();
-    auto central = 
-      dynamic_cast<Glom::ConnectionPoolBackends::PostgresCentralHosted*>(backend);
+    auto central = std::dynamic_pointer_cast<Glom::ConnectionPoolBackends::PostgresCentralHosted>(backend);
     g_assert(central);
 
     central->set_host(group.m_arg_server_hostname);
diff --git a/glom/glom_test_connection.cc b/glom/glom_test_connection.cc
index 7b9b5f7..f59e938 100644
--- a/glom/glom_test_connection.cc
+++ b/glom/glom_test_connection.cc
@@ -194,13 +194,13 @@ int main(int argc, char* argv[])
 
   //Specify the backend and backend-specific details to be used by the connectionpool.
   //This is usually done by ConnectionPool::setup_from_document():
-  Glom::ConnectionPoolBackends::Backend* backend = nullptr;
+  std::shared_ptr<Glom::ConnectionPoolBackends::Backend> backend;
 #ifdef GLOM_ENABLE_MYSQL
   if(group.m_arg_use_mysql)
   {
     //TODO: Move some of the *CentralHosted API into a multiply-inherited Server base class,
     //to avoid the duplication?
-    auto derived_backend = new Glom::ConnectionPoolBackends::MySQLCentralHosted;
+    auto derived_backend = std::make_shared<Glom::ConnectionPoolBackends::MySQLCentralHosted;
 
     //Use a specified port, or try all suitable ports:
     if(group.m_arg_server_port)
@@ -219,7 +219,7 @@ int main(int argc, char* argv[])
   else
 #endif //GLOM_ENABLE_MYSQL
   {
-    auto derived_backend = new Glom::ConnectionPoolBackends::PostgresCentralHosted;
+    auto derived_backend = std::make_shared<Glom::ConnectionPoolBackends::PostgresCentralHosted>();
 
     //Use a specified port, or try all suitable ports:
     if(group.m_arg_server_port)
diff --git a/glom/libglom/connectionpool.cc b/glom/libglom/connectionpool.cc
index f78bf28..5acd38a 100644
--- a/glom/libglom/connectionpool.cc
+++ b/glom/libglom/connectionpool.cc
@@ -110,7 +110,6 @@ ConnectionPool::ConnectionPool()
 :
   m_epc_publisher(nullptr),
   m_dialog_epc_progress(nullptr),
-  m_backend(nullptr),
   m_sharedconnection_refcount(0),
   m_ready_to_connect(false),
   m_pFieldTypes(nullptr),
@@ -145,41 +144,41 @@ void ConnectionPool::setup_from_document(const std::shared_ptr<const Document>&
   {
   case Document::HostingMode::POSTGRES_SELF:
     {
-      auto backend = new ConnectionPoolBackends::PostgresSelfHosted;
+      auto backend = std::make_shared<ConnectionPoolBackends::PostgresSelfHosted>();
       backend->set_database_directory_uri(document->get_connection_self_hosted_directory_uri());
-      set_backend(std::shared_ptr<ConnectionPool::Backend>(backend)); //TODO: Use make_shared()?
+      set_backend(backend);
     }
     break;
   case Document::HostingMode::POSTGRES_CENTRAL:
     {
-      auto backend = new ConnectionPoolBackends::PostgresCentralHosted;
+      auto backend = std::make_shared<ConnectionPoolBackends::PostgresCentralHosted>();
       backend->set_host(document->get_connection_server());
       backend->set_port(document->get_connection_port());
       backend->set_try_other_ports(document->get_connection_try_other_ports());
-      set_backend(std::shared_ptr<ConnectionPool::Backend>(backend)); //TODO: Use make_shared()?
+      set_backend(backend);
     }
     break;
   case Document::HostingMode::SQLITE:
     {
-      auto backend = new ConnectionPoolBackends::Sqlite;
+      auto backend = std::make_shared<ConnectionPoolBackends::Sqlite>();
       backend->set_database_directory_uri(document->get_connection_self_hosted_directory_uri());
-      set_backend(std::shared_ptr<ConnectionPool::Backend>(backend)); //TODO: Use make_shared()?
+      set_backend(backend);
     }
     break;
   case Document::HostingMode::MYSQL_SELF:
     {
-      auto backend = new ConnectionPoolBackends::MySQLSelfHosted;
+      auto backend = std::make_shared<ConnectionPoolBackends::MySQLSelfHosted>();
       backend->set_database_directory_uri(document->get_connection_self_hosted_directory_uri());
-      set_backend(std::shared_ptr<ConnectionPool::Backend>(backend)); //TODO: Use make_shared()?
+      set_backend(backend);
     }
     break;
   case Document::HostingMode::MYSQL_CENTRAL:
     {
-      auto backend = new ConnectionPoolBackends::MySQLCentralHosted;
+      auto backend = std::make_shared<ConnectionPoolBackends::MySQLCentralHosted>();
       backend->set_host(document->get_connection_server());
       backend->set_port(document->get_connection_port());
       backend->set_try_other_ports(document->get_connection_try_other_ports());
-      set_backend(std::shared_ptr<ConnectionPool::Backend>(backend)); //TODO: Use make_shared()?
+      set_backend(backend);
     }
     break;
 
@@ -212,26 +211,25 @@ void ConnectionPool::set_ready_to_connect(bool val)
   m_ready_to_connect = val;
 }
 
-void ConnectionPool::set_backend(std::shared_ptr<Backend> backend)
+void ConnectionPool::set_backend(const std::shared_ptr<Backend>& backend)
 {
   m_backend = backend;
 }
 
-ConnectionPool::Backend* ConnectionPool::get_backend()
+std::shared_ptr<ConnectionPool::Backend> ConnectionPool::get_backend()
 {
-  return m_backend.get();
+  return m_backend;
 }
 
-const ConnectionPool::Backend* ConnectionPool::get_backend() const
+std::shared_ptr<const ConnectionPool::Backend> ConnectionPool::get_backend() const
 {
-  return m_backend.get();
+  return m_backend;
 }
 
 bool ConnectionPool::get_backend_supports_cursor() const
 {
   //TODO: Is there a generic libgda way to discover this?
-  const auto sqlite_backend =
-    dynamic_cast<const ConnectionPoolBackends::Sqlite*>(get_backend());
+  const auto sqlite_backend = std::dynamic_pointer_cast<const ConnectionPoolBackends::Sqlite>(get_backend());
   return !sqlite_backend;
 }
 
@@ -244,7 +242,7 @@ std::shared_ptr<SharedConnection> ConnectionPool::get_and_connect()
   if(!connection_pool)
     return result;
 
-  if(!(connection_pool->m_backend.get()))
+  if(!(connection_pool->m_backend))
   {
     std::cerr << G_STRFUNC << ": m_backend is null." << std::endl;
     return result; //TODO: Return a failure_type::NO_BACKEND error?, though that would be tedious.
@@ -289,7 +287,7 @@ std::shared_ptr<SharedConnection> ConnectionPool::connect()
   //std::cout << G_STRFUNC << ": debug" << std::endl;
 
   //Don't try to connect if we don't have a backend to connect to.
-  g_return_val_if_fail(m_backend.get(), std::shared_ptr<SharedConnection>());
+  g_return_val_if_fail(m_backend, std::shared_ptr<SharedConnection>());
 
   if(get_ready_to_connect() || m_fake_connection)
   {
@@ -402,7 +400,7 @@ std::shared_ptr<SharedConnection> ConnectionPool::connect()
 
 void ConnectionPool::create_database(const SlotProgress& slot_progress, const Glib::ustring& database_name)
 {
-  if(m_backend.get())
+  if(m_backend)
     m_backend->create_database(slot_progress, database_name, get_user(), get_password());
 }
 
@@ -423,7 +421,7 @@ void ConnectionPool::set_user(const Glib::ustring& value)
 
 bool ConnectionPool::save_backup(const SlotProgress& slot_progress, const std::string& path_dir)
 {
-  g_assert(m_backend.get());
+  g_assert(m_backend);
 
   const auto old_uri = m_backend->get_database_directory_uri();
 
@@ -449,7 +447,7 @@ bool ConnectionPool::save_backup(const SlotProgress& slot_progress, const std::s
 
 bool ConnectionPool::convert_backup(const SlotProgress& slot_progress, const std::string& 
backup_data_file_path)
 {
-  g_assert(m_backend.get());
+  g_assert(m_backend);
 
   const auto result = m_backend->convert_backup(slot_progress, backup_data_file_path, m_user, m_password, 
m_database);
   if(!result)
@@ -517,7 +515,7 @@ const FieldTypes* ConnectionPool::get_field_types() const
 
 Gnome::Gda::SqlOperatorType ConnectionPool::get_string_find_operator() const
 {
-  g_assert(m_backend.get());
+  g_assert(m_backend);
   return m_backend->get_string_find_operator();
 }
 
@@ -629,7 +627,7 @@ static void on_linux_signal(int signum)
 
 ConnectionPool::StartupErrors ConnectionPool::startup(const SlotProgress& slot_progress, bool network_shared)
 {
-  if(!m_backend.get())
+  if(!m_backend)
     return Backend::StartupErrors::FAILED_UNKNOWN_REASON;
 
   const auto started = m_backend->startup(slot_progress, network_shared);
@@ -667,7 +665,7 @@ bool ConnectionPool::cleanup(const SlotProgress& slot_progress)
   //And make sure that connect() tries to make a new connection:
   invalidate_connection();
       
-  if(m_backend.get())
+  if(m_backend)
     result = m_backend->cleanup(slot_progress);
 
 #ifndef G_OS_WIN32
@@ -688,7 +686,7 @@ bool ConnectionPool::cleanup(const SlotProgress& slot_progress)
 
 bool ConnectionPool::set_network_shared(const SlotProgress& slot_progress, bool network_shared)
 {
-  if(m_backend.get())
+  if(m_backend)
     return m_backend->set_network_shared(slot_progress, network_shared);
   else
     return false;
@@ -806,7 +804,7 @@ bool ConnectionPool::change_columns(const Glib::ustring& table_name, const type_
 
 ConnectionPool::InitErrors ConnectionPool::initialize(const SlotProgress& slot_progress, bool network_shared)
 {
-  if(m_backend.get())
+  if(m_backend)
     return m_backend->initialize(slot_progress, get_user(), get_password(), network_shared);
   else
     return Backend::InitErrors::OTHER;
diff --git a/glom/libglom/connectionpool.h b/glom/libglom/connectionpool.h
index 3835fc9..1ead068 100644
--- a/glom/libglom/connectionpool.h
+++ b/glom/libglom/connectionpool.h
@@ -84,12 +84,9 @@ class Document;
  */
 class ConnectionPool : public sigc::trackable
 {
-private:
-
-
 public:
 
-  //These are public for use by std::make_shared<>().
+  //This are public for use by std::make_shared<>().
   //TODO: Does this need to be virtual?
   ConnectionPool();
   virtual ~ConnectionPool();
@@ -142,10 +139,10 @@ public:
    */
   void set_fake_connection();
 
-  void set_backend(std::shared_ptr<Backend> backend);
+  void set_backend(const std::shared_ptr<Backend>& backend);
 
-  Backend* get_backend();
-  const Backend* get_backend() const;
+  std::shared_ptr<Backend> get_backend();
+  std::shared_ptr<const Backend> get_backend() const;
   
   /** Discover whether the backend can create GdaDataModels that can be iterated,
    * by creating them with the GDA_STATEMENT_MODEL_CURSOR_FORWARD flag.
diff --git a/glom/libglom/db_utils.cc b/glom/libglom/db_utils.cc
index 539f824..5c91186 100644
--- a/glom/libglom/db_utils.cc
+++ b/glom/libglom/db_utils.cc
@@ -199,8 +199,7 @@ bool create_database(const std::shared_ptr<Document>& document, const Glib::ustr
     
     //Save the port, if appropriate, so the document can be used to connect again:
     auto backend = connection_pool->get_backend();
-    auto central = 
-      dynamic_cast<Glom::ConnectionPoolBackends::PostgresCentralHosted*>(backend);
+    auto central = std::dynamic_pointer_cast<Glom::ConnectionPoolBackends::PostgresCentralHosted>(backend);
     if(central)
     {
       document->set_connection_port( central->get_port() );
@@ -2242,9 +2241,8 @@ void set_fake_connection()
 {
   //Allow a fake connection, so sqlbuilder_get_full_query() can work:
   auto connection_pool = Glom::ConnectionPool::get_instance();
-  auto backend = 
-    new Glom::ConnectionPoolBackends::PostgresCentralHosted();
-  connection_pool->set_backend(std::shared_ptr<Glom::ConnectionPool::Backend>(backend));
+  auto backend = std::make_shared<Glom::ConnectionPoolBackends::PostgresCentralHosted>();
+  connection_pool->set_backend(backend);
   connection_pool->set_fake_connection();
 }
 
diff --git a/glom/libglom/test_connectionpool.cc b/glom/libglom/test_connectionpool.cc
index a6487f9..90a820e 100644
--- a/glom/libglom/test_connectionpool.cc
+++ b/glom/libglom/test_connectionpool.cc
@@ -50,15 +50,15 @@ int main()
       connection_pool->set_password("murraycpw");
 
 #ifdef GLOM_ENABLE_POSTGRESQL
-      auto backend = new Glom::ConnectionPoolBackends::PostgresCentralHosted;
+      auto backend = std::make_shared<Glom::ConnectionPoolBackends::PostgresCentralHosted>();
       backend->set_host("localhost");
       backend->set_port(5433);
       backend->set_try_other_ports(false);
 #else
-      auto backend = new Glom::ConnectionPoolBackends::Sqlite;
+      auto backend = std::make_shared<Glom::ConnectionPoolBackends::Sqlite>();
 #endif //GLOM_ENABLE_POSTGRESQL
 
-      connection_pool->set_backend(std::shared_ptr<Glom::ConnectionPool::Backend>(backend));
+      connection_pool->set_backend(backend);
       connection_pool->set_ready_to_connect(); //connect_to_server() will now attempt the connection-> 
Shared instances of m_Connection will also be usable.
     }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]