LCOV - code coverage report
Current view: top level - src - account_factory.h (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 96.6 % 29 28
Test Date: 2026-06-13 09:18:46 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /*
       2              :  *  Copyright (C) 2004-2026 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 <functional>
      25              : 
      26              : namespace jami {
      27              : 
      28              : class Account;
      29              : class AccountGeneratorBase;
      30              : 
      31              : template<class T>
      32              : using AccountMap = std::map<std::string, std::shared_ptr<T>, std::less<>>;
      33              : 
      34              : class AccountFactory
      35              : {
      36              : public:
      37              :     static const std::string_view DEFAULT_ACCOUNT_TYPE; // = SIPAccount::ACCOUNT_TYPE;
      38              : 
      39              :     AccountFactory();
      40              : 
      41              :     bool isSupportedType(std::string_view accountType) const;
      42              : 
      43              :     std::shared_ptr<Account> createAccount(std::string_view accountType, const std::string& id);
      44              : 
      45              :     void removeAccount(Account& account);
      46              : 
      47              :     void removeAccount(std::string_view id);
      48              : 
      49              :     template<class T = Account>
      50           35 :     bool hasAccount(std::string_view id) const
      51              :     {
      52           35 :         std::lock_guard lk(mutex_);
      53              : 
      54           35 :         const auto map = getMap_<T>();
      55           70 :         return map and map->find(id) != map->cend();
      56           35 :     }
      57              : 
      58              :     template<class T = Account>
      59              :     void clear()
      60              :     {
      61              :         std::lock_guard lk(mutex_);
      62              : 
      63              :         auto map = getMap_<T>();
      64              :         if (!map)
      65              :             return;
      66              : 
      67              :         map->clear();
      68              :     }
      69              : 
      70              :     template<class T = Account>
      71              :     bool empty() const
      72              :     {
      73              :         std::lock_guard lock(mutex_);
      74              : 
      75              :         const auto map = getMap_<T>();
      76              :         return map and map->empty();
      77              :     }
      78              : 
      79              :     template<class T = Account>
      80              :     std::size_t accountCount() const
      81              :     {
      82              :         std::lock_guard lock(mutex_);
      83              : 
      84              :         const auto map = getMap_<T>();
      85              :         if (!map)
      86              :             return 0;
      87              : 
      88              :         return map->size();
      89              :     }
      90              : 
      91              :     template<class T = Account>
      92        12960 :     std::shared_ptr<T> getAccount(std::string_view id) const
      93              :     {
      94        12960 :         std::lock_guard lock(mutex_);
      95              : 
      96        12960 :         const auto map = getMap_<T>();
      97        12960 :         if (!map)
      98            8 :             return nullptr;
      99              : 
     100        12952 :         const auto& it = map->find(id);
     101        12952 :         if (it == map->cend())
     102            0 :             return nullptr;
     103              : 
     104        12952 :         return std::static_pointer_cast<T>(it->second);
     105        12960 :     }
     106              : 
     107              :     template<class T = Account>
     108           83 :     std::vector<std::shared_ptr<T>> getAllAccounts() const
     109              :     {
     110           83 :         std::lock_guard lock(mutex_);
     111           83 :         std::vector<std::shared_ptr<T>> v;
     112              : 
     113           83 :         if (const auto map = getMap_<T>()) {
     114           68 :             v.reserve(map->size());
     115          217 :             for (const auto& it : *map)
     116          149 :                 v.emplace_back(std::static_pointer_cast<T>(it.second));
     117              :         }
     118          166 :         return v;
     119           83 :     }
     120              : 
     121              : private:
     122              :     mutable std::recursive_mutex mutex_ {};
     123              :     std::map<std::string, std::function<std::shared_ptr<Account>(const std::string&)>, std::less<>> generators_ {};
     124              :     std::map<std::string, AccountMap<Account>, std::less<>> accountMaps_ {};
     125              : 
     126              :     template<class T>
     127        13078 :     const AccountMap<Account>* getMap_() const
     128              :     {
     129        13078 :         const auto& itermap = accountMaps_.find(T::ACCOUNT_TYPE);
     130        13078 :         if (itermap != accountMaps_.cend())
     131        13020 :             return &itermap->second;
     132           58 :         return nullptr;
     133              :     }
     134              : };
     135              : 
     136              : template<>
     137              : bool AccountFactory::hasAccount(std::string_view id) const;
     138              : 
     139              : template<>
     140              : void AccountFactory::clear();
     141              : 
     142              : template<>
     143              : std::vector<std::shared_ptr<Account>> AccountFactory::getAllAccounts() const;
     144              : 
     145              : template<>
     146              : std::shared_ptr<Account> AccountFactory::getAccount(std::string_view accountId) const;
     147              : 
     148              : template<>
     149              : bool AccountFactory::empty() const;
     150              : 
     151              : template<>
     152              : std::size_t AccountFactory::accountCount() const;
     153              : 
     154              : } // namespace jami
        

Generated by: LCOV version 2.0-1