OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <iostream>
29 #include <string>
30 
31 #include <boost/shared_ptr.hpp>
32 
33 #include "../common/WLogger.h"
34 
35 #include "WModuleInputData.h"
36 #include "WModuleOutputData.h"
37 
38 /**
39  * This is a simple class which forwards output data to output data connectors. It itself is a output data connector and can be used
40  * as one, but also provides the possibility to forward data changes to other output data connectors.
41  */
42 template< typename T >
44 {
45 public:
46 
47  /**
48  * Constructor. This creates a new output data connector which is able to forward data changes <b>FROM</b> other output data connectors.
49  *
50  * \param module the module which is owner of this connector.
51  * \param name The name of this connector.
52  * \param description Short description of this connector.
53  */
54  WModuleOutputForwardData( boost::shared_ptr< WModule > module, std::string name="", std::string description="" )
55  :WModuleOutputData< T >( module, name, description )
56  {
57  // initialize the output data connector
58  m_in = boost::shared_ptr< WModuleInputData< T > >( new WModuleInputData< T >( module, "[FWD]" + name, description ) );
59 
60  // subscribe both signals
61  m_in->subscribeSignal( CONNECTION_ESTABLISHED, boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
62  m_in->subscribeSignal( DATA_CHANGED, boost::bind( &WModuleOutputForwardData::inputNotifyDataChange, this, _1, _2 ) );
63  };
64 
65  /**
66  * Destructor.
67  */
69  {
70  }
71 
72  /**
73  * Forward the output to the specified output. The specified output must be compatible with the template parameter of this output.
74  *
75  * \param from the output connector whose data should be forwarded.
76  */
77  virtual void forward( boost::shared_ptr< WModuleConnector > from )
78  {
79  m_in->connect( from );
80  }
81 
82  /**
83  * Remove the specified connector from the forwarding list.
84  *
85  * \param from the output connector to be removed from forwarding list.
86  */
87  virtual void unforward( boost::shared_ptr< WModuleConnector > from )
88  {
89  m_in->disconnect( from );
90  }
91 
92 protected:
93 
94  /**
95  * The output connector which collects data and distributes it to all connectors connected using the forwardTo() method.
96  */
97  boost::shared_ptr< WModuleInputData< T > > m_in;
98 
99  /**
100  * Gets called whenever a connected output updates its data. In detail: it is a callback for m_in and waits simply forwards
101  * new data to this output instance.
102  */
103  virtual void inputNotifyDataChange( boost::shared_ptr<WModuleConnector> /*input*/, boost::shared_ptr<WModuleConnector> /*output*/ )
104  {
105  // if the input changes its data-> forward the change to this output instance
106  this->updateData( m_in->getData() );
107  }
108 
109 private:
110 };
111 
112 #endif // WMODULEOUTPUTFORWARDDATA_H
113