OpenWalnut  1.4.0
WModuleInputForwardData.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 WMODULEINPUTFORWARDDATA_H
26 #define WMODULEINPUTFORWARDDATA_H
27 
28 #include <string>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/shared_ptr.hpp>
32 #endif
33 
34 #include "../common/WLogger.h"
35 
36 #include "WModuleInputData.h"
37 #include "WModuleOutputData.h"
38 
39 /**
40  * This is a simple class which forwards input data to input data connectors. It itself is a input data connector and can be used
41  * as one, but also provides the possibility to forward data changes to other input data connectors, using a separate output data
42  * connector (which is not visible to the outside world).
43  */
44 template< typename T >
46 {
47 public:
48  /**
49  * Pointer to this. For convenience.
50  */
51  typedef boost::shared_ptr< WModuleInputForwardData< T > > PtrType;
52 
53  /**
54  * Reference to this type.
55  */
57 
58  /**
59  * Type of the connector.
60  */
62 
63  /**
64  * Typedef to the contained transferable.
65  */
66  typedef T TransferType;
67 
68  /**
69  * Constructor. This creates a new input data connector which is able to forward data changes <b>TO</b> other input data connectors.
70  *
71  * \param module the module which is owner of this connector.
72  * \param name The name of this connector.
73  * \param description Short description of this connector.
74  */
75  WModuleInputForwardData( boost::shared_ptr< WModule > module, std::string name="", std::string description="" )
76  :WModuleInputData< T >( module, name, description )
77  {
78  // initialize the output data connector
79  m_out = boost::shared_ptr< WModuleOutputData< T > >( new WModuleOutputData< T >( module, "[FWD]" + name, description ) );
80  };
81 
82  /**
83  * Destructor.
84  */
86  {
87  }
88 
89  /**
90  * Forward the input to the specified input. The specified input must be compatible with the template parameter of this input.
91  *
92  * \param to the input connector to forward data to.
93  */
94  virtual void forward( boost::shared_ptr< WModuleConnector > to )
95  {
96  m_out->connect( to );
97  }
98 
99  /**
100  * Remove the specified connector from the forwarding list.
101  *
102  * \param to the input connector to be removed from forwarding list.
103  */
104  virtual void unforward( boost::shared_ptr< WModuleConnector > to )
105  {
106  m_out->disconnect( to );
107  }
108 
109  /**
110  * Convenience method to create a new instance of this in forward data connector with proper type.
111  *
112  * \param module The module owning this instance
113  * \param name The name of this connector.
114  * \param description The description of this connector.
115  *
116  * \return The pointer to the created forward connector.
117  */
118  static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
119 
120  /**
121  * Convenience method to create a new instance of this in forward data connector with proper
122  * type and add it to the list of connectors of the specified module.
123  *
124  * \param module The module owning this instance
125  * \param name The name of this connector.
126  * \param description The description of this connector.
127  *
128  * \return The pointer to the created forward connector.
129  */
130  static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
131 
132 protected:
133  /**
134  * The output connector which collects data and distributes it to all connectors connected using the forwardTo() method.
135  */
136  boost::shared_ptr< WModuleOutputData< T > > m_out;
137 
138  /**
139  * Gets called whenever a connected output updates its data. This method uses this callback to update the m_out connector to
140  * inform all inputs to which the data should be forwarded.
141  *
142  * \param input the input connector receiving the change
143  * \param output the output connector sending the change
144  */
145  virtual void notifyDataChange( boost::shared_ptr<WModuleConnector> input, boost::shared_ptr<WModuleConnector> output )
146  {
147  m_out->updateData( WModuleInputData< T >::getData() );
148 
149  // simply forward the call
151  }
152 
153  /**
154  * Gets called whenever a connection between a remote and local connector gets closed. This is used here to forward the NULL data.
155  *
156  * \param here the connector of THIS module getting disconnected.
157  * \param there the connector of the other module getting disconnected.
158  */
159  virtual void notifyConnectionClosed( boost::shared_ptr<WModuleConnector> here, boost::shared_ptr<WModuleConnector> there )
160  {
161  m_out->reset();
162 
163  // simply forward the call
165  }
166 
167 private:
168 };
169 
170 template < typename T >
171 inline typename WModuleInputForwardData< T >::PtrType WModuleInputForwardData< T >::create( boost::shared_ptr< WModule > module,
172  std::string name,
173  std::string description )
174 {
175  return typename WModuleInputForwardData< T >::PtrType ( new WModuleInputForwardData< T >( module, name, description ) );
176 }
177 
178 template < typename T >
179 inline typename WModuleInputForwardData< T >::PtrType WModuleInputForwardData< T >::createAndAdd( boost::shared_ptr< WModule > module,
180  std::string name,
181  std::string description )
182 {
183  typename WModuleInputForwardData< T >::PtrType c = create( module, name, description );
184  module->addConnector( c );
185  return c;
186 }
187 
188 
189 #endif // WMODULEINPUTFORWARDDATA_H
190 
This is a simple class which forwards input data to input data connectors.
Definition: WModule.h:76
boost::shared_ptr< WModuleOutputData< T > > m_out
The output connector which collects data and distributes it to all connectors connected using the for...
virtual ~WModuleInputForwardData()
Destructor.
T TransferType
Typedef to the contained transferable.
Class offering an instantiate-able data connection between modules.
Definition: WModule.h:75
virtual void forward(boost::shared_ptr< WModuleConnector > to)
Forward the input to the specified input.
WModuleInputForwardData< T > & RefType
Reference to this type.
WModuleInputForwardData< T > Type
Type of the connector.
Class offering an instantiate-able data connection between modules.
Definition: WModule.h:77
virtual void notifyConnectionClosed(boost::shared_ptr< WModuleConnector > here, boost::shared_ptr< WModuleConnector > there)
Gets called whenever a connection between a remote and local connector gets closed.
WModuleInputForwardData(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
static PtrType create(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this in forward data connector with proper type...
virtual void notifyConnectionClosed(boost::shared_ptr< WModuleConnector > here, boost::shared_ptr< WModuleConnector > there)
Gets called whenever a connection between a remote and local connector gets closed.
virtual void notifyDataChange(boost::shared_ptr< WModuleConnector > input, boost::shared_ptr< WModuleConnector > output)
Gets called whenever a connected output updates its data.
boost::shared_ptr< WModuleInputForwardData< T > > PtrType
Pointer to this.
virtual void notifyDataChange(boost::shared_ptr< WModuleConnector > input, boost::shared_ptr< WModuleConnector > output)
Gets called when the data on this input connector changed.
static PtrType createAndAdd(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this in forward data connector with proper type and ad...
virtual void unforward(boost::shared_ptr< WModuleConnector > to)
Remove the specified connector from the forwarding list.