//  // //  #### # # # -= SQLiteX =-  // // # # ## ## SQLiteX.h  // //  ## # ###  // //  ## # ### SQLite main header  // //  # # ## ##  // // #### ##### # # R1 (C)2005 Markus Ewald -> License.txt  // //  // #ifndef SQLITEX_SQLITEX_H #define SQLITEX_SQLITEX_H #include "SQLiteX/Config.h" #include "SQLite/sqlite3.h" namespace SQLiteX { //  // //  SQLiteX::SharedSQLite3  // //  // /// Shared sqlite database handle /** This class keeps an sqlite database used by multiple objects alive until the database can be released. */ class SharedSQLite3 { public: /// Constructor SQLITEX_API explicit SharedSQLite3(sqlite3 *pSQLite) : m_pRefCount(0), m_pSQLite(pSQLite) { m_pRefCount = new size_t(1); } /// Copy constructor SQLITEX_API SharedSQLite3(const SharedSQLite3 &Other) : m_pRefCount(Other.m_pRefCount), m_pSQLite(Other.m_pSQLite) { ++(*m_pRefCount); } /// Destructor SQLITEX_API ~SharedSQLite3() { release(); } /// Copy assignment operator SQLITEX_API SharedSQLite3 &operator =(const SharedSQLite3 &Other) { release(); m_pSQLite = Other.m_pSQLite; m_pRefCount = Other.m_pRefCount; ++(*m_pRefCount); return *this; } /// Implicit conversion to constant sqlite database handle SQLITEX_API operator sqlite3 *() const { return m_pSQLite; } private: /// Release the sqlite database handle void release() { if(m_pRefCount) { if(!--(*m_pRefCount)) { ::sqlite3_close(m_pSQLite); delete m_pRefCount; } m_pRefCount = 0; } } /// The sqlite database handle sqlite3 *m_pSQLite; /// Reference counter, shared by all clones of an instance size_t *m_pRefCount; }; } // namespace SQLiteX #endif // SQLITEX_SQLITEX_H