![]() |
00001 /* -*- C++ -*- */ 00002 00003 /**************************************************************************** 00004 ** Copyright (c) quickfixengine.org All rights reserved. 00005 ** 00006 ** This file is part of the QuickFIX FIX Engine 00007 ** 00008 ** This file may be distributed under the terms of the quickfixengine.org 00009 ** license as defined by quickfixengine.org and appearing in the file 00010 ** LICENSE included in the packaging of this file. 00011 ** 00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00014 ** 00015 ** See http://www.quickfixengine.org/LICENSE for licensing information. 00016 ** 00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are 00018 ** not clear to you. 00019 ** 00020 ****************************************************************************/ 00021 00022 #ifndef FIX_DATABASECONNECTIONPOOL_H 00023 #define FIX_DATABASECONNECTIONPOOL_H 00024 00025 #ifdef _MSC_VER 00026 #pragma warning( disable : 4503 4355 4786 4290 ) 00027 #endif 00028 00029 #include "DatabaseConnectionID.h" 00030 #include <string> 00031 #include <map> 00032 00033 namespace FIX 00034 { 00035 template< typename T > class DatabaseConnectionPool 00036 { 00037 public: 00038 DatabaseConnectionPool( bool poolConnections ) 00039 : m_poolConnections( poolConnections ) {} 00040 00041 T* create( const DatabaseConnectionID& id ) 00042 { 00043 if( !m_poolConnections ) 00044 return new T( id ); 00045 00046 if( m_connections.find( id ) == m_connections.end() ) 00047 { 00048 m_connections[id] = Connection 00049 ( new T(id), 0 ); 00050 } 00051 m_connections[id].second++; 00052 return m_connections[id].first; 00053 } 00054 00055 bool destroy( T* pConnection ) 00056 { 00057 if( !m_poolConnections ) 00058 { 00059 delete pConnection; 00060 return true; 00061 } 00062 00063 const DatabaseConnectionID& id = pConnection->connectionID(); 00064 if( m_connections.find( id ) == m_connections.end() ) 00065 return false; 00066 00067 Connection& connection = m_connections[id]; 00068 if( connection.first != pConnection ) 00069 return false; 00070 00071 connection.second--; 00072 if( connection.second == 0 ) 00073 { 00074 m_connections.erase( id ); 00075 delete pConnection; 00076 } 00077 return true; 00078 } 00079 00080 private: 00081 typedef std::pair<T*, int> 00082 Connection; 00083 typedef std::map<DatabaseConnectionID, Connection> 00084 Connections; 00085 00086 Connections m_connections; 00087 bool m_poolConnections; 00088 }; 00089 } 00090 00091 #endif