OpenWalnut  1.5.0dev
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 <memory>
29 #include <shared_mutex>
30 #include <string>
31 
32 #include <boost/thread/locks.hpp>
33 
34 #include "WModuleConnector.h"
35 
36 class WCondition;
37 
38 
39 
40 /**
41  * Class implementing input connection functionality between modules.
42  */
44 {
45 public:
46  /**
47  * Constructor.
48  *
49  * \param module the module which is owner of this connector.
50  * \param name The name of this connector.
51  * \param description Short description of this connector.
52  */
53  WModuleInputConnector( std::shared_ptr< WModule > module, std::string name="", std::string description="" );
54 
55  /**
56  * Destructor.
57  */
58  virtual ~WModuleInputConnector();
59 
60  /**
61  * Checks whether the specified connector is an output connector.
62  *
63  * \param con the connector to check against.
64  *
65  * \return true if compatible.
66  */
67  virtual bool connectable( std::shared_ptr<WModuleConnector> con );
68 
69  /**
70  * Checks whether the specified connector is connectable to this one, but ignores compatibility the type to be transferred.
71  *
72  * \param con the connector to check against.
73  *
74  * \return true if compatible.
75  */
76  virtual bool lazyConnectable( std::shared_ptr<WModuleConnector> con );
77 
78  /**
79  * Connects (subscribes) a specified notify function with a signal this module instance is offering.
80  *
81  * \exception WModuleSignalSubscriptionFailed thrown if the signal can't be connected.
82  *
83  * \param signal the signal to connect to.
84  * \param notifier the notifier function to bind.
85  *
86  * \return the connection. Disconnect it manually if not needed anymore!
87  */
88  boost::signals2::connection subscribeSignal( MODULE_CONNECTOR_SIGNAL signal, t_GenericSignalHandlerType notifier );
89 
90  /**
91  * Returns true if this instance is an WModuleInputConnector.
92  *
93  * \return true if castable to WModuleInputConnector.
94  */
95  virtual bool isInputConnector() const;
96 
97  /**
98  * Returns true if this instance is an WModuleOutputConnector.
99  *
100  * \return true if castable to WModuleOutputConnector.
101  */
102  virtual bool isOutputConnector() const;
103 
104  /**
105  * Denotes whether the connected output was updated. This does NOT denote an actual change in the current data!
106  *
107  * \return true if there has been an update.
108  */
109  virtual bool updated();
110 
111  /**
112  * Resets the updated-flag. Call this from your module to reset the value of updated().
113  *
114  * \return the update flag before reset. Useful to get the flag and reset it in one call.
115  */
116  virtual bool handledUpdate();
117 
118 protected:
119  /**
120  * Connect additional signals.
121  *
122  * \param con the connector that requests connection.
123  *
124  */
125  virtual void connectSignals( std::shared_ptr<WModuleConnector> con );
126 
127  /**
128  * Disconnect all signals subscribed by this connector from "con".
129  *
130  * \param con the connector that gets disconnected.
131  */
132  virtual void disconnectSignals( std::shared_ptr<WModuleConnector> con );
133 
134  /**
135  * Gets called when the data on this input connector changed.
136  *
137  * \param input the input connector receiving the change.
138  * \param output the output connector sending the change notification.
139  */
140  virtual void notifyDataChange( std::shared_ptr<WModuleConnector> input, std::shared_ptr<WModuleConnector> output );
141 
142  /**
143  * Gets called whenever a connector gets connected to the specified input.
144  *
145  * \param here the connector of THIS module that got connected to "there"
146  * \param there the connector that has been connected with the connector "here" of this module.
147  */
148  virtual void notifyConnectionEstablished( std::shared_ptr<WModuleConnector> here, std::shared_ptr<WModuleConnector> there );
149 
150  /**
151  * Sets the update flag (use updated() to query it)to true. This is normally called by the notifyDataChange callback.
152  */
153  virtual void setUpdated();
154 
155 private:
156  /**
157  * Signal for "DATA_CHANGED" Events. We use a separate signal here (instead of using the signal send by the connected output)
158  * since the output can not determine the receiver when signalling. So we use an own signal handler and signal to "forward"
159  * the message and complete the information with a this-pointer.
160  */
161  t_GenericSignalType signal_DataChanged;
162 
163  /**
164  * Connection for Data Changed signal of the connected output connector.
165  */
166  boost::signals2::connection m_DataChangedConnection;
167 
168  /**
169  * This lock protects the m_updated flag.
170  */
171  std::shared_mutex m_updatedLock;
172 
173  /**
174  * A flag denoting that an update was received. It does not denote a real change in the value!
175  */
176  bool m_updated;
177 };
178 
179 #endif // WMODULEINPUTCONNECTOR_H
180 
Class to encapsulate boost::condition_variable_any.
Definition: WCondition.h:42
Base class for modelling connections between kernel modules.
Class implementing input connection functionality between modules.
virtual bool connectable(std::shared_ptr< WModuleConnector > con)
Checks whether the specified connector is an output connector.
virtual void disconnectSignals(std::shared_ptr< WModuleConnector > con)
Disconnect all signals subscribed by this connector from "con".
virtual bool isInputConnector() const
Returns true if this instance is an WModuleInputConnector.
virtual bool isOutputConnector() const
Returns true if this instance is an WModuleOutputConnector.
std::shared_mutex m_updatedLock
This lock protects the m_updated flag.
virtual bool updated()
Denotes whether the connected output was updated.
virtual bool handledUpdate()
Resets the updated-flag.
t_GenericSignalType signal_DataChanged
Signal for "DATA_CHANGED" Events.
virtual void setUpdated()
Sets the update flag (use updated() to query it)to true.
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.
virtual void connectSignals(std::shared_ptr< WModuleConnector > con)
Connect additional signals.
WModuleInputConnector(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
virtual bool lazyConnectable(std::shared_ptr< WModuleConnector > con)
Checks whether the specified connector is connectable to this one, but ignores compatibility the type...
virtual void notifyDataChange(std::shared_ptr< WModuleConnector > input, std::shared_ptr< WModuleConnector > output)
Gets called when the data on this input connector changed.
virtual void notifyConnectionEstablished(std::shared_ptr< WModuleConnector > here, std::shared_ptr< WModuleConnector > there)
Gets called whenever a connector gets connected to the specified input.
virtual ~WModuleInputConnector()
Destructor.
boost::signals2::connection m_DataChangedConnection
Connection for Data Changed signal of the connected output connector.
bool m_updated
A flag denoting that an update was received.