3#include <Core/Utils/Singleton.hpp>
4#include <Core/Utils/StdUtils.hpp>
5#include <Engine/RaEngine.hpp>
6#include <Engine/Scene/Component.hpp>
12#include <unordered_map>
60 struct CallbackTypes<
std::shared_ptr<T>> {
79 inline std::size_t operator()(
const Key& k )
const;
83 struct CallbackBase {};
85 struct GetterCallback :
public CallbackBase {
86 typename CallbackTypes<T>::Getter m_cb;
89 struct SetterCallback :
public CallbackBase {
90 typename CallbackTypes<T>::Setter m_cb;
93 struct RwCallback :
public CallbackBase {
94 typename CallbackTypes<T>::ReadWrite m_cb;
101 ComponentMessenger() =
default;
107 template <
typename ReturnType>
108 inline typename CallbackTypes<ReturnType>::Getter getterCallback(
const Entity* entity,
111 template <
typename ReturnType>
112 inline typename CallbackTypes<ReturnType>::ReadWrite rwCallback(
const Entity* entity,
115 template <
typename ReturnType>
116 inline typename CallbackTypes<ReturnType>::Setter setterCallback(
const Entity* entity,
126 template <
typename ReturnType>
127 inline const ReturnType& get(
const Entity* entity,
const std::string&
id );
133 template <
typename ReturnType>
134 inline bool canGet(
const Entity* entity,
const std::string&
id );
136 template <
typename ReturnType>
137 inline bool canSet(
const Entity* entity,
const std::string&
id );
139 template <
typename ReturnType>
140 inline bool canRw(
const Entity* entity,
const std::string&
id );
146 template <
typename ReturnType>
147 inline void registerOutput(
const Entity* entity,
150 const typename CallbackTypes<ReturnType>::Getter& cb );
152 template <
typename ReturnType>
153 inline void registerReadWrite(
const Entity* entity,
156 const typename CallbackTypes<ReturnType>::ReadWrite& cb );
158 template <
typename ReturnType>
159 inline void registerInput(
const Entity* entity,
162 const typename CallbackTypes<ReturnType>::Setter& cb );
165 void unregisterAll(
const Entity* entity, Component* component );
169 EntityMap m_entityGetLists;
170 EntityMap m_entitySetLists;
171 EntityMap m_entityRwLists;
174inline std::size_t ComponentMessenger::HashFunc::operator()(
const Key& k )
const {
175 return Core::Utils::hash( k );
178template <
typename ReturnType>
180ComponentMessenger::getterCallback(
const Entity* entity,
const std::string&
id ) {
181 CORE_ASSERT( canGet<ReturnType>( entity,
id ),
"Unregistered callback" );
182 return static_cast<GetterCallback<ReturnType>*
>(
183 m_entityGetLists[entity][Key(
id,
std::type_index(
typeid( ReturnType ) ) )].get() )
187template <
typename ReturnType>
189ComponentMessenger::rwCallback(
const Entity* entity,
const std::string&
id ) {
190 CORE_ASSERT( canRw<ReturnType>( entity,
id ),
"Unregistered callback" );
191 return static_cast<RwCallback<ReturnType>*
>(
192 m_entityRwLists[entity][Key(
id,
std::type_index(
typeid( ReturnType ) ) )].get() )
196template <
typename ReturnType>
198ComponentMessenger::setterCallback(
const Entity* entity,
const std::string&
id ) {
199 CORE_ASSERT( canSet<ReturnType>( entity,
id ),
"Unregistered callback" );
200 return static_cast<SetterCallback<ReturnType>*
>(
201 m_entitySetLists[entity][Key(
id,
std::type_index(
typeid( ReturnType ) ) )].get() )
205template <
typename ReturnType>
206inline const ReturnType& ComponentMessenger::get(
const Entity* entity,
const std::string&
id ) {
217template <
typename ReturnType>
218inline bool ComponentMessenger::canGet(
const Entity* entity,
const std::string&
id ) {
220 const auto& entityListPos = m_entityGetLists.
find( entity );
221 if ( entityListPos == m_entityGetLists.
end() )
225 const CallbackMap& entityList = entityListPos->second;
229 const auto& callbackEntry = entityList.find( key );
230 const bool found = ( callbackEntry != entityList.end() );
234template <
typename ReturnType>
235inline bool ComponentMessenger::canSet(
const Entity* entity,
const std::string&
id ) {
237 auto entityListPos = m_entitySetLists.
find( entity );
238 if ( entityListPos == m_entitySetLists.
end() )
242 const CallbackMap& entityList = entityListPos->second;
246 const auto& callbackEntry = entityList.find( key );
247 const bool found = ( callbackEntry != entityList.end() );
251template <
typename ReturnType>
252inline bool ComponentMessenger::canRw(
const Entity* entity,
const std::string&
id ) {
254 const auto& entityListPos = m_entityRwLists.
find( entity );
255 if ( entityListPos == m_entityRwLists.
end() )
259 const CallbackMap& entityList = entityListPos->second;
263 const auto& callbackEntry = entityList.find( key );
264 const bool found = ( callbackEntry != entityList.end() );
268template <
typename ReturnType>
270ComponentMessenger::registerOutput(
const Entity* entity,
274 CORE_ASSERT( entity && comp->getEntity() == entity,
"Component not added to entity" );
277 CallbackMap& entityList = m_entityGetLists[entity];
280 CORE_ASSERT( entityList.find( key ) == entityList.end(),
281 "Output function already registered for " +
id );
283 GetterCallback<ReturnType>* getter =
new GetterCallback<ReturnType>();
285 entityList[key].reset( getter );
288template <
typename ReturnType>
290ComponentMessenger::registerReadWrite(
const Entity* entity,
294 CORE_ASSERT( entity && comp->getEntity() == entity,
"Component not added to entity" );
297 CallbackMap& entityList = m_entityRwLists[entity];
300 CORE_ASSERT( entityList.find( key ) == entityList.end(),
301 "Rw function already registered for " +
id );
303 RwCallback<ReturnType>* rw =
new RwCallback<ReturnType>();
305 entityList[key].reset( rw );
308template <
typename ReturnType>
310ComponentMessenger::registerInput(
const Entity* entity,
314 CORE_ASSERT( entity && comp->getEntity() == entity,
"Component not added to entity" );
317 CallbackMap& entityList = m_entitySetLists[entity];
320 CORE_ASSERT( entityList.find( key ) == entityList.end(),
321 "Input function already registered for " +
id );
323 SetterCallback<ReturnType>* setter =
new SetterCallback<ReturnType>();
325 entityList[key].reset( setter );
hepler function to manage enum as underlying types in VariableSet
This describes the function pointers accepted for each type.
std::function< void(const T *)> Setter
Function pointer for a setter function.
std::function< T *(void)> ReadWrite
Function pointer for a read/write getter.
std::function< const T *(void)> Getter
Function pointer for a getter function.
static const T & getHelper(const Getter &g)
Calls the callback and retrieves the object.