OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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  * Reference to this type.
57  */
59 
60  /**
61  * Type of the connector.
62  */
64 
65  /**
66  * Typedef to the contained transferable.
67  */
68  typedef T TransferType;
69 
70  /**
71  * Convenience method to create a new instance of this out data connector with proper type.
72  *
73  * \param module the module owning this instance
74  * \param name the name of this connector.
75  * \param description the description of this connector.
76  *
77  * \return the pointer to the created connector.
78  */
79  static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
80 
81  /**
82  * 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
83  * specified module.
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 createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
92 
93  /**
94  * Constructor.
95  *
96  * \param module the module which is owner of this connector.
97  * \param name The name of this connector.
98  * \param description Short description of this connector.
99  */
100  WModuleOutputData( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" )
101  :WModuleOutputConnector( module, name, description )
102  {
103  m_data = boost::shared_ptr< T >();
104  };
105 
106  /**
107  * Destructor.
108  */
110  {
111  };
112 
113  /**
114  * Update the data associated
115  *
116  * \param data the data do send
117  */
118  virtual void updateData( boost::shared_ptr< T > data )
119  {
120  m_data = data;
121 
122  // broadcast this event
123  triggerUpdate();
124  };
125 
126  /**
127  * Resets the data on this output. It actually sets NULL and triggers an update.
128  */
129  virtual void reset()
130  {
131  updateData( boost::shared_ptr< T >() );
132  }
133 
134  /**
135  * This method simply propagates an update but does not actually change the data.
136  */
137  virtual void triggerUpdate()
138  {
139  // broadcast this event
141  };
142 
143  /**
144  * Gives back the currently set data as WTransferable.
145  *
146  * \return the data. If no data has been set: a NULL pointer is returned.
147  */
148  virtual const boost::shared_ptr< WTransferable > getRawData() const
149  {
150  return m_data;
151  };
152 
153  /**
154  * Gives back the currently set data.
155  *
156  * \return the data. If no data has been set: a NULL pointer is returned.
157  */
158  const boost::shared_ptr< T > getData() const
159  {
160  return m_data;
161  };
162 
163  /**
164  * Checks whether the specified connector is an input connector and compatible with T.
165  *
166  * \param con the connector to check against.
167  *
168  * \return true if compatible.
169  */
170  virtual bool connectable( boost::shared_ptr<WModuleConnector> con )
171  {
172  // since WModuleInputData::connectable already does all the type checking, we simply forward the call
174  };
175 
176  /**
177  * Returns the prototype of the Type T used in this connector.
178  *
179  * \return the prototype of the transfered type.
180  */
181  virtual boost::shared_ptr< WPrototyped > getTransferPrototype()
182  {
183  // get prototype or the data pointer currently set
184  return ( m_data == boost::shared_ptr< T >() ) ? T::getPrototype() : boost::shared_static_cast< WPrototyped >( m_data );
185  };
186 
187 protected:
188 
189 private:
190 
191  /**
192  * The data associated with this connector.
193  */
194  boost::shared_ptr< T > m_data;
195 };
196 
197 template < typename T >
198 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::create( boost::shared_ptr< WModule > module, std::string name,
199  std::string description )
200 {
201  typedef typename WModuleOutputData< T >::PtrType PTR;
202  typedef typename WModuleOutputData< T >::Type TYPE;
203  return PTR( new TYPE( module, name, description ) );
204 }
205 
206 template < typename T >
207 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
208  std::string description )
209 {
210  typename WModuleOutputData< T >::PtrType c = create( module, name, description );
211  module->addConnector( c );
212  return c;
213 }
214 
215 #endif // WMODULEOUTPUTDATA_H
216