DatabaseConnectionPool.h
Go to the documentation of this file.
/* -*- C++ -*- */
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifndef FIX_DATABASECONNECTIONPOOL_H
#define FIX_DATABASECONNECTIONPOOL_H
#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 4290 )
#endif
#include "DatabaseConnectionID.h"
#include <string>
#include <map>
namespace FIX
{
template< typename T > class DatabaseConnectionPool
{
public:
DatabaseConnectionPool( bool poolConnections )
: m_poolConnections( poolConnections ) {}
T* create( const DatabaseConnectionID& id )
{
if( !m_poolConnections )
return new T( id );
if( m_connections.find( id ) == m_connections.end() )
{
m_connections[id] = Connection
( new T(id), 0 );
}
m_connections[id].second++;
return m_connections[id].first;
}
bool destroy( T* pConnection )
{
if( !m_poolConnections )
{
delete pConnection;
return true;
}
const DatabaseConnectionID& id = pConnection->connectionID();
if( m_connections.find( id ) == m_connections.end() )
return false;
Connection& connection = m_connections[id];
if( connection.first != pConnection )
return false;
connection.second--;
if( connection.second == 0 )
{
m_connections.erase( id );
delete pConnection;
}
return true;
}
private:
typedef std::pair<T*, int>
Connection;
typedef std::map<DatabaseConnectionID, Connection>
Connections;
Connections m_connections;
bool m_poolConnections;
};
}
#endif
DatabaseConnectionID.h
FIX::DatabaseConnectionPool::m_connections
Connections m_connections
Definition: DatabaseConnectionPool.h:86
FIX::DatabaseConnectionPool::Connections
std::map< DatabaseConnectionID, Connection > Connections
Definition: DatabaseConnectionPool.h:84
FIX::DatabaseConnectionPool::DatabaseConnectionPool
DatabaseConnectionPool(bool poolConnections)
Definition: DatabaseConnectionPool.h:38
FIX::DatabaseConnectionPool::m_poolConnections
bool m_poolConnections
Definition: DatabaseConnectionPool.h:87
FIX
Definition: Acceptor.cpp:34
FIX::DatabaseConnectionPool::Connection
std::pair< T *, int > Connection
Definition: DatabaseConnectionPool.h:82
FIX::DatabaseConnectionPool::destroy
bool destroy(T *pConnection)
Definition: DatabaseConnectionPool.h:55
FIX::DatabaseConnectionPool::create
T * create(const DatabaseConnectionID &id)
Definition: DatabaseConnectionPool.h:41
FIX::DatabaseConnectionPool
Definition: DatabaseConnectionPool.h:35
FIX::DatabaseConnectionID
Definition: DatabaseConnectionID.h:34