~aleteoryx/muditaos

ref: f724d9074ba9cfd662b18d7b9de7fcb18548824b muditaos/module-sys/Service/ServiceCreator.hpp -rw-r--r-- 1.5 KiB
f724d907 — Adam Dobrowolski [EGD-5701] Added DOM dump to harness 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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 "ServiceManifest.hpp"

#include <memory>

namespace sys
{
    class Service;

    class BaseServiceCreator
    {
      public:
        explicit BaseServiceCreator(ServiceManifest &&manifest) noexcept : manifest{std::move(manifest)}
        {}
        virtual ~BaseServiceCreator() noexcept = default;

        [[nodiscard]] virtual std::shared_ptr<Service> create() const = 0;

        [[nodiscard]] auto getName() const noexcept -> const ServiceManifest::ServiceName &
        {
            return manifest.name;
        }

        [[nodiscard]] auto getDependencies() const noexcept -> const std::vector<ServiceManifest::ServiceName> &
        {
            return manifest.dependencies;
        }

        [[nodiscard]] auto getStartTimeout() const noexcept -> ServiceManifest::Timeout
        {
            return manifest.timeout;
        }

      private:
        ServiceManifest manifest;
    };

    template <typename T> class ServiceCreator : public BaseServiceCreator
    {
      public:
        using BaseServiceCreator::BaseServiceCreator;

        [[nodiscard]] auto create() const -> std::shared_ptr<Service> override
        {
            return std::make_shared<T>();
        }
    };

    template <typename T> std::unique_ptr<BaseServiceCreator> CreatorFor() noexcept
    {
        return std::make_unique<ServiceCreator<T>>(ManifestOf<T>());
    }
} // namespace sys