~aleteoryx/muditaos

ref: 1156320af5b52e2ed45c79ebb71d16601bbaecda muditaos/module-bsp/hal/GenericFactory.hpp -rw-r--r-- 717 bytes
1156320a — Adam Dobrowolski [EGD-7502] TimeService Alarms get returns total elements count 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <module-os/CriticalSectionGuard.hpp>

#include <memory>

namespace hal::impl
{
    template <typename TDerived, typename TInterface, typename... Args>
    inline std::shared_ptr<TInterface> factory(Args... args)
    {
        const auto criticalSection = cpp_freertos::CriticalSectionGuard{};
        static std::weak_ptr<TDerived> singleton;
        auto inst = singleton.lock();
        if (not inst) {
            inst      = std::make_shared<TDerived>(args...);
            singleton = inst;
        }
        return inst;
    }
} // namespace hal::impl