From 194c1ba7f7c3a833a55f44df54d1ae40b622a497 Mon Sep 17 00:00:00 2001 From: Oren Miller Date: Thu, 11 Sep 2014 22:14:05 -0500 Subject: [PATCH] Store data dictionaries in shared_ptr so we don't have to worry about cleanup --- src/C++/DataDictionary.cpp | 2 +- src/C++/DataDictionaryProvider.cpp | 8 ++++---- src/C++/DataDictionaryProvider.h | 13 +++++++++---- src/C++/SessionFactory.cpp | 26 +++++++++++--------------- src/C++/SessionFactory.h | 10 ++++------ src/C++/test/SessionTestCase.cpp | 16 ++++++++-------- 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/C++/DataDictionary.cpp b/src/C++/DataDictionary.cpp index 8bf719dee..02568892e 100644 --- a/src/C++/DataDictionary.cpp +++ b/src/C++/DataDictionary.cpp @@ -75,7 +75,7 @@ DataDictionary::~DataDictionary() FieldPresenceMap::const_iterator iter = presenceMap.begin(); for ( ; iter != presenceMap.end(); ++iter ) delete iter->second.second; -} + } } DataDictionary& DataDictionary::operator=( const DataDictionary& rhs ) diff --git a/src/C++/DataDictionaryProvider.cpp b/src/C++/DataDictionaryProvider.cpp index b824aa412..cc3fbb709 100644 --- a/src/C++/DataDictionaryProvider.cpp +++ b/src/C++/DataDictionaryProvider.cpp @@ -37,7 +37,7 @@ DataDictionaryProvider::DataDictionaryProvider( const DataDictionaryProvider& co const DataDictionary& DataDictionaryProvider::getSessionDataDictionary (const BeginString& beginString) const throw( DataDictionaryNotFound ) { - std::map::const_iterator find = + std::map >::const_iterator find = m_transportDictionaries.find(beginString); if( find != m_transportDictionaries.end() ) return *find->second; @@ -48,7 +48,7 @@ const DataDictionary& DataDictionaryProvider::getSessionDataDictionary const DataDictionary& DataDictionaryProvider::getApplicationDataDictionary (const ApplVerID& applVerID) const throw( DataDictionaryNotFound ) { - std::map::const_iterator find = + std::map >::const_iterator find = m_applicationDictionaries.find(applVerID); if( find != m_applicationDictionaries.end() ) return *find->second; @@ -57,13 +57,13 @@ const DataDictionary& DataDictionaryProvider::getApplicationDataDictionary } void DataDictionaryProvider::addTransportDataDictionary -(const BeginString& beginString, const DataDictionary * pDD) +(const BeginString& beginString, std::shared_ptr pDD) { m_transportDictionaries[beginString.getValue()] = pDD; } void DataDictionaryProvider::addApplicationDataDictionary -(const ApplVerID& applVerID, const DataDictionary * pDD) +(const ApplVerID& applVerID, std::shared_ptr pDD) { m_applicationDictionaries[applVerID.getValue()] = pDD; } diff --git a/src/C++/DataDictionaryProvider.h b/src/C++/DataDictionaryProvider.h index d4d770f15..2bad68f9b 100644 --- a/src/C++/DataDictionaryProvider.h +++ b/src/C++/DataDictionaryProvider.h @@ -50,12 +50,17 @@ class DataDictionaryProvider const DataDictionary& getApplicationDataDictionary(const ApplVerID& applVerID) const throw( DataDictionaryNotFound ); - void addTransportDataDictionary(const BeginString& beginString, const DataDictionary*); - void addApplicationDataDictionary(const ApplVerID& applVerID, const DataDictionary*); + void addTransportDataDictionary(const BeginString& beginString, std::shared_ptr); + void addApplicationDataDictionary(const ApplVerID& applVerID, std::shared_ptr); + + void addTransportDataDictionary(const BeginString& beginString, const std::string& path) + { addTransportDataDictionary(beginString, std::shared_ptr( new DataDictionary(path) )); } + void addApplicationDataDictionary(const ApplVerID& applVerID, const std::string& path) + { addApplicationDataDictionary(applVerID, std::shared_ptr( new DataDictionary(path) )); } private: - std::map m_transportDictionaries; - std::map m_applicationDictionaries; + std::map > m_transportDictionaries; + std::map > m_applicationDictionaries; DataDictionary emptyDataDictionary; }; } diff --git a/src/C++/SessionFactory.cpp b/src/C++/SessionFactory.cpp index 32eb0ffd2..18c32c56a 100644 --- a/src/C++/SessionFactory.cpp +++ b/src/C++/SessionFactory.cpp @@ -36,9 +36,6 @@ namespace FIX { SessionFactory::~SessionFactory() { - Dictionaries::iterator i = m_dictionaries.begin(); - for ( ; i != m_dictionaries.end(); ++i ) - delete *i; } Session* SessionFactory::create( const SessionID& sessionID, @@ -206,25 +203,24 @@ void SessionFactory::destroy( Session* pSession ) delete pSession; } -const DataDictionary * SessionFactory::createDataDictionary(const SessionID& sessionID, - const Dictionary& settings, - const std::string& settingsKey) throw(ConfigError) +std::shared_ptr SessionFactory::createDataDictionary(const SessionID& sessionID, + const Dictionary& settings, + const std::string& settingsKey) throw(ConfigError) { - DataDictionary * pDD = 0; + std::shared_ptr pDD; std::string path = settings.getString( settingsKey ); - PathToDictionary::iterator i = m_pathToDictionary.find( path ); - if ( i != m_pathToDictionary.end() ) + Dictionaries::iterator i = m_dictionaries.find( path ); + if ( i != m_dictionaries.end() ) { pDD = i->second; } else { - pDD = new DataDictionary( path ); - m_pathToDictionary[ path ] = pDD; + pDD = std::shared_ptr(new DataDictionary( path )); + m_dictionaries[ path ] = pDD; } - DataDictionary * pCopyOfDD = new DataDictionary(*pDD); - m_dictionaries.push_back(pCopyOfDD); + std::shared_ptr pCopyOfDD = std::shared_ptr(new DataDictionary(*pDD)); if( settings.has( VALIDATE_FIELDS_OUT_OF_ORDER ) ) pCopyOfDD->checkFieldsOutOfOrder( settings.getBool( VALIDATE_FIELDS_OUT_OF_ORDER ) ); @@ -240,7 +236,7 @@ void SessionFactory::processFixtDataDictionaries(const SessionID& sessionID, const Dictionary& settings, DataDictionaryProvider& provider) throw(ConfigError) { - const DataDictionary * pDataDictionary = createDataDictionary(sessionID, settings, TRANSPORT_DATA_DICTIONARY); + std::shared_ptr pDataDictionary = createDataDictionary(sessionID, settings, TRANSPORT_DATA_DICTIONARY); provider.addTransportDataDictionary(sessionID.getBeginString(), pDataDictionary); for(Dictionary::const_iterator data = settings.begin(); data != settings.end(); ++data) @@ -271,7 +267,7 @@ void SessionFactory::processFixDataDictionary(const SessionID& sessionID, const Dictionary& settings, DataDictionaryProvider& provider) throw(ConfigError) { - const DataDictionary * pDataDictionary = createDataDictionary(sessionID, settings, DATA_DICTIONARY); + std::shared_ptr pDataDictionary = createDataDictionary(sessionID, settings, DATA_DICTIONARY); provider.addTransportDataDictionary(sessionID.getBeginString(), pDataDictionary); provider.addApplicationDataDictionary(Message::toApplVerID(sessionID.getBeginString()), pDataDictionary); } diff --git a/src/C++/SessionFactory.h b/src/C++/SessionFactory.h index 44ac14bba..4673675b4 100644 --- a/src/C++/SessionFactory.h +++ b/src/C++/SessionFactory.h @@ -60,12 +60,11 @@ class SessionFactory void destroy( Session* pSession ); private: - typedef std::map < std::string, DataDictionary* > PathToDictionary; - typedef std::vector Dictionaries; + typedef std::map < std::string, std::shared_ptr > Dictionaries; - const DataDictionary * createDataDictionary(const SessionID& sessionID, - const Dictionary& settings, - const std::string& settingsKey) throw(ConfigError); + std::shared_ptr createDataDictionary(const SessionID& sessionID, + const Dictionary& settings, + const std::string& settingsKey) throw(ConfigError); void processFixtDataDictionaries(const SessionID& sessionID, const Dictionary& settings, @@ -80,7 +79,6 @@ class SessionFactory Application& m_application; MessageStoreFactory& m_messageStoreFactory; LogFactory* m_pLogFactory; - PathToDictionary m_pathToDictionary; Dictionaries m_dictionaries; }; } diff --git a/src/C++/test/SessionTestCase.cpp b/src/C++/test/SessionTestCase.cpp index c53be00d4..4c0686dac 100644 --- a/src/C++/test/SessionTestCase.cpp +++ b/src/C++/test/SessionTestCase.cpp @@ -352,7 +352,7 @@ struct sessionFixture : public TestCallback TimeRange sessionTime( startTime, endTime ); DataDictionaryProvider provider; - provider.addTransportDataDictionary( sessionID.getBeginString(), new DataDictionary("../spec/FIX42.xml") ); + provider.addTransportDataDictionary( sessionID.getBeginString(), "../spec/FIX42.xml" ); object = new Session( *this, factory, sessionID, provider, sessionTime, heartBtInt, 0 ); object->setResponder( this ); @@ -391,9 +391,9 @@ struct sessionT11Fixture : public TestCallback TimeRange sessionTime( startTime, endTime ); DataDictionaryProvider provider; - provider.addTransportDataDictionary( sessionID.getBeginString(), new DataDictionary("../spec/FIXT11.xml") ); - provider.addApplicationDataDictionary( ApplVerID(ApplVerID_FIX50), new DataDictionary("../spec/FIX50.xml") ); - provider.addApplicationDataDictionary( ApplVerID(ApplVerID_FIX42), new DataDictionary("../spec/FIX42.xml") ); + provider.addTransportDataDictionary( sessionID.getBeginString(), "../spec/FIXT11.xml" ); + provider.addApplicationDataDictionary( ApplVerID(ApplVerID_FIX50), "../spec/FIX50.xml" ); + provider.addApplicationDataDictionary( ApplVerID(ApplVerID_FIX42), "../spec/FIX42.xml" ); object = new Session( *this, factory, sessionID, provider, sessionTime, heartBtInt, 0 ); object->setSenderDefaultApplVerID( FIX::Message::toApplVerID(BeginString("FIX.5.0")) ); @@ -615,7 +615,7 @@ TEST_FIXTURE(acceptorFixture, callDisconnect) TEST_FIXTURE(sessionFixture, doesSessionExist) { DataDictionaryProvider provider; - provider.addTransportDataDictionary( BeginString("FIX.4.2"), new DataDictionary() ); + provider.addTransportDataDictionary( BeginString("FIX.4.2"), std::shared_ptr(new DataDictionary()) ); Session * pSession1 = new Session ( *this, factory, SessionID( BeginString( "FIX.4.2" ), @@ -686,7 +686,7 @@ TEST_FIXTURE(sessionFixture, doesSessionExist) TEST_FIXTURE(sessionFixture, lookupSession) { DataDictionaryProvider provider; - provider.addTransportDataDictionary( BeginString("FIX.4.2"), new DataDictionary() ); + provider.addTransportDataDictionary( BeginString("FIX.4.2"), std::shared_ptr(new DataDictionary()) ); Session* pSession1 = new Session ( *this, factory, SessionID( BeginString( "FIX.4.2" ), @@ -741,7 +741,7 @@ TEST_FIXTURE(sessionFixture, lookupSession) TEST_FIXTURE(sessionFixture, registerSession) { DataDictionaryProvider provider; - provider.addTransportDataDictionary( BeginString("FIX.4.2"), new DataDictionary() ); + provider.addTransportDataDictionary( BeginString("FIX.4.2"), std::shared_ptr(new DataDictionary()) ); Session* pSession = new Session ( *this, factory, SessionID( BeginString( "FIX.4.2" ), @@ -1355,7 +1355,7 @@ struct initiatorCreatedBeforeStartTimeFixture : public TestCallback SessionID sessionID( BeginString( "FIX.4.2" ), SenderCompID( "MJKG" ), TargetCompID( "IZZY" ) ); DataDictionaryProvider provider; - provider.addTransportDataDictionary( sessionID.getBeginString(), new DataDictionary("../spec/FIX42.xml") ); + provider.addTransportDataDictionary( sessionID.getBeginString(), "../spec/FIX42.xml" ); object = new Session( *this, memStoreFactory, sessionID, provider, sessionTime, HEARTBTINT, 0 ); }