OpenWalnut  1.4.0
WModuleInputConnector.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WMODULEINPUTCONNECTOR_H
26 #define WMODULEINPUTCONNECTOR_H
27 
28 #include <string>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/thread/locks.hpp>
32 #endif
33 
34 class WModule;
35 #include "WModuleConnector.h"
36 
37 class WCondition;
38 
39 
40 
41 /**
42  * Class implementing input connection functionality between modules.
43  */
45 {
46 public:
47  /**
48  * Constructor.
49  *
50  * \param module the module which is owner of this connector.
51  * \param name The name of this connector.
52  * \param description Short description of this connector.
53  */
54  WModuleInputConnector( boost::shared_ptr< WModule > module, std::string name="", std::string description="" );
55 
56  /**
57  * Destructor.
58  */
59  virtual ~WModuleInputConnector();
60 
61  /**
62  * Checks whether the specified connector is an output connector.
63  *
64  * \param con the connector to check against.
65  *
66  * \return true if compatible.
67  */
68  virtual bool connectable( boost::shared_ptr<WModuleConnector> con );
69 
70  /**
71  * Gets the condition variable that gets fired whenever new data has been sent.
72  *
73  * \return the condition
74  */
75  boost::shared_ptr< WCondition > getDataChangedCondition();
76 
77  /**
78  * Connects (subscribes) a specified notify function with a signal this module instance is offering.
79  *
80  * \exception WModuleSignalSubscriptionFailed thrown if the signal can't be connected.
81  *
82  * \param signal the signal to connect to.
83  * \param notifier the notifier function to bind.
84  *
85  * \return the connection. Disconnect it manually if not needed anymore!
86  */
87  boost::signals2::connection subscribeSignal( MODULE_CONNECTOR_SIGNAL signal, t_GenericSignalHandlerType notifier );
88 
89  /**
90  * Returns true if this instance is an WModuleInputConnector.
91  *
92  * \return true if castable to WModuleInputConnector.
93  */
94  virtual bool isInputConnector() const;
95 
96  /**
97  * Returns true if this instance is an WModuleOutputConnector.
98  *
99  * \return true if castable to WModuleOutputConnector.
100  */
101  virtual bool isOutputConnector() const;
102 
103  /**
104  * Denotes whether the connected output was updated. This does NOT denote an actual change in the current data!
105  *
106  * \return true if there has been an update.
107  */
108  virtual bool updated();
109 
110  /**
111  * Resets the updated-flag. Call this from your module to reset the value of updated().
112  *
113  * \return the update flag before reset. Useful to get the flag and reset it in one call.
114  */
115  virtual bool handledUpdate();
116 
117 protected:
118  /**
119  * Connect additional signals.
120  *
121  * \param con the connector that requests connection.
122  *
123  */
124  virtual void connectSignals( boost::shared_ptr<WModuleConnector> con );
125 
126  /**
127  * Disconnect all signals subscribed by this connector from "con".
128  *
129  * \param con the connector that gets disconnected.
130  */
131  virtual void disconnectSignals( boost::shared_ptr<WModuleConnector> con );
132 
133  /**
134  * Gets called when the data on this input connector changed.
135  *
136  * \param input the input connector receiving the change.
137  * \param output the output connector sending the change notification.
138  */
139  virtual void notifyDataChange( boost::shared_ptr<WModuleConnector> input, boost::shared_ptr<WModuleConnector> output );
140 
141  /**
142  * Gets called whenever a connector gets connected to the specified input.
143  *
144  * \param here the connector of THIS module that got connected to "there"
145  * \param there the connector that has been connected with the connector "here" of this module.
146  */
147  virtual void notifyConnectionEstablished( boost::shared_ptr<WModuleConnector> here, boost::shared_ptr<WModuleConnector> there );
148 
149  /**
150  * Sets the update flag (use updated() to query it)to true. This is normally called by the notifyDataChange callback.
151  */
152  virtual void setUpdated();
153 
154 private:
155  /**
156  * Signal for "DATA_CHANGED" Events. We use a separate signal here (instead of using the signal send by the connected output)
157  * since the output can not determine the receiver when signalling. So we use an own signal handler and signal to "forward"
158  * the message and complete the information with a this-pointer.
159  */
160  t_GenericSignalType signal_DataChanged;
161 
162  /**
163  * Condition fired whenever data changes.
164  */
165  boost::shared_ptr< WCondition > m_dataChangedCondition;
166 
167  /**
168  * Connection for Data Changed signal of the connected output connector.
169  */
170  boost::signals2::connection m_DataChangedConnection;
171 
172  /**
173  * This lock protects the m_updated flag.
174  */
175  boost::shared_mutex m_updatedLock;
176 
177  /**
178  * A flag denoting that an update was received. It does not denote a real change in the value!
179  */
180  bool m_updated;
181 };
182 
183 #endif // WMODULEINPUTCONNECTOR_H
184 
virtual void setUpdated()
Sets the update flag (use updated() to query it)to true.
virtual bool isOutputConnector() const
Returns true if this instance is an WModuleOutputConnector.
Class representing a single module of OpenWalnut.
Definition: WModule.h:83
boost::shared_ptr< WCondition > getDataChangedCondition()
Gets the condition variable that gets fired whenever new data has been sent.
virtual void notifyConnectionEstablished(boost::shared_ptr< WModuleConnector > here, boost::shared_ptr< WModuleConnector > there)
Gets called whenever a connector gets connected to the specified input.
virtual bool updated()
Denotes whether the connected output was updated.
WModuleInputConnector(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
boost::shared_mutex m_updatedLock
This lock protects the m_updated flag.
virtual bool connectable(boost::shared_ptr< WModuleConnector > con)
Checks whether the specified connector is an output connector.
virtual void connectSignals(boost::shared_ptr< WModuleConnector > con)
Connect additional signals.
boost::signals2::connection subscribeSignal(MODULE_CONNECTOR_SIGNAL signal, t_GenericSignalHandlerType notifier)
Connects (subscribes) a specified notify function with a signal this module instance is offering...
bool m_updated
A flag denoting that an update was received.
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:47
virtual void disconnectSignals(boost::shared_ptr< WModuleConnector > con)
Disconnect all signals subscribed by this connector from "con".
boost::signals2::connection m_DataChangedConnection
Connection for Data Changed signal of the connected output connector.
virtual bool handledUpdate()
Resets the updated-flag.
t_GenericSignalType signal_DataChanged
Signal for "DATA_CHANGED" Events.
virtual void notifyDataChange(boost::shared_ptr< WModuleConnector > input, boost::shared_ptr< WModuleConnector > output)
Gets called when the data on this input connector changed.
Class implementing input connection functionality between modules.
virtual bool isInputConnector() const
Returns true if this instance is an WModuleInputConnector.
Base class for modelling connections between kernel modules.
boost::shared_ptr< WCondition > m_dataChangedCondition
Condition fired whenever data changes.
virtual ~WModuleInputConnector()
Destructor.