initial commit
This commit is contained in:
commit
4b60ece582
327 changed files with 28286 additions and 0 deletions
27
lib/wDispatcher/CMakeLists.txt
Normal file
27
lib/wDispatcher/CMakeLists.txt
Normal file
|
@ -0,0 +1,27 @@
|
|||
cmake_minimum_required(VERSION 2.8.12)
|
||||
project(wDispatcher)
|
||||
|
||||
find_package(Qt5Core REQUIRED)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
set(HEADERS
|
||||
dispatcher.h
|
||||
handler.h
|
||||
defaulthandler.h
|
||||
logger.h
|
||||
parentreporter.h
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
dispatcher.cpp
|
||||
handler.cpp
|
||||
defaulthandler.cpp
|
||||
logger.cpp
|
||||
parentreporter.cpp
|
||||
)
|
||||
|
||||
add_library(wDispatcher ${HEADERS} ${SOURCES})
|
||||
|
||||
target_link_libraries(wDispatcher Qt5::Core)
|
11
lib/wDispatcher/defaulthandler.cpp
Normal file
11
lib/wDispatcher/defaulthandler.cpp
Normal file
|
@ -0,0 +1,11 @@
|
|||
#include "defaulthandler.h"
|
||||
|
||||
W::DefaultHandler::DefaultHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
W::DefaultHandler::~DefaultHandler()
|
||||
{
|
||||
|
||||
}
|
18
lib/wDispatcher/defaulthandler.h
Normal file
18
lib/wDispatcher/defaulthandler.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
#ifndef DEFAULTHANDLER_H
|
||||
#define DEFAULTHANDLER_H
|
||||
|
||||
#include <wType/event.h>
|
||||
|
||||
namespace W
|
||||
{
|
||||
class DefaultHandler
|
||||
{
|
||||
public:
|
||||
DefaultHandler();
|
||||
virtual ~DefaultHandler();
|
||||
|
||||
virtual bool call(const W::Event& ev) const = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DEFAULTHANDLER_H
|
84
lib/wDispatcher/dispatcher.cpp
Normal file
84
lib/wDispatcher/dispatcher.cpp
Normal file
|
@ -0,0 +1,84 @@
|
|||
#include "dispatcher.h"
|
||||
|
||||
W::Dispatcher::Dispatcher()
|
||||
{}
|
||||
|
||||
W::Dispatcher::~Dispatcher()
|
||||
{}
|
||||
|
||||
void W::Dispatcher::pass(const W::Event& ev) const
|
||||
{
|
||||
|
||||
n_map::const_iterator itr;
|
||||
itr = nodes.find(ev.getDestination());
|
||||
|
||||
if (itr != nodes.end())
|
||||
{
|
||||
W::Order<W::Handler*>::const_iterator beg = itr->second.begin();
|
||||
W::Order<W::Handler*>::const_iterator end = itr->second.end();
|
||||
|
||||
std::list<W::Handler*> list(beg, end);
|
||||
std::list<W::Handler*>::const_iterator itr = list.begin();
|
||||
std::list<W::Handler*>::const_iterator tEnd = list.end();
|
||||
for (; itr != tEnd; ++itr) {
|
||||
(*itr)->pass(ev);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
d_order::const_iterator itr = defaultHandlers.begin();
|
||||
d_order::const_iterator end = defaultHandlers.end();
|
||||
|
||||
for (; itr != end; ++itr)
|
||||
{
|
||||
if ((*itr)->call(ev)){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void W::Dispatcher::registerHandler(W::Handler* dp)
|
||||
{
|
||||
n_map::iterator itr = nodes.find(dp->getAddress());
|
||||
if (itr == nodes.end()) {
|
||||
W::Order<W::Handler*> ord;
|
||||
itr = nodes.insert(std::make_pair(dp->getAddress(), ord)).first;
|
||||
}
|
||||
itr->second.push_back(dp);
|
||||
}
|
||||
|
||||
void W::Dispatcher::unregisterHandler(W::Handler* dp)
|
||||
{
|
||||
n_map::iterator itr = nodes.find(dp->getAddress());
|
||||
if (itr != nodes.end())
|
||||
{
|
||||
W::Order<W::Handler*>::const_iterator o_itr = itr->second.find(dp);
|
||||
if (o_itr != itr->second.end()) {
|
||||
itr->second.erase(dp);
|
||||
|
||||
if (itr->second.size() == 0) {
|
||||
nodes.erase(itr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw 5;//TODO exception;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw 5;//TODO exception;
|
||||
}
|
||||
}
|
||||
|
||||
void W::Dispatcher::registerDefaultHandler(W::DefaultHandler* dh)
|
||||
{
|
||||
defaultHandlers.push_back(dh);
|
||||
}
|
||||
|
||||
void W::Dispatcher::unregisterDefaultHandler(W::DefaultHandler* dh)
|
||||
{
|
||||
defaultHandlers.erase(dh);
|
||||
}
|
48
lib/wDispatcher/dispatcher.h
Normal file
48
lib/wDispatcher/dispatcher.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
#ifndef DISPATCHER_H
|
||||
#define DISPATCHER_H
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
|
||||
#include <wType/string.h>
|
||||
#include <wType/address.h>
|
||||
#include <wType/event.h>
|
||||
|
||||
#include <QtCore/QObject>
|
||||
|
||||
#include "handler.h"
|
||||
#include "defaulthandler.h"
|
||||
|
||||
#include <wContainer/order.h>
|
||||
|
||||
namespace W
|
||||
{
|
||||
class Dispatcher:
|
||||
public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Dispatcher();
|
||||
~Dispatcher();
|
||||
|
||||
void registerHandler(W::Handler* dp);
|
||||
void unregisterHandler(W::Handler* dp);
|
||||
|
||||
void registerDefaultHandler(W::DefaultHandler* dh);
|
||||
void unregisterDefaultHandler(W::DefaultHandler* dh);
|
||||
|
||||
public slots:
|
||||
void pass(const W::Event& ev) const;
|
||||
|
||||
protected:
|
||||
typedef std::map<W::Address, W::Order<W::Handler*>> n_map;
|
||||
typedef W::Order<W::DefaultHandler*> d_order;
|
||||
|
||||
n_map nodes;
|
||||
d_order defaultHandlers;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // DISPATCHER_H
|
17
lib/wDispatcher/handler.cpp
Normal file
17
lib/wDispatcher/handler.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include "handler.h"
|
||||
|
||||
W::Handler::Handler(const W::Address& p_rel_addr):
|
||||
address(p_rel_addr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
W::Handler::~Handler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
const W::Address& W::Handler::getAddress() const
|
||||
{
|
||||
return address;
|
||||
}
|
56
lib/wDispatcher/handler.h
Normal file
56
lib/wDispatcher/handler.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
#ifndef HANDLER_H
|
||||
#define HANDLER_H
|
||||
|
||||
#include "wType/address.h"
|
||||
#include "wType/event.h"
|
||||
|
||||
namespace W
|
||||
{
|
||||
template<typename InstanceType, typename MethodType>
|
||||
class ImplHandle;
|
||||
|
||||
class Handler
|
||||
{
|
||||
public:
|
||||
Handler(const Address& p_rel_addr);
|
||||
virtual ~Handler();
|
||||
|
||||
template<typename InstanceType, typename MethodType>
|
||||
static Handler* create(const Address& addr, InstanceType* inst, MethodType mth)
|
||||
{
|
||||
return new ImplHandle<InstanceType, MethodType>(addr, inst, mth);
|
||||
}
|
||||
|
||||
const W::Address& getAddress() const;
|
||||
|
||||
virtual void pass(const W::Event& ev) const = 0;
|
||||
|
||||
private:
|
||||
W::Address address;
|
||||
};
|
||||
|
||||
template<typename InstanceType, typename MethodType>
|
||||
class ImplHandle: public Handler
|
||||
{
|
||||
public:
|
||||
ImplHandle(const Address& p_rel_addr, InstanceType *p_inst, MethodType p_mth):
|
||||
Handler(p_rel_addr),
|
||||
inst(p_inst),
|
||||
mth(p_mth)
|
||||
{}
|
||||
|
||||
~ImplHandle() {}
|
||||
|
||||
void pass(const W::Event& ev) const
|
||||
{
|
||||
( ( *inst ).*mth )(ev);
|
||||
}
|
||||
|
||||
private:
|
||||
InstanceType* inst;
|
||||
MethodType mth;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // HANDLER_H
|
24
lib/wDispatcher/logger.cpp
Normal file
24
lib/wDispatcher/logger.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include "logger.h"
|
||||
|
||||
#include "iostream"
|
||||
|
||||
W::Logger::Logger():
|
||||
DefaultHandler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
W::Logger::~Logger()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool W::Logger::call(const W::Event& ev) const
|
||||
{
|
||||
std::cout << "Event went to default handler.\n";
|
||||
std::cout << "Destination: " << ev.getDestination().toString() << "\n";
|
||||
std::cout << "Data: " << ev.getData().toString();
|
||||
std::cout << std::endl;
|
||||
|
||||
return false;
|
||||
}
|
21
lib/wDispatcher/logger.h
Normal file
21
lib/wDispatcher/logger.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef LOGGER_H
|
||||
#define LOGGER_H
|
||||
|
||||
#include "defaulthandler.h"
|
||||
|
||||
#include <wType/event.h>
|
||||
|
||||
namespace W
|
||||
{
|
||||
class Logger:
|
||||
public DefaultHandler
|
||||
{
|
||||
public:
|
||||
Logger();
|
||||
~Logger();
|
||||
|
||||
bool call(const W::Event& ev) const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LOGGER_H
|
44
lib/wDispatcher/parentreporter.cpp
Normal file
44
lib/wDispatcher/parentreporter.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "parentreporter.h"
|
||||
|
||||
W::ParentReporter::ParentReporter():
|
||||
W::DefaultHandler(),
|
||||
handlers()
|
||||
{
|
||||
}
|
||||
|
||||
W::ParentReporter::~ParentReporter()
|
||||
{
|
||||
}
|
||||
|
||||
bool W::ParentReporter::call(const W::Event& ev) const
|
||||
{
|
||||
const W::Address& addr = ev.getDestination();
|
||||
std::map<int, W::Handler*> result;
|
||||
|
||||
Hmap::const_iterator itr = handlers.begin();
|
||||
Hmap::const_iterator end = handlers.end();
|
||||
|
||||
for (; itr != end; ++itr) { //need to find the closest parent to the event destination
|
||||
if (addr.begins(itr->first)) { //the closest parent has the longest address of those whose destinatiion begins with
|
||||
result.insert(std::make_pair(itr->first.size(), itr->second));
|
||||
}
|
||||
}
|
||||
if (result.size() > 0) {
|
||||
std::map<int, W::Handler*>::const_iterator itr = result.end();
|
||||
--itr;
|
||||
itr->second->pass(ev); //need to report only to the closest parent
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void W::ParentReporter::registerParent(const W::Address& address, W::Handler* handler)
|
||||
{
|
||||
Hmap::const_iterator itr = handlers.find(address);
|
||||
if (itr != handlers.end()) {
|
||||
throw 1;
|
||||
} else {
|
||||
handlers.insert(std::make_pair(address, handler));
|
||||
}
|
||||
}
|
25
lib/wDispatcher/parentreporter.h
Normal file
25
lib/wDispatcher/parentreporter.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef PARENTREPORTER_H
|
||||
#define PARENTREPORTER_H
|
||||
|
||||
#include "defaulthandler.h"
|
||||
#include "handler.h"
|
||||
#include <map>
|
||||
|
||||
namespace W {
|
||||
|
||||
class ParentReporter : public DefaultHandler
|
||||
{
|
||||
public:
|
||||
ParentReporter();
|
||||
~ParentReporter();
|
||||
|
||||
bool call(const W::Event& ev) const;
|
||||
void registerParent(const W::Address& address, W::Handler* handler);
|
||||
|
||||
private:
|
||||
typedef std::map<W::Address, W::Handler*> Hmap;
|
||||
Hmap handlers;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // PARENTREPORTER_H
|
Loading…
Add table
Add a link
Reference in a new issue