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