Created
August 1, 2011 12:16
-
-
Save BlockoS/1118028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef NETWORK_H | |
#define NETWORK_H | |
#include "Config.h" | |
// [todo] enum for error? | |
class Network | |
{ | |
public: | |
class Backbone | |
{ | |
public: | |
virtual ~Backbone() {} | |
virtual int Init () = 0; | |
virtual int Deinit() = 0; | |
// [todo] error translation ? | |
}; | |
protected: | |
class ReceiveCallback | |
{ | |
public: | |
virtual ~ReceiveCallback() {} | |
virtual int operator() (const uint8_t* data, size_t len) = 0; | |
}; | |
template <class T> | |
class ReceiveCallbackImpl : public ReceiveCallback | |
{ | |
public: | |
typedef int (T::*TProc) (const uint8_t*, size_t); | |
ReceiveCallbackImpl(T* instance, TProc method); | |
~ReceiveCallbackImpl() {} | |
int operator() (const uint8_t data, size_t len); | |
private: | |
T* m_instance; | |
TProc *m_method; | |
}; | |
public: | |
Network(); | |
~Network(); | |
int Connect(const char* address, long int port); | |
int Disconnect(); | |
int Send(const uint8_t* data, size_t len); | |
template <class T> | |
int RegisterReceiveCallback(T *instance, int (T::*method) (const uint8_t* data, size_t len)); | |
protected: | |
Backbone *m_core; | |
ReceiveCallback *m_recv; | |
}; | |
template <class T> | |
Network::ReceiveCallbackImpl<T>::ReceiveCallbackImpl(T* instance, TProc method) : | |
m_instance(instance), | |
m_method(method) | |
{} | |
template <class T> | |
int Network::ReceiveCallbackImpl<T>::operator() (const uint8_t data, size_t len) | |
{ | |
return instance->(*method)(data, len); | |
} | |
#endif // NETWORK_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment