OpenWalnut  1.4.0
WModuleOutputData.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 WMODULEOUTPUTDATA_H
26 #define WMODULEOUTPUTDATA_H
27 
28 #include <string>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/shared_ptr.hpp>
32 #endif
33 
34 #include "../common/WLogger.h"
35 
36 // this is necessary since we have some kind of cyclic includes
37 template < typename T > class WModuleInputData;
38 #include "WModuleInputData.h"
39 #include "../common/WPrototyped.h"
40 #include "../common/WTransferable.h"
41 
42 #include "WModuleOutputConnector.h"
43 
44 /**
45  * Class offering an instantiate-able data connection between modules.
46  * Due to is template style it is possible to bind nearly arbitrary data.
47  */
48 template < typename T >
50 {
51 public:
52  /**
53  * Pointer to this. For convenience.
54  */
55  typedef boost::shared_ptr< WModuleOutputData< T > > PtrType;
56 
57  /**
58  * Pointer to this. For convenience.
59  */
60  typedef boost::shared_ptr< WModuleOutputData< T > > SPtr;
61 
62  /**
63  * Pointer to this. For convenience.
64  */
65  typedef boost::shared_ptr< const WModuleOutputData< T > > ConstSPtr;
66 
67  /**
68  * Reference to this type.
69  */
71 
72  /**
73  * Type of the connector.
74  */
76 
77  /**
78  * Typedef to the contained transferable.
79  */
80  typedef T TransferType;
81 
82  /**
83  * Convenience method to create a new instance of this out data connector with proper type.
84  *
85  * \param module the module owning this instance
86  * \param name the name of this connector.
87  * \param description the description of this connector.
88  *
89  * \return the pointer to the created connector.
90  */
91  static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
92 
93  /**
94  * 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
95  * specified module.
96  *
97  * \param module the module owning this instance
98  * \param name the name of this connector.
99  * \param description the description of this connector.
100  *
101  * \return the pointer to the created connector.
102  */
103  static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
104 
105  /**
106  * Constructor.
107  *
108  * \param module the module which is owner of this connector.
109  * \param name The name of this connector.
110  * \param description Short description of this connector.
111  */
112  WModuleOutputData( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" )
113  :WModuleOutputConnector( module, name, description )
114  {
115  m_data = boost::shared_ptr< T >();
116  };
117 
118  /**
119  * Destructor.
120  */
122  {
123  };
124 
125  /**
126  * Update the data associated
127  *
128  * \param data the data do send
129  */
130  virtual void updateData( boost::shared_ptr< T > data )
131  {
132  m_data = data;
133 
134  // broadcast this event
135  triggerUpdate();
136  };
137 
138  /**
139  * Resets the data on this output. It actually sets NULL and triggers an update.
140  */
141  virtual void reset()
142  {
143  updateData( boost::shared_ptr< T >() );
144  }
145 
146  /**
147  * This method simply propagates an update but does not actually change the data.
148  */
149  virtual void triggerUpdate()
150  {
151  // broadcast this event
153  };
154 
155  /**
156  * Gives back the currently set data as WTransferable.
157  *
158  * \return the data. If no data has been set: a NULL pointer is returned.
159  */
160  virtual const boost::shared_ptr< WTransferable > getRawData() const
161  {
162  return m_data;
163  };
164 
165  /**
166  * Gives back the currently set data.
167  *
168  * \return the data. If no data has been set: a NULL pointer is returned.
169  */
170  const boost::shared_ptr< T > getData() const
171  {
172  return m_data;
173  };
174 
175  /**
176  * Checks whether the specified connector is an input connector and compatible with T.
177  *
178  * \param con the connector to check against.
179  *
180  * \return true if compatible.
181  */
182  virtual bool connectable( boost::shared_ptr<WModuleConnector> con )
183  {
184  // since WModuleInputData::connectable already does all the type checking, we simply forward the call
186  };
187 
188  /**
189  * Returns the prototype of the Type T used in this connector.
190  *
191  * \return the prototype of the transfered type.
192  */
193  virtual boost::shared_ptr< WPrototyped > getTransferPrototype()
194  {
195  // get prototype or the data pointer currently set
196  return ( m_data == boost::shared_ptr< T >() ) ? T::getPrototype() : boost::static_pointer_cast< WPrototyped >( m_data );
197  };
198 
199 protected:
200 private:
201  /**
202  * The data associated with this connector.
203  */
204  boost::shared_ptr< T > m_data;
205 };
206 
207 template < typename T >
208 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::create( boost::shared_ptr< WModule > module, std::string name,
209  std::string description )
210 {
211  typedef typename WModuleOutputData< T >::PtrType PTR;
212  typedef typename WModuleOutputData< T >::Type TYPE;
213  return PTR( new TYPE( module, name, description ) );
214 }
215 
216 template < typename T >
217 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
218  std::string description )
219 {
220  typename WModuleOutputData< T >::PtrType c = create( module, name, description );
221  module->addConnector( c );
222  return c;
223 }
224 
225 #endif // WMODULEOUTPUTDATA_H
226 
virtual boost::shared_ptr< WPrototyped > getTransferPrototype()
Returns the prototype of the Type T used in this connector.
WModuleOutputData(boost::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
virtual bool connectable(boost::shared_ptr< WModuleConnector > con)
Checks whether the specified connector is an input connector and compatible with T.
boost::shared_ptr< const WModuleOutputData< T > > ConstSPtr
Pointer to this.
virtual const boost::shared_ptr< WTransferable > getRawData() const
Gives back the currently set data as WTransferable.
WModuleOutputData< T > Type
Type of the connector.
Class offering an instantiate-able data connection between modules.
Definition: WModule.h:75
const boost::shared_ptr< T > getData() const
Gives back the currently set data.
Class offering an instantiate-able data connection between modules.
Definition: WModule.h:77
virtual void propagateDataChange()
Propagates the signal "DATA_CHANGED" to all connected items.
Interface class for the concept "Prototype".
Definition: WPrototyped.h:39
boost::shared_ptr< WModuleOutputData< T > > SPtr
Pointer to this.
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...
T TransferType
Typedef to the contained transferable.
virtual ~WModuleOutputData()
Destructor.
Class implementing output connection functionality between modules.
virtual bool connectable(boost::shared_ptr< WModuleConnector > con)
Checks whether the specified connector is an input connector.
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...
boost::shared_ptr< WModuleOutputData< T > > PtrType
Pointer to this.
virtual void triggerUpdate()
This method simply propagates an update but does not actually change the data.
boost::shared_ptr< T > m_data
The data associated with this connector.
WModuleOutputData< T > & RefType
Reference to this type.
virtual void updateData(boost::shared_ptr< T > data)
Update the data associated.
virtual void reset()
Resets the data on this output.