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-04-25 08:05:53 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
       5             :  *
       6             :  *  This program is free software; you can redistribute it and/or modify
       7             :  *  it under the terms of the GNU General Public License as published by
       8             :  *  the Free Software Foundation; either version 3 of the License, or
       9             :  *  (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, Boston, MA  02110-1301 USA.
      19             :  */
      20             : 
      21             : #pragma once
      22             : 
      23             : #include <string>
      24             : #include <map>
      25             : #include <vector>
      26             : #include <memory>
      27             : #include <mutex>
      28             : #include <utility>
      29             : #include <functional>
      30             : #include <ciso646>
      31             : 
      32             : namespace jami {
      33             : 
      34             : class Account;
      35             : class AccountGeneratorBase;
      36             : 
      37             : template<class T>
      38             : using AccountMap = std::map<std::string, std::shared_ptr<T>, std::less<>>;
      39             : 
      40             : class AccountFactory
      41             : {
      42             : public:
      43             :     static const std::string_view DEFAULT_ACCOUNT_TYPE;// = SIPAccount::ACCOUNT_TYPE;
      44             : 
      45             :     AccountFactory();
      46             : 
      47             :     bool isSupportedType(std::string_view accountType) const;
      48             : 
      49             :     std::shared_ptr<Account> createAccount(std::string_view accountType, const std::string& id);
      50             : 
      51             :     void removeAccount(Account& account);
      52             : 
      53             :     void removeAccount(std::string_view id);
      54             : 
      55             :     template<class T = Account>
      56           0 :     bool hasAccount(std::string_view id) const
      57             :     {
      58           0 :         std::lock_guard lk(mutex_);
      59             : 
      60           0 :         const auto map = getMap_<T>();
      61           0 :         return map and map->find(id) != map->cend();
      62           0 :     }
      63             : 
      64             :     template<class T = Account>
      65             :     void clear()
      66             :     {
      67             :         std::lock_guard lk(mutex_);
      68             : 
      69             :         auto map = getMap_<T>();
      70             :         if (!map)
      71             :             return;
      72             : 
      73             :         map->clear();
      74             :     }
      75             : 
      76             :     template<class T = Account>
      77             :     bool empty() const
      78             :     {
      79             :         std::lock_guard lock(mutex_);
      80             : 
      81             :         const auto map = getMap_<T>();
      82             :         return map and map->empty();
      83             :     }
      84             : 
      85             :     template<class T = Account>
      86             :     std::size_t accountCount() const
      87             :     {
      88             :         std::lock_guard lock(mutex_);
      89             : 
      90             :         const auto map = getMap_<T>();
      91             :         if (!map)
      92             :             return 0;
      93             : 
      94             :         return map->size();
      95             :     }
      96             : 
      97             :     template<class T = Account>
      98       14398 :     std::shared_ptr<T> getAccount(std::string_view id) const
      99             :     {
     100       14398 :         std::lock_guard lock(mutex_);
     101             : 
     102       14398 :         const auto map = getMap_<T>();
     103       14398 :         if (!map)
     104          12 :             return nullptr;
     105             : 
     106       14386 :         const auto& it = map->find(id);
     107       14386 :         if (it == map->cend())
     108           0 :             return nullptr;
     109             : 
     110       14386 :         return std::static_pointer_cast<T>(it->second);
     111       14398 :     }
     112             : 
     113             :     template<class T = Account>
     114          91 :     std::vector<std::shared_ptr<T>> getAllAccounts() const
     115             :     {
     116          91 :         std::lock_guard lock(mutex_);
     117          91 :         std::vector<std::shared_ptr<T>> v;
     118             : 
     119          91 :         if (const auto map = getMap_<T>()) {
     120          75 :             v.reserve(map->size());
     121         243 :             for (const auto& it : *map)
     122         168 :                 v.emplace_back(std::static_pointer_cast<T>(it.second));
     123             :         }
     124         182 :         return v;
     125          91 :     }
     126             : 
     127             : private:
     128             :     mutable std::recursive_mutex mutex_ {};
     129             :     std::map<std::string, std::function<std::shared_ptr<Account>(const std::string&)>, std::less<>> generators_ {};
     130             :     std::map<std::string, AccountMap<Account>, std::less<>> accountMaps_ {};
     131             : 
     132             :     template<class T>
     133       14489 :     const AccountMap<Account>* getMap_() const
     134             :     {
     135       14489 :         const auto& itermap = accountMaps_.find(T::ACCOUNT_TYPE);
     136       14489 :         if (itermap != accountMaps_.cend())
     137       14461 :             return &itermap->second;
     138          28 :         return nullptr;
     139             :     }
     140             : };
     141             : 
     142             : template<>
     143             : bool AccountFactory::hasAccount(std::string_view id) const;
     144             : 
     145             : template<>
     146             : void AccountFactory::clear();
     147             : 
     148             : template<>
     149             : std::vector<std::shared_ptr<Account>> AccountFactory::getAllAccounts() const;
     150             : 
     151             : template<>
     152             : std::shared_ptr<Account> AccountFactory::getAccount(std::string_view accountId) const;
     153             : 
     154             : template<>
     155             : bool AccountFactory::empty() const;
     156             : 
     157             : template<>
     158             : std::size_t AccountFactory::accountCount() const;
     159             : 
     160             : } // namespace jami

Generated by: LCOV version 1.14