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