CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
NetworkAction.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
12
15
16#include <any>
17
18namespace CeresEngine {
19
22 virtual ~AbstractNetworkAction() = default;
23
26 [[nodiscard]] virtual Any dispatchAny(Entity entity) const = 0;
27 };
28
32 template<typename A>
34 : public AbstractNetworkAction,
35 public EntityAction<NetworkAction<A>, std::conditional_t<std::is_same_v<typename A::ReturnType, void>, Async<>, Async<typename A::ReturnType>>> {
37 using ReturnValueType = typename A::ReturnType;
38
42
47 template<typename... Args> explicit NetworkAction(Args&&... args);
48
50 [[nodiscard]] Any dispatchAny(Entity entity) const final;
51
54 [[nodiscard]] ReturnValueType dispatch(Entity entity) const;
55 };
56
59 template<typename A> constexpr bool isNetworkAction = std::is_base_of<AbstractNetworkAction, A>::value;
60
63 template<typename... As> constexpr bool areNetworkActions = (isNetworkAction<As> && ...);
64
65 // ---------------------------------------------------------------------------------------------
66
67 template<typename A> template<typename... Args> NetworkAction<A>::NetworkAction(Args&&... args) : action(std::forward<Args>(args)...) {}
68
69 template<typename A> Any NetworkAction<A>::dispatchAny(Entity entity) const {
70 if constexpr(std::is_same_v<ReturnValueType, void>) {
71 entity.act<A>(action);
72 return {};
73 } else {
74 return entity.act<A>(action);
75 }
76 }
77
78 template<typename A> typename NetworkAction<A>::ReturnValueType NetworkAction<A>::dispatch(Entity entity) const { return entity.act<A>(entity); }
79
80} // namespace CeresEngine
Definition Any.hpp:13
The base entity class.
Definition Entity.hpp:41
decltype(auto) act(Args &&... args) const
Orders the entity to act on the action A.
Definition Application.hpp:19
constexpr bool areNetworkActions
A trait that checks if the types As are all actions.
Definition NetworkAction.hpp:63
constexpr bool isNetworkAction
A trait that checks if the type A is a network action.
Definition NetworkAction.hpp:59
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
Definition NetworkAction.hpp:20
virtual ~AbstractNetworkAction()=default
Destroys the AbstractNetworkAction instance.
virtual Any dispatchAny(Entity entity) const =0
Dispatches the action to the given entity.
A class that must be inherited by concrete action types.
Definition Action.hpp:33
The NetworkAction is a special type of action that can be performed remotely on the server.
Definition NetworkAction.hpp:35
ReturnValueType dispatch(Entity entity) const
Dispatches the action to the given entity.
Definition NetworkAction.hpp:78
A action
The wrapped action type.
Definition NetworkAction.hpp:41
typename A::ReturnType ReturnValueType
The type that must be returned by action handlers.
Definition NetworkAction.hpp:37
NetworkAction(Args &&... args)
Creates a new NetworkAction.
Definition NetworkAction.hpp:67
Any dispatchAny(Entity entity) const final
Dispatches the action to the given entity.
Definition NetworkAction.hpp:69