OpenWalnut  1.5.0dev
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 <memory>
29 #include <string>
30 
31 
32 #include "../common/WLogger.h"
33 #include "../common/WPrototyped.h"
34 #include "../common/WTransferable.h"
35 #include "WModuleInputData.h"
36 #include "WModuleOutputConnector.h"
37 
38 /**
39  * Class offering an instantiate-able data connection between modules.
40  * Due to is template style it is possible to bind nearly arbitrary data.
41  */
42 template < typename T >
44 {
45 public:
46  /**
47  * Pointer to this. For convenience.
48  */
49  typedef std::shared_ptr< WModuleOutputData< T > > PtrType;
50 
51  /**
52  * Pointer to this. For convenience.
53  */
54  typedef std::shared_ptr< WModuleOutputData< T > > SPtr;
55 
56  /**
57  * Pointer to this. For convenience.
58  */
59  typedef std::shared_ptr< const WModuleOutputData< T > > ConstSPtr;
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( std::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( std::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
98 
99  /**
100  * Constructor.
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  WModuleOutputData( std::shared_ptr< WModule > module, std::string name = "", std::string description = "" )
107  :WModuleOutputConnector( module, name, description )
108  {
109  m_data = std::shared_ptr< T >();
110  };
111 
112  /**
113  * Destructor.
114  */
116  {
117  };
118 
119  /**
120  * Update the data associated.
121  *
122  * \param data the data do send
123  */
124  virtual void updateData( std::shared_ptr< T > data )
125  {
126  m_data = data;
127 
128  // broadcast this event
129  triggerUpdate();
130  };
131 
132  /**
133  * Resets the data on this output. It actually sets NULL and triggers an update.
134  */
135  virtual void reset()
136  {
137  updateData( std::shared_ptr< T >() );
138  }
139 
140  /**
141  * This method simply propagates an update but does not actually change the data.
142  */
143  virtual void triggerUpdate()
144  {
145  // broadcast this event
147  };
148 
149  /**
150  * Gives back the currently set data as WTransferable.
151  *
152  * \return the data. If no data has been set: a NULL pointer is returned.
153  */
154  virtual const std::shared_ptr< WTransferable > getRawData() const
155  {
156  return m_data;
157  };
158 
159  /**
160  * Gives back the currently set data.
161  *
162  * \return the data. If no data has been set: a NULL pointer is returned.
163  */
164  const std::shared_ptr< T > getData() const
165  {
166  return m_data;
167  };
168 
169  /**
170  * Checks whether the specified connector is an input connector and compatible with T.
171  *
172  * \param con the connector to check against.
173  *
174  * \return true if compatible.
175  */
176  virtual bool connectable( std::shared_ptr<WModuleConnector> con )
177  {
178  // since WModuleInputData::connectable already does all the type checking, we simply forward the call
180  };
181 
182  /**
183  * Returns the prototype of the Type T used in this connector.
184  *
185  * \return the prototype of the transfered type.
186  */
187  virtual std::shared_ptr< WPrototyped > getTransferPrototype()
188  {
189  // get prototype or the data pointer currently set
190  return ( m_data == std::shared_ptr< T >() ) ? T::getPrototype() : std::static_pointer_cast< WPrototyped >( m_data );
191  };
192 
193 protected:
194  /**
195  * The data associated with this connector.
196  *
197  * \note If you modify this or its contents, consider triggering an update. See \ref updateData() for details.
198  */
199  std::shared_ptr< T > m_data;
200 private:
201 };
202 
203 template < typename T >
204 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::create( std::shared_ptr< WModule > module, std::string name,
205  std::string description )
206 {
207  typedef typename WModuleOutputData< T >::PtrType PTR;
208  typedef typename WModuleOutputData< T >::Type TYPE;
209  return PTR( new TYPE( module, name, description ) );
210 }
211 
212 template < typename T >
213 typename WModuleOutputData< T >::PtrType WModuleOutputData< T >::createAndAdd( std::shared_ptr< WModule > module, std::string name,
214  std::string description )
215 {
216  typename WModuleOutputData< T >::PtrType c = create( module, name, description );
217  module->addConnector( c );
218  return c;
219 }
220 
221 #endif // WMODULEOUTPUTDATA_H
222 
Class implementing output connection functionality between modules.
virtual bool connectable(std::shared_ptr< WModuleConnector > con)
Checks whether the specified connector is an input connector.
virtual void propagateDataChange()
Propagates the signal "DATA_CHANGED" to all connected items.
Class offering an instantiate-able data connection between modules.
virtual std::shared_ptr< WPrototyped > getTransferPrototype()
Returns the prototype of the Type T used in this connector.
virtual void triggerUpdate()
This method simply propagates an update but does not actually change the data.
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...
T TransferType
Typedef to the contained transferable.
std::shared_ptr< WModuleOutputData< T > > SPtr
Pointer to this.
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.
const std::shared_ptr< T > getData() const
Gives back the currently set data.
WModuleOutputData< T > Type
Type of the connector.
virtual bool connectable(std::shared_ptr< WModuleConnector > con)
Checks whether the specified connector is an input connector and compatible with T.
std::shared_ptr< const WModuleOutputData< T > > ConstSPtr
Pointer to this.
virtual void reset()
Resets the data on this output.
virtual const std::shared_ptr< WTransferable > getRawData() const
Gives back the currently set data as WTransferable.
WModuleOutputData(std::shared_ptr< WModule > module, std::string name="", std::string description="")
Constructor.
virtual ~WModuleOutputData()
Destructor.
WModuleOutputData< T > & RefType
Reference to this type.
virtual void updateData(std::shared_ptr< T > data)
Update the data associated.
std::shared_ptr< T > m_data
The data associated with this connector.
std::shared_ptr< WModuleOutputData< T > > PtrType
Pointer to this.