OpenWalnut  1.4.0
WModuleOutputForwardData.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 WMODULEOUTPUTFORWARDDATA_H
26 #define WMODULEOUTPUTFORWARDDATA_H
27 
28 #include <string>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/shared_ptr.hpp>
32 #endif
33 
34 #include "WModuleInputData.h"
35 #include "WModuleOutputData.h"
36 
37 /**
38  * This is a simple class which forwards output data to output data connectors. It itself is a output data connector and can be used
39  * as one, but also provides the possibility to forward data changes to other output data connectors.
40  */
41 template< typename T >
43 {
44 public:
45  /**
46  * Pointer to this. For convenience.
47  */
48  typedef boost::shared_ptr< WModuleOutputForwardData< T > > SPtr;
49 
50  /**
51  * Pointer to this. For convenience.
52  */
53  typedef boost::shared_ptr< const WModuleOutputForwardData< 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  * Convenience method to create a new instance of this out data connector with proper type.
77  *
78  * \param module the module owning this instance
79  * \param name the name of this connector.
80  * \param description the description of this connector.
81  *
82  * \return the pointer to the created connector.
83  */
84  static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
85 
86  /**
87  * Convenience method to create a new instance of this out data connector with proper type and add it to the list of connectors of the
88  * specified module.
89  *
90  * \param module the module owning this instance
91  * \param name the name of this connector.
92  * \param description the description of this connector.
93  *
94  * \return the pointer to the created connector.
95  */
96  static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
97 
98  /**
99  * Constructor. This creates a new output data connector which is able to forward data changes <b>FROM</b> other output data connectors.
100  *
101  * \param module the module which is owner of this connector.
102  * \param name The name of this connector.
103  * \param description Short description of this connector.
104  */
105  WModuleOutputForwardData( boost::shared_ptr< WModule > module, std::string name="", std::string description="" )
106  :WModuleOutputData< T >( module, name, description )
107  {
108  // initialize the output data connector
109  m_in = boost::shared_ptr< WModuleInputData< T > >( new WModuleInputData< T >( module, "[FWD]" + name, description ) );
110 
111  // subscribe both signals
112  m_in->subscribeSignal( CONNECTION_ESTABLISHED, boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
113  m_in->subscribeSignal( DATA_CHANGED, boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
114  };
115 
116  /**
117  * Destructor.
118  */
120  {
121  }
122 
123  /**
124  * Forward the output to the specified output. The specified output must be compatible with the template parameter of this output.
125  *
126  * \param from the output connector whose data should be forwarded.
127  */
128  virtual void forward( boost::shared_ptr< WModuleConnector > from )
129  {
130  m_in->connect( from );
131  }
132 
133  /**
134  * Remove the specified connector from the forwarding list.
135  *
136  * \param from the output connector to be removed from forwarding list.
137  */
138  virtual void unforward( boost::shared_ptr< WModuleConnector > from )
139  {
140  m_in->disconnect( from );
141  }
142 
143 protected:
144  /**
145  * The output connector which collects data and distributes it to all connectors connected using the forwardTo() method.
146  */
147  boost::shared_ptr< WModuleInputData< T > > m_in;
148 
149  /**
150  * Gets called whenever a connected output updates its data. In detail: it is a callback for m_in and waits simply forwards
151  * new data to this output instance.
152  */
153  virtual void inputNotifyDataChange( boost::shared_ptr<WModuleConnector> /*input*/, boost::shared_ptr<WModuleConnector> /*output*/ )
154  {
155  // if the input changes its data-> forward the change to this output instance
156  WModuleOutputData< T >::updateData( m_in->getData() );
157  }
158 
159 private:
160 };
161 
162 template < typename T >
163 typename WModuleOutputForwardData< T >::PtrType WModuleOutputForwardData< T >::create( boost::shared_ptr< WModule > module, std::string name,
164  std::string description )
165 {
166  typedef typename WModuleOutputForwardData< T >::PtrType PTR;
167  typedef typename WModuleOutputForwardData< T >::Type TYPE;
168  return PTR( new TYPE( module, name, description ) );
169 }
170 
171 template < typename T >
172 typename WModuleOutputForwardData< T >::PtrType WModuleOutputForwardData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
173  std::string description )
174 {
175  typename WModuleOutputForwardData< T >::PtrType c = create( module, name, description );
176  module->addConnector( c );
177  return c;
178 }
179 
180 #endif // WMODULEOUTPUTFORWARDDATA_H
181 
T TransferType
Typedef to the contained transferable.
virtual void unforward(boost::shared_ptr< WModuleConnector > from)
Remove the specified connector from the forwarding list.
This is a simple class which forwards output data to output data connectors.
boost::shared_ptr< const WModuleOutputForwardData< T > > ConstSPtr
Pointer to this.
virtual void inputNotifyDataChange(boost::shared_ptr< WModuleConnector >, boost::shared_ptr< WModuleConnector >)
Gets called whenever a connected output updates its data.
static PtrType create(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this out data connector with proper type...
Class offering an instantiate-able data connection between modules.
Definition: WModule.h:75
boost::shared_ptr< WModuleInputData< T > > m_in
The output connector which collects data and distributes it to all connectors connected using the for...
Class offering an instantiate-able data connection between modules.
Definition: WModule.h:77
virtual void forward(boost::shared_ptr< WModuleConnector > from)
Forward the output to the specified output.
static PtrType createAndAdd(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Convenience method to create a new instance of this out data connector with proper type and add it to...
WModuleOutputForwardData< T > Type
Type of the connector.
WModuleOutputForwardData(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
boost::shared_ptr< WModuleOutputForwardData< T > > SPtr
Pointer to this.
WModuleOutputForwardData< T > & RefType
Reference to this type.
virtual void updateData(boost::shared_ptr< T > data)
Update the data associated.
virtual ~WModuleOutputForwardData()
Destructor.