LCOV - code coverage report
Current view: top level - foo/src - account_factory.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 29 29 100.0 %
Date: 2026-02-28 10:41:24 Functions: 7 7 100.0 %

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

Generated by: LCOV version 1.14