LCOV - code coverage report
Current view: top level - src - account_factory.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 23 29 79.3 %
Date: 2024-11-15 09:04:49 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : #pragma once
      18             : 
      19             : #include <string>
      20             : #include <map>
      21             : #include <vector>
      22             : #include <memory>
      23             : #include <mutex>
      24             : #include <utility>
      25             : #include <functional>
      26             : #include <ciso646>
      27             : 
      28             : namespace jami {
      29             : 
      30             : class Account;
      31             : class AccountGeneratorBase;
      32             : 
      33             : template<class T>
      34             : using AccountMap = std::map<std::string, std::shared_ptr<T>, std::less<>>;
      35             : 
      36             : class AccountFactory
      37             : {
      38             : public:
      39             :     static const std::string_view DEFAULT_ACCOUNT_TYPE;// = SIPAccount::ACCOUNT_TYPE;
      40             : 
      41             :     AccountFactory();
      42             : 
      43             :     bool isSupportedType(std::string_view accountType) const;
      44             : 
      45             :     std::shared_ptr<Account> createAccount(std::string_view accountType, const std::string& id);
      46             : 
      47             :     void removeAccount(Account& account);
      48             : 
      49             :     void removeAccount(std::string_view id);
      50             : 
      51             :     template<class T = Account>
      52           0 :     bool hasAccount(std::string_view id) const
      53             :     {
      54           0 :         std::lock_guard lk(mutex_);
      55             : 
      56           0 :         const auto map = getMap_<T>();
      57           0 :         return map and map->find(id) != map->cend();
      58           0 :     }
      59             : 
      60             :     template<class T = Account>
      61             :     void clear()
      62             :     {
      63             :         std::lock_guard lk(mutex_);
      64             : 
      65             :         auto map = getMap_<T>();
      66             :         if (!map)
      67             :             return;
      68             : 
      69             :         map->clear();
      70             :     }
      71             : 
      72             :     template<class T = Account>
      73             :     bool empty() const
      74             :     {
      75             :         std::lock_guard lock(mutex_);
      76             : 
      77             :         const auto map = getMap_<T>();
      78             :         return map and map->empty();
      79             :     }
      80             : 
      81             :     template<class T = Account>
      82             :     std::size_t accountCount() const
      83             :     {
      84             :         std::lock_guard lock(mutex_);
      85             : 
      86             :         const auto map = getMap_<T>();
      87             :         if (!map)
      88             :             return 0;
      89             : 
      90             :         return map->size();
      91             :     }
      92             : 
      93             :     template<class T = Account>
      94       32793 :     std::shared_ptr<T> getAccount(std::string_view id) const
      95             :     {
      96       32793 :         std::lock_guard lock(mutex_);
      97             : 
      98       32798 :         const auto map = getMap_<T>();
      99       32798 :         if (!map)
     100           6 :             return nullptr;
     101             : 
     102       32792 :         const auto& it = map->find(id);
     103       32792 :         if (it == map->cend())
     104           0 :             return nullptr;
     105             : 
     106       32792 :         return std::static_pointer_cast<T>(it->second);
     107       32798 :     }
     108             : 
     109             :     template<class T = Account>
     110          84 :     std::vector<std::shared_ptr<T>> getAllAccounts() const
     111             :     {
     112          84 :         std::lock_guard lock(mutex_);
     113          84 :         std::vector<std::shared_ptr<T>> v;
     114             : 
     115          84 :         if (const auto map = getMap_<T>()) {
     116          70 :             v.reserve(map->size());
     117         221 :             for (const auto& it : *map)
     118         151 :                 v.emplace_back(std::static_pointer_cast<T>(it.second));
     119             :         }
     120         168 :         return v;
     121          84 :     }
     122             : 
     123             : private:
     124             :     mutable std::recursive_mutex mutex_ {};
     125             :     std::map<std::string, std::function<std::shared_ptr<Account>(const std::string&)>, std::less<>> generators_ {};
     126             :     std::map<std::string, AccountMap<Account>, std::less<>> accountMaps_ {};
     127             : 
     128             :     template<class T>
     129       32882 :     const AccountMap<Account>* getMap_() const
     130             :     {
     131       32882 :         const auto& itermap = accountMaps_.find(T::ACCOUNT_TYPE);
     132       32882 :         if (itermap != accountMaps_.cend())
     133       32862 :             return &itermap->second;
     134          20 :         return nullptr;
     135             :     }
     136             : };
     137             : 
     138             : template<>
     139             : bool AccountFactory::hasAccount(std::string_view id) const;
     140             : 
     141             : template<>
     142             : void AccountFactory::clear();
     143             : 
     144             : template<>
     145             : std::vector<std::shared_ptr<Account>> AccountFactory::getAllAccounts() const;
     146             : 
     147             : template<>
     148             : std::shared_ptr<Account> AccountFactory::getAccount(std::string_view accountId) const;
     149             : 
     150             : template<>
     151             : bool AccountFactory::empty() const;
     152             : 
     153             : template<>
     154             : std::size_t AccountFactory::accountCount() const;
     155             : 
     156             : } // namespace jami

Generated by: LCOV version 1.14