Ring Daemon 16.0.0
Loading...
Searching...
No Matches
account_factory.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2004-2025 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
28namespace jami {
29
30class Account;
32
33template<class T>
34using AccountMap = std::map<std::string, std::shared_ptr<T>, std::less<>>;
35
37{
38public:
39 static const std::string_view DEFAULT_ACCOUNT_TYPE;// = SIPAccount::ACCOUNT_TYPE;
40
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
48
49 void removeAccount(std::string_view id);
50
51 template<class T = Account>
52 bool hasAccount(std::string_view id) const
53 {
54 std::lock_guard lk(mutex_);
55
56 const auto map = getMap_<T>();
57 return map and map->find(id) != map->cend();
58 }
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 std::shared_ptr<T> getAccount(std::string_view id) const
95 {
96 std::lock_guard lock(mutex_);
97
98 const auto map = getMap_<T>();
99 if (!map)
100 return nullptr;
101
102 const auto& it = map->find(id);
103 if (it == map->cend())
104 return nullptr;
105
106 return std::static_pointer_cast<T>(it->second);
107 }
108
109 template<class T = Account>
110 std::vector<std::shared_ptr<T>> getAllAccounts() const
111 {
112 std::lock_guard lock(mutex_);
113 std::vector<std::shared_ptr<T>> v;
114
115 if (const auto map = getMap_<T>()) {
116 v.reserve(map->size());
117 for (const auto& it : *map)
118 v.emplace_back(std::static_pointer_cast<T>(it.second));
119 }
120 return v;
121 }
122
123private:
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 const AccountMap<Account>* getMap_() const
130 {
131 const auto& itermap = accountMaps_.find(T::ACCOUNT_TYPE);
132 if (itermap != accountMaps_.cend())
133 return &itermap->second;
134 return nullptr;
135 }
136};
137
138template<>
139bool AccountFactory::hasAccount(std::string_view id) const;
140
141template<>
143
144template<>
145std::vector<std::shared_ptr<Account>> AccountFactory::getAllAccounts() const;
146
147template<>
148std::shared_ptr<Account> AccountFactory::getAccount(std::string_view accountId) const;
149
150template<>
152
153template<>
155
156} // namespace jami
std::shared_ptr< T > getAccount(std::string_view id) const
std::vector< std::shared_ptr< T > > getAllAccounts() const
bool isSupportedType(std::string_view accountType) const
static const std::string_view DEFAULT_ACCOUNT_TYPE
std::size_t accountCount() const
bool hasAccount(std::string_view id) const
void removeAccount(Account &account)
std::shared_ptr< Account > createAccount(std::string_view accountType, const std::string &id)
void emitSignal(Args... args)
Definition ring_signal.h:64
std::map< std::string, std::shared_ptr< T >, std::less<> > AccountMap