Permalink
Browse files

Make the entity message enum typesafe to avoid nameclashes with e.g. …

…socket.h
  • Loading branch information...
t4im committed Nov 26, 2018
1 parent cda62d6 commit 15ed6dd7dbb81a0c35f6c7c64e8959e96920f852
Showing with 7 additions and 10 deletions.
  1. +0 −3 CBSE.py
  2. +4 −4 templates/Backend.cpp
  3. +3 −3 templates/Backend.h
@@ -70,9 +70,6 @@ def get_name(self):
def get_num_args(self):
return len(self.args)

def get_enum_name(self):
return 'MSG_' + self.name.upper()

def get_handler_name(self):
return 'Handle' + self.name

@@ -64,8 +64,8 @@ Entity::Entity(const MessageHandler *messageHandlers, const int* componentOffset
{}

// Base entity's message dispatcher.
bool Entity::SendMessage(int msg, const void* data) {
MessageHandler handler = messageHandlers[msg];
bool Entity::SendMessage(EntityMessage msg, const void* data) {
MessageHandler handler = messageHandlers[static_cast<int>(msg)];
if (handler) {
handler(this, data);
return true;
@@ -80,10 +80,10 @@ bool Entity::SendMessage(int msg, const void* data) {

bool Entity::{{message.name}}({{message.get_function_args()}}) {
{% if message.get_num_args() == 0 %}
return SendMessage({{message.get_enum_name()}}, nullptr);
return SendMessage(EntityMessage::{{message.get_name()}}, nullptr);
{% else %}
{{message.get_tuple_type()}} data({{message.get_args_names()}});
return SendMessage({{message.get_enum_name()}}, &data);
return SendMessage(EntityMessage::{{message.get_name()}}, &data);
{% endif %}
}
{% endfor %}
@@ -69,9 +69,9 @@
// /////////// //

//* An enum of message IDs used to index the vtable.
enum {
enum class EntityMessage {
{% for message in messages %}
{{message.get_enum_name()}},
{{message.get_name()}},
{% endfor %}
};

@@ -172,7 +172,7 @@ class Entity {
* @brief Generic message dispatcher.
* @note Should not be called directly, use message helpers instead.
*/
bool SendMessage(int msg, const void* data);
bool SendMessage(EntityMessage msg, const void* data);

public:
// ///////////////////// //

0 comments on commit 15ed6dd

Please sign in to comment.