Files
MapLibreParticleLab/include/mln/actor/optional_actor_ref.hpp
Bart Louwers 2a8ebc4906 core: rename mbgl directories to mln (#4511)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-08-22 20:50:14 +02:00

41 lines
996 B
C++

#pragma once
#include <mln/actor/actor_ref.hpp>
namespace mln {
class TaggedScheduler;
template <class Object>
class OptionalActorRef {
public:
OptionalActorRef() = default;
OptionalActorRef(bool syncRef, Object& object, std::weak_ptr<Mailbox> weakMailbox) {
if (syncRef) {
objectRef = &object;
} else {
actorRef = std::make_optional<ActorRef<Object>>(object, weakMailbox);
}
}
explicit OptionalActorRef(Object& object) { objectRef = &object; }
explicit OptionalActorRef(ActorRef<Object> actorRef_) { actorRef = actorRef_; }
template <typename Fn, class... Args>
void invoke(Fn fn, Args&&... args) const {
if (objectRef) {
(objectRef->*fn)(std::forward<Args>(args)...);
} else if (actorRef) {
actorRef->invoke(fn, std::forward<Args>(args)...);
}
}
private:
Object* objectRef = nullptr;
std::optional<ActorRef<Object>> actorRef;
};
} // namespace mln