Wexus2 0.20
wexus2.src/wexus/OpenDatabases.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __INCLUDED_WEXUS_OPENDATABASES_H__
00009 #define __INCLUDED_WEXUS_OPENDATABASES_H__
00010 
00011 #include <QSqlDatabase>
00012 #include <QMap>
00013 
00014 #include <wexus/TR1.h>
00015 
00016 namespace wexus
00017 {
00018   class OpenDatabases;
00019 }
00020 
00021 /**
00022  * A per-thread class that managers all the open QSqlDatabase
00023  * connections held by a thread.
00024  *
00025  * We need to use something like this because QSqlDatabase objects
00026  * can only be used in the threads that created them, hense each thread
00027  * needs its own set.
00028  *
00029  * @author Aleksander Demko
00030  */ 
00031 class wexus::OpenDatabases
00032 {
00033   public:
00034     /**
00035      * Speciall wrapper around the smart pointer because
00036      * QSqlDatabase needs some extra closing logic.
00037      *
00038      * This class is copyable.
00039      *
00040      * @author Aleksander Demko
00041      */ 
00042     class Handle
00043     {
00044       public:
00045         Handle(void);
00046         Handle(const QString &filename);
00047 
00048         /**
00049          * Gets the QSqlDatabase from this threads's TLS.
00050          *
00051          * @author Aleksander Demko
00052          */ 
00053         QSqlDatabase & database(void);
00054 
00055       private:
00056         QString dm_filename;
00057     };
00058 
00059   public:
00060     /// destructor
00061     ~OpenDatabases();
00062 
00063     /**
00064      * Returns the "static-like" instance for this thread.
00065      *
00066      * @author Aleksander Demko
00067      */ 
00068     static OpenDatabases *threadInstance(void);
00069 
00070     /// uses threadInstance
00071     static QSqlDatabase & database(const QString &filename);
00072 
00073   protected:
00074     /// constructor, called by threadInstance when needed
00075     OpenDatabases(void);
00076 
00077   private:
00078     typedef std::shared_ptr<QSqlDatabase> data_t;
00079     typedef QMap<QString, data_t> map_t;
00080 
00081     map_t dm_map;
00082 };
00083 
00084 #endif
00085 
 All Classes Namespaces Functions Variables Enumerations Enumerator