Files
MuditaOS/module-sys/Service/ServiceProxy.hpp
Adam Dobrowolski b38adceced [EGD-6019] Minimum settings ownership lifetime fixups
added weakptr link to settings and checks
    it wont crash on deinitialized setings now
Pseuto UT are passing
Added:
    - deregistration on Settings destrution
    - weak referencing of Service to not crash Settings on missuse
    - Proxy as initialization parameter to Settings
Unused code removed
Enabled tests to be written for Settings
Removed dependency from freertos in test global file
EntryPath tests updated and compilation slimed
2021-05-18 13:25:54 +02:00

47 lines
1.3 KiB
C++

// 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 <memory>
#include <stdexcept>
#include <utility>
namespace sys
{
class Service;
}
namespace service
{
/// Proxy class for classes using Service raw pointer
/// - initialized in InitHandler
/// - provides weak bounding with Service (to avoid infinite shared ptr loop)
/// - used to provide Class <-> System interface
/// - invalid ServiceProxy will throw runtime error instead of crash on invalid Service* ptr
class ServiceProxy
{
private:
std::weak_ptr<sys::Service> service;
protected:
[[nodiscard]] std::shared_ptr<sys::Service> getService()
{
if (auto val = service.lock(); val != nullptr) {
return val;
}
throw std::runtime_error("no service");
}
public:
explicit ServiceProxy(std::weak_ptr<sys::Service> service) : service(std::move(service))
{}
ServiceProxy() = default;
virtual ~ServiceProxy() = default;
[[nodiscard]] bool isValid() const noexcept
{
return !service.expired();
}
};
} // namespace service