lundi 26 janvier 2015

Sharing member functions between objects

I'm designing an application that uses one class to manage a TCP connection and one to manage UI elements. The connection manager receives message strings and does minimal processing on them to determine their type. If they're of a known type, the connection manager will pass the strings along to the GUI manager so it can update the UI elements accordingly.


My challenge is this: if I don't want to include header files across objects, how do I permit access to the other object's public functions?


For example:



//message_types.h
typedef void(*MessageHandlerPointer)(std::string);

enum MessageTypes { info_type, time_type, command_type, reply_type,
inside_type, update_type, NUM_TYPES };
/////////////////////////////////////////////////////////////////////////
//ConnectionManager.h
class ConnectionManager
{
string hostname;
string port;
int connection_fd;

string message_types[NUM_TYPES];
string partial_message;

void process_message(string message);
MessageHandlerPointer message_handlers[NUM_TYPES];

public:
ConnectionManager(string hostname, string port);
~ConnectionManager();

int connect();
void disconnect();
void listen();
};
/////////////////////////////////////////////////////////////////////////
//ConnectionManager.cpp
ConnectionManager::ConnectionManager(string hostname, string port,
void (*message_handlers[NUM_TYPES])(string)):
hostname(hostname), port(port),
message_types { "i", "t", "c", "r", "I", "u" }
{
for(int i = 0; i < NUM_TYPES; i++)
{
this->message_handlers[i] = message_handlers[i];
}
}
/////////////////////////////////////////////////////////////////////////
//GuiManager.h
class GuiManager
{
void info_handler(string msg);
void time_handler(string msg);
void command_handler(string msg);
void reply_handler(string msg);
void inside_handler(string msg);
void update_handler(string msg);

public:
GuiManager();
~GuiManager();

MessageHandlerPointer message_handlers[NUM_TYPES];
};
/////////////////////////////////////////////////////////////////////////
//GuiManager.cpp
GuiManager::GuiManager()
{
message_handlers[info_code] = &info_handler;
message_handlers[time_code] = &time_handler;
message_handlers[command_code] = &command_handler;
message_handlers[reply_code] = &reply_handler;
message_handlers[inside_code] = &inside_handler;
message_handlers[update_code] = &update_handler;
}
/////////////////////////////////////////////////////////////////////////
//generic main.cpp
int main()
{
GuiManager gm();
ConnectionManager cm("host", "45123", gm.message_handlers);
}


But C++ doesn't want me to do that, and I vaguely understand why. Member functions aren't free functions. But I was hoping that I could perhaps make the functions somewhat owner- or class-agnostic?


Either way, my idea isn't going to get me where I want to be, so I'd be glad to hear someone else's impression of what the best solution would be.


Also, I recognize that I might be getting a little ridiculous for the sake of modularity in not letting the objects interface with one another directly. Am I missing the point / sacrificing simplicity for principle?


I'm fairly new to OO, so I'm interested in all of the details of any answer. =)


Aucun commentaire:

Enregistrer un commentaire