source: pacpusframework/branches/2.0-beta1/include/Pacpus/kernel/inputOutputBase.h@ 135

Last change on this file since 135 was 110, checked in by morasjul, 11 years ago

Beta-2 : Fix CMakeList (link), remove useless code and add improvement

  • Property svn:executable set to *
File size: 4.1 KB
Line 
1#ifndef IN_OUT_BASE_H
2#define IN_OUT_BASE_H
3
4#include <Pacpus/kernel/pacpus.h>
5#include <Pacpus/kernel/ConnectionBase.h>
6#include <Pacpus/kernel/PacpusEvent.h>
7
8#include <QApplication>
9#include <typeinfo>
10#include <QList>
11#include <QStringList>
12
13namespace pacpus {
14
15class ComponentBase;
16
17class PACPUSLIB_API AbstractInterface : public QObject
18{
19 Q_OBJECT
20protected:
21 AbstractInterface(QString name, ComponentBase * component, QObject * parent = 0):_name(name),_component(component),QObject(parent) {}
22 ~AbstractInterface(){}
23public:
24 QString getSignature();
25 QString getName() {return _name;}
26 virtual QString getDataType() = 0;
27 ComponentBase * getComponent() {return _component;}
28
29 void addConnection(ConnectionBase connection) { _connection.append(connection);}
30 bool removeConnection(ConnectionBase connection) { return _connection.removeOne(connection);}
31
32 bool hasConnection() { return _connection.size() > 0;}
33
34protected:
35 QString _name;
36 ComponentBase * _component;
37 QList<ConnectionBase> _connection;
38};
39
40class PACPUSLIB_API InputInterfaceBase : public AbstractInterface
41{
42 Q_OBJECT
43protected:
44 InputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0):AbstractInterface(name,component,parent) {}
45
46public:
47 //InputInterfaceBase(QString name, ComponentBase * component,int a, QObject * parent = 0):AbstractInterface(name,component,parent) {} // TODO add ctor with function pointer
48
49 enum ReadingMode {
50 NeverSkip,
51 TimeBounded,
52 GetLast
53 };
54
55 virtual ~InputInterfaceBase(){}
56
57 virtual void customEvent(QEvent* e) {
58
59 //if(event->type())
60 //TODO get event Type anf call callback function
61
62 PacpusEvent * event = dynamic_cast<PacpusEvent *>(e);
63 QByteArray buf;
64 QDataStream out(&buf,QIODevice::WriteOnly);
65 event->streamOut(out);
66 // Callback QByteArray
67 }
68
69 void setReadingMode(ReadingMode mode) { readingMode_ = mode;}
70 virtual PacpusEvent* getEventTemplate() {return new PacpusEvent(GENERIC_EVENT);} // TODO check ??
71protected:
72 ReadingMode readingMode_;
73private:
74 // metode(QByteArray)
75
76 //QQueue jobQueue_;
77};
78
79class PACPUSLIB_API OutputInterfaceBase: public AbstractInterface
80{
81 Q_OBJECT
82
83public:
84 OutputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0):AbstractInterface(name,component,parent) {}
85
86 virtual ~OutputInterfaceBase(){}
87
88 QStringList getInputConnectedList() {
89 QStringList list;
90 for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it)
91 list.append(it->getInterface()->getName());
92 return list;
93 }
94
95 void send(/*const*/ QByteArray & data) {
96 // TODO check at least one Typed connection
97
98 for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it){
99 QDataStream in(&data,QIODevice::ReadOnly);
100 PacpusEvent* event = dynamic_cast<InputInterfaceBase*>(_connection.at(0).getInterface())->getEventTemplate();
101 event->streamIn(in);
102 QApplication::postEvent(it->getInterface(),event,it->getPriority());
103 }
104
105
106 }
107
108};
109
110static bool connectInterface(OutputInterfaceBase* out, InputInterfaceBase * in, int priority, InputInterfaceBase::ReadingMode mode = InputInterfaceBase::GetLast)
111{
112 if(out->getDataType() == in->getDataType() || out->getDataType() == QString(typeid(QByteArray).name()) || in->getDataType() == QString(typeid(QByteArray).name())) {
113
114 // Add connection
115 out->addConnection(ConnectionBase(in,priority)); // TODO make connect function
116 in->addConnection(ConnectionBase(out,priority));
117 in->setReadingMode(mode);
118 //LOG_INFO("connection : Output " << out->getSignature() << " => Input " << in->getSignature());
119 return true;
120 } else {
121 //LOG_WARN("connecting " << out->getSignature() << ":" << out->getDataType() << " to " << in->getSignature() << ":" << in->getDataType() << " failled : DataType incompatible " << QString(typeid(QByteArray).name()));
122 return false;
123 }
124}
125
126} // namespace pacpus
127
128#endif // IN_OUT_BASE_H
Note: See TracBrowser for help on using the repository browser.