source: pacpusframework/branches/2.0-beta1/include/Pacpus/kernel/InputOutputBase.h@ 159

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

Somme cleaning in InputOutputInterface.h and InputOutputBase.h

  • Property svn:executable set to *
File size: 5.2 KB
RevLine 
[89]1#ifndef IN_OUT_BASE_H
2#define IN_OUT_BASE_H
3
[153]4#include <Pacpus/kernel/Log.h>
[95]5#include <Pacpus/kernel/pacpus.h>
[89]6#include <Pacpus/kernel/ConnectionBase.h>
[96]7#include <Pacpus/kernel/PacpusEvent.h>
[89]8
[96]9#include <QApplication>
[89]10#include <QList>
[148]11#include <QString>
[89]12#include <QStringList>
[148]13#include <typeinfo>
[89]14
15namespace pacpus {
16
17class ComponentBase;
18
[148]19class PACPUSLIB_API AbstractInterface
20 : public QObject
[89]21{
22 Q_OBJECT
[148]23
[89]24protected:
[148]25 AbstractInterface(QString name, ComponentBase * component, QObject * parent = 0)
26 : m_name(name)
27 , m_component(component)
28 , QObject(parent)
[153]29 {
30 LOG_DEBUG("constructing abstract connection '" << getName() << "'");
31 }
[148]32
33 ~AbstractInterface()
34 {}
35
[89]36public:
37 QString getSignature();
[148]38 QString getName()
39 {
40 return m_name;
41 }
42
[89]43 virtual QString getDataType() = 0;
[148]44
45 ComponentBase * getComponent()
46 {
47 return m_component;
48 }
[89]49
[148]50 void addConnection(ConnectionBase connection)
51 {
52 m_connections.append(connection);
53 }
[96]54
[148]55 bool removeConnection(ConnectionBase connection)
56 {
57 return m_connections.removeOne(connection);
58 }
[100]59
[148]60 bool hasConnection()
61 {
62 return m_connections.size() > 0;
63 }
64
[89]65protected:
[148]66 QList<ConnectionBase> & connections()
67 {
68 return m_connections;
69 }
70
71 const QList<ConnectionBase> & connections() const
72 {
73 return m_connections;
74 }
75
76 QString name()
77 {
78 return m_name;
79 }
80
81 ComponentBase * component()
82 {
83 return m_component;
84 }
85
86 const ComponentBase * component() const
87 {
88 return m_component;
89 }
90
91private:
92 QString m_name;
93 ComponentBase * m_component;
94 QList<ConnectionBase> m_connections;
[89]95};
96
[148]97class PACPUSLIB_API InputInterfaceBase
98 : public AbstractInterface
[89]99{
100 Q_OBJECT
[152]101
[96]102protected:
[148]103 InputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0)
104 : AbstractInterface(name, component, parent)
105 {}
[96]106
[89]107public:
[96]108 //InputInterfaceBase(QString name, ComponentBase * component,int a, QObject * parent = 0):AbstractInterface(name,component,parent) {} // TODO add ctor with function pointer
109
[148]110 enum ReadingMode {
[102]111 NeverSkip,
112 TimeBounded,
113 GetLast
114 };
115
[148]116 virtual ~InputInterfaceBase()
117 {}
[89]118
[159]119 // TODO for serealization prupose (not yet implemented !!!)
[148]120 virtual void customEvent(QEvent* e)
121 {
[96]122 //if(event->type())
[148]123 //TODO get event Type anf call callback function
[96]124
[148]125 PacpusEvent * event = dynamic_cast<PacpusEvent *>(e);
126 QByteArray buf;
127 QDataStream out(&buf, QIODevice::WriteOnly);
128 event->streamOut(out);
129 // Callback QByteArray
[96]130 }
131
[148]132 ReadingMode & readingMode()
133 {
134 return m_readingMode;
135 }
136
137 const ReadingMode & readingMode() const
138 {
139 return m_readingMode;
140 }
141
142 void setReadingMode(ReadingMode mode)
143 {
144 m_readingMode = mode;
145 }
146
147 virtual PacpusEvent* getEventTemplate()
148 {
149 // TODO: check
150 return new PacpusEvent(GENERIC_EVENT);
151 }
152
[110]153private:
[148]154 ReadingMode m_readingMode;
[110]155
[148]156 // metode(QByteArray)
157 //QQueue jobQueue_;
[89]158};
159
[148]160class PACPUSLIB_API OutputInterfaceBase
161 : public AbstractInterface
[89]162{
163 Q_OBJECT
[96]164
[89]165public:
[148]166 OutputInterfaceBase(QString name, ComponentBase * component, QObject * parent = 0)
167 : AbstractInterface(name, component, parent)
168 {}
[96]169
[148]170 virtual ~OutputInterfaceBase()
171 {}
[89]172
[148]173 QStringList getInputConnectedList()
174 {
[89]175 QStringList list;
[148]176 for(QList<ConnectionBase>::iterator it = connections().begin(); it != connections().end(); ++it) {
[96]177 list.append(it->getInterface()->getName());
[148]178 }
[89]179 return list;
180 }
181
[159]182 // TODO for serealization prupose (not yet implemented !!!)
[148]183 void send(/*const*/ QByteArray & data)
184 {
[110]185 // TODO check at least one Typed connection
[89]186
[148]187 for(QList<ConnectionBase>::iterator it = connections().begin(); it!=connections().end(); ++it){
[110]188 QDataStream in(&data,QIODevice::ReadOnly);
[148]189 PacpusEvent* event = dynamic_cast<InputInterfaceBase*>(connections().at(0).getInterface())->getEventTemplate();
[110]190 event->streamIn(in);
191 QApplication::postEvent(it->getInterface(),event,it->getPriority());
192 }
[96]193 }
194
[89]195};
196
[110]197static bool connectInterface(OutputInterfaceBase* out, InputInterfaceBase * in, int priority, InputInterfaceBase::ReadingMode mode = InputInterfaceBase::GetLast)
[96]198{
199 if(out->getDataType() == in->getDataType() || out->getDataType() == QString(typeid(QByteArray).name()) || in->getDataType() == QString(typeid(QByteArray).name())) {
200 // Add connection
201 out->addConnection(ConnectionBase(in,priority)); // TODO make connect function
202 in->addConnection(ConnectionBase(out,priority));
[110]203 in->setReadingMode(mode);
[96]204 //LOG_INFO("connection : Output " << out->getSignature() << " => Input " << in->getSignature());
205 return true;
206 } else {
[148]207 //LOG_WARN("connecting " << out->getSignature() << ":" << out->getDataType() << " to " << in->getSignature() << ":" << in->getDataType() << " failled : DataType incompatible " << QString(typeid(QByteArray).name()));
208 return false;
[96]209 }
210}
211
[89]212} // namespace pacpus
213
214#endif // IN_OUT_BASE_H
Note: See TracBrowser for help on using the repository browser.