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