OpenWalnut  1.5.0dev
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 <memory>
29 #include <string>
30 
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 std::shared_ptr< WModuleOutputForwardData< T > > SPtr;
47 
48  /**
49  * Pointer to this. For convenience.
50  */
51  typedef std::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( std::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( std::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( std::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 = std::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,
111  this,
112  boost::placeholders::_1,
113  boost::placeholders::_2 ) );
114  m_in->subscribeSignal( DATA_CHANGED, boost::bind( &WModuleOutputForwardData::inputNotifyDataChange,
115  this,
116  boost::placeholders::_1,
117  boost::placeholders::_2 ) );
118  };
119 
120  /**
121  * Destructor.
122  */
124  {
125  }
126 
127  /**
128  * Forward the output to the specified output. The specified output must be compatible with the template parameter of this output.
129  *
130  * \param from the output connector whose data should be forwarded.
131  */
132  virtual void forward( std::shared_ptr< WModuleConnector > from )
133  {
134  m_in->connect( from );
135  }
136 
137  /**
138  * Remove the specified connector from the forwarding list.
139  *
140  * \param from the output connector to be removed from forwarding list.
141  */
142  virtual void unforward( std::shared_ptr< WModuleConnector > from )
143  {
144  m_in->disconnect( from );
145  }
146 
147 protected:
148  /**
149  * The output connector which collects data and distributes it to all connectors connected using the forwardTo() method.
150  */
151  std::shared_ptr< WModuleInputData< T > > m_in;
152 
153  /**
154  * Gets called whenever a connected output updates its data. In detail: it is a callback for m_in and waits simply forwards
155  * new data to this output instance.
156  */
157  virtual void inputNotifyDataChange( std::shared_ptr<WModuleConnector> /*input*/, std::shared_ptr<WModuleConnector> /*output*/ )
158  {
159  // if the input changes its data-> forward the change to this output instance
161  }
162 
163 private:
164 };
165 
166 template < typename T >
167 typename WModuleOutputForwardData< T >::PtrType WModuleOutputForwardData< T >::create( std::shared_ptr< WModule > module, std::string name,
168  std::string description )
169 {
170  typedef typename WModuleOutputForwardData< T >::PtrType PTR;
171  typedef typename WModuleOutputForwardData< T >::Type TYPE;
172  return PTR( new TYPE( module, name, description ) );
173 }
174 
175 template < typename T >
176 typename WModuleOutputForwardData< T >::PtrType WModuleOutputForwardData< T >::createAndAdd( std::shared_ptr< WModule > module, std::string name,
177  std::string description )
178 {
179  typename WModuleOutputForwardData< T >::PtrType c = create( module, name, description );
180  module->addConnector( c );
181  return c;
182 }
183 
184 #endif // WMODULEOUTPUTFORWARDDATA_H
std::shared_ptr< WModuleConnector > SPtr
Shared pointer to this class.
Class offering an instantiate-able data connection between modules.
Class offering an instantiate-able data connection between modules.
virtual void updateData(std::shared_ptr< T > data)
Update the data associated.
std::shared_ptr< WModuleOutputData< T > > PtrType
Pointer to this.
This is a simple class which forwards output data to output data connectors.
virtual ~WModuleOutputForwardData()
Destructor.
std::shared_ptr< const WModuleOutputForwardData< T > > ConstSPtr
Pointer to this.
std::shared_ptr< WModuleOutputForwardData< T > > SPtr
Pointer to this.
virtual void inputNotifyDataChange(std::shared_ptr< WModuleConnector >, std::shared_ptr< WModuleConnector >)
Gets called whenever a connected output updates its data.
virtual void unforward(std::shared_ptr< WModuleConnector > from)
Remove the specified connector from the forwarding list.
virtual void forward(std::shared_ptr< WModuleConnector > from)
Forward the output to the specified output.
WModuleOutputForwardData< T > Type
Type of the connector.
WModuleOutputForwardData< T > & RefType
Reference to this type.
WModuleOutputForwardData(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
static PtrType create(std::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.
T TransferType
Typedef to the contained transferable.
static PtrType createAndAdd(std::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...
std::shared_ptr< WModuleInputData< T > > m_in
The output connector which collects data and distributes it to all connectors connected using the for...