1 /* 2 * Filesystem provider registry. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include "provider_registry.h" 23 24 /* To complete forward declaration. */ 25 26 #include "provider.h" 27 28 29 30 /* Methods for use with the lock already acquired. */ 31 32 /* Return any registered provider for the given 'fileid' or NULL if no such 33 provider is registered. */ 34 35 Provider *ProviderRegistry::get(fileid_t fileid) 36 { 37 ProviderMapping::iterator entry = _providers.find(fileid); 38 Provider *provider; 39 40 if (entry == _providers.end()) 41 provider = NULL; 42 else 43 provider = entry->second; 44 45 return provider; 46 } 47 48 /* Remove a provider and its resources for the given 'fileid'. */ 49 50 void ProviderRegistry::remove(fileid_t fileid, Provider *provider) 51 { 52 _providers.erase(fileid); 53 delete provider; 54 } 55 56 /* Register a 'provider' for the given 'fileid'. */ 57 58 void ProviderRegistry::set(fileid_t fileid, Provider *provider) 59 { 60 ProviderMapping::iterator entry = _providers.find(fileid); 61 62 if (entry != _providers.end()) 63 return; 64 65 _providers[fileid] = provider; 66 } 67 68 69 70 /* Detach from a provider, potentially removing it from the registry. */ 71 72 void ProviderRegistry::detach(fileid_t fileid, Provider *provider) 73 { 74 std::lock_guard<std::mutex> guard(_lock); 75 76 /* Remove from the registry, also removing any underlying object if removal 77 is pending. */ 78 79 if (!provider->detach()) 80 remove(fileid, provider); 81 } 82 83 // vim: tabstop=4 expandtab shiftwidth=4