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