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