Ring Daemon
Loading...
Searching...
No Matches
global_instance.h
Go to the documentation of this file.
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
18#pragma once
19
20#include <memory>
21#include <mutex>
22
23namespace jami {
24
36template<class T, signed MaxRespawn = -1>
37std::shared_ptr<T>
39{
40 static std::recursive_mutex mutex; // recursive as instance calls recursively
41 static std::weak_ptr<T> wlink;
42
43 std::unique_lock lock(mutex);
44
45 if (wlink.expired()) {
46 static signed counter {MaxRespawn};
47 if (not counter)
48 return nullptr;
49 auto link = std::make_shared<T>();
50 wlink = link;
51 if (counter > 0)
52 --counter;
53 return link;
54 }
55
56 return wlink.lock();
57}
58
59} // namespace jami
void emitSignal(Args... args)
Definition jami_signal.h:64
std::shared_ptr< T > getGlobalInstance()
Return a shared pointer on an auto-generated global instance of class T.