Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: WMS Client Driver 4 : * Purpose: WMSMiniDriverManager implementation. 5 : * Author: Adam Nowacki, nowak@xpam.de 6 : * 7 : ****************************************************************************** 8 : * 9 : * Copyright (c) 2007, Adam Nowacki 10 : * 2016, Lucian Plesea 11 : * 12 : * A single global MiniDriverManager exists, containing factories for all 13 : *possible types of WMS minidrivers. Minidriver object factories get registered 14 : *in wmsdriver.cpp, during the WMS driver registration with GDAL 15 : * 16 : * SPDX-License-Identifier: MIT 17 : ****************************************************************************/ 18 : 19 : #include "wmsdriver.h" 20 : 21 : class WMSMiniDriverManager 22 : { 23 : public: 24 9 : WMSMiniDriverManager() 25 9 : { 26 9 : } 27 : 28 9 : ~WMSMiniDriverManager() 29 9 : { 30 9 : erase(); 31 9 : } 32 : 33 : public: 34 : void Register(WMSMiniDriverFactory *mdf); 35 : // Clean up the minidriver factories 36 : void erase(); 37 : WMSMiniDriverFactory *Find(const CPLString &name); 38 : 39 : protected: 40 : std::vector<WMSMiniDriverFactory *> m_mdfs; 41 : }; 42 : 43 : // Called by WMS driver deregister, also by destructor 44 9 : void WMSMiniDriverManager::erase() 45 : { 46 108 : for (size_t i = 0; i < m_mdfs.size(); i++) 47 99 : delete m_mdfs[i]; 48 9 : m_mdfs.clear(); 49 9 : } 50 : 51 447 : WMSMiniDriverFactory *WMSMiniDriverManager::Find(const CPLString &name) 52 : { 53 2001 : for (size_t i = 0; i < m_mdfs.size(); i++) 54 1902 : if (EQUAL(name.c_str(), m_mdfs[i]->m_name)) 55 348 : return m_mdfs[i]; 56 99 : return nullptr; 57 : } 58 : 59 99 : void WMSMiniDriverManager::Register(WMSMiniDriverFactory *mdf) 60 : { 61 : // Prevent duplicates 62 99 : if (!Find(mdf->m_name)) 63 99 : m_mdfs.push_back(mdf); 64 : else // Register takes ownership of factories, so it removes the duplicate 65 0 : delete mdf; 66 99 : } 67 : 68 : // global object containing minidriver factories 69 : static WMSMiniDriverManager g_mini_driver_manager; 70 : 71 : // If a matching factory is found in the global minidriver manager, it returns a 72 : // new minidriver object 73 348 : WMSMiniDriver *NewWMSMiniDriver(const CPLString &name) 74 : { 75 348 : const WMSMiniDriverFactory *factory = g_mini_driver_manager.Find(name); 76 348 : if (factory == nullptr) 77 0 : return nullptr; 78 348 : return factory->New(); 79 : } 80 : 81 : // Registers a minidriver factory with the global minidriver manager 82 99 : void WMSRegisterMiniDriverFactory(WMSMiniDriverFactory *mdf) 83 : { 84 99 : g_mini_driver_manager.Register(mdf); 85 99 : } 86 : 87 0 : void WMSDeregisterMiniDrivers(CPL_UNUSED GDALDriver *) 88 : { 89 0 : g_mini_driver_manager.erase(); 90 0 : }