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