Closed Lomadriel closed 9 years ago
I assume that the system is pretty similar to the InputManager part, so I would like to signal exactly the same thing that I said for issue https://github.com/Mirai-Team/mirai-project/issues/43 about parameters.
nop. It's no the same thing. AddListener(string, function). Broadcast(string, param...)
Added with parameter in EventManager2. Need a overhaul but it's functional.
Exemple not compiling on my computer :
[ 77%] Built target EncryptionExample
[ 80%] Building CXX object examples/eventManager/CMakeFiles/EventManagerExample.dir/src/main.cpp.o
In file included from /home/benoit/Dev/mirai-project/include/MiraiProject/eventManager/EventManager.hpp:111:0,
from /home/benoit/Dev/mirai-project/examples/eventManager/src/main.cpp:28:
/home/benoit/Dev/mirai-project/include/MiraiProject/eventManager/EventManager.tpp: In member function ‘T mp::EventManager::broadcast(std::string, Args ...) [with T = int; Args = {int}; std::string = std::basic_string<char>]’:
/home/benoit/Dev/mirai-project/include/MiraiProject/eventManager/EventManager.tpp:52:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Hum, you have only quoted a warning. So I don't know why the compilation fails.
But I want to update the example which is quite useless.
Done
Uh, yes, It was just a warning and the executable was produced in fact. I didn't seen it at the moment.
The new example is better, but you should define Player class in another file like Monster.
About the EventManager class :
There is probably a way to replace
static std::shared_ptr<EventManager> instance_;
by
static EventManager instance_;
Here https://github.com/Mirai-Team/mirai-project/blob/EventManager2/include/MiraiProject/eventManager/EventManager.hpp#L105
Then, you simply return a reference here : https://github.com/Mirai-Team/mirai-project/blob/EventManager2/include/MiraiProject/eventManager/EventManager.hpp#L47
Like in WindowManager class : https://github.com/Mirai-Team/mirai-project/blob/EventManager2/include/MiraiProject/util/WindowManager.hpp
It is better to not use pointers if possible.
In this case we can't, it's a singleton pattern. Indeed there is a singleton pattern with reference but unsafe so I think it's better to use pointer
This message replace the previous one. Indeed we can. But I don't if gcc supports it. I check it and if gcc support it. I do it.
I see no reason this way could works only for the very case of the WindowManager and not for this one. And why it would be unsafe to do it here ?
WindowManager isn't singleton but I have to find a way to keep this class singleton and thread-safe.
But you can still return a reference. Using the shared_ptr is thread-safe ?
If I return a reference like you do in WindowManager class, this class will no longer be singleton.
A reference isn't a copy, but an hidden pointer to the same instance. So I don't see why this is no longer a singleton.
I know that but it's a little bit more complicated. I can do it like in WindowManager but if I do it the class is "singleton" but absolutely not thread-safe.
Allows you to run a function in a class according to an action from another class. (Listener and Notifier system)