OpenWalnut  1.3.1
WModuleInputData.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 WMODULEINPUTDATA_H
26 #define WMODULEINPUTDATA_H
27 
28 #include <string>
29 
30 #include <boost/shared_ptr.hpp>
31 #include <boost/thread/locks.hpp>
32 
33 // this is necessary since we have some kind of cyclic includes
34 template < typename T > class WModuleOutputData;
35 #include "WModuleOutputData.h"
36 #include "exceptions/WModuleConnectorUnconnected.h"
37 #include "../common/WTransferable.h"
38 #include "../common/WPrototyped.h"
39 
40 #include "WModuleInputConnector.h"
41 #include "WModuleOutputConnector.h"
42 
43 /**
44  * Class offering an instantiate-able data connection between modules.
45  * Due to is template style it is possible to bind nearly arbitrary data.
46  */
47 template < typename T >
49 {
50 public:
51  /**
52  * Pointer to this. For convenience.
53  */
54  typedef boost::shared_ptr< WModuleInputData< T > > PtrType;
55 
56  /**
57  * Reference to this type.
58  */
60 
61  /**
62  * Type of the connector.
63  */
65 
66  /**
67  * Typedef to the contained transferable.
68  */
69  typedef T TransferType;
70 
71  /**
72  * Convenience method to create a new instance of this in data connector with proper type.
73  *
74  * \param module the module owning this instance
75  * \param name the name of this connector.
76  * \param description the description of this connector.
77  *
78  * \return the pointer to the created connector.
79  */
80  static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
81 
82  /**
83  * Convenience method to create a new instance of this in data connector with proper type and add it to the list of connectors of the
84  * specified module.
85  *
86  * \param module the module owning this instance
87  * \param name the name of this connector.
88  * \param description the description of this connector.
89  *
90  * \return the pointer to the created connector.
91  */
92  static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
93 
94  /**
95  * Constructor.
96  *
97  * \param module the module which is owner of this connector.
98  * \param name The name of this connector.
99  * \param description Short description of this connector.
100  */
101  WModuleInputData( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" ):
102  WModuleInputConnector( module, name, description ),
103  m_disconnecting( false )
104  {
105  };
106 
107  /**
108  * Destructor.
109  */
111  {
112  };
113 
114  /**
115  * Disconnects this connector if connected. If it is not connected: nothing happens.
116  *
117  * \param con the connector to disconnect.
118  * \param removeFromOwnList if true the specified connection is also removed from the own connection list. If false it won't.
119  */
120  virtual void disconnect( boost::shared_ptr<WModuleConnector> con, bool removeFromOwnList = true );
121 
122  /**
123  * Gives the currently set data and resets the update flag.
124  *
125  * \param reset reset the flag of updated() if true (default).
126  *
127  * \return the data currently set. NULL if no data has been sent yet or the connector is unconnected.
128  */
129  const boost::shared_ptr< T > getData( bool reset = true )
130  {
131  // get a lock
132  boost::shared_lock<boost::shared_mutex> lock = boost::shared_lock<boost::shared_mutex>( m_connectionListLock );
133 
134  // Only reset change flag of requested
135  if( reset )
136  {
137  handledUpdate();
138  }
139 
140  // is there something in the list?
141  if( m_disconnecting || m_connected.empty() )
142  {
143  lock.unlock();
144  return boost::shared_ptr< T >();
145  }
146 
147  // get data
148  boost::shared_ptr< T > dat = boost::shared_dynamic_cast< T >(
149  boost::shared_dynamic_cast< WModuleOutputConnector >( *m_connected.begin() )->getRawData()
150  );
151 
152  // unlock and return
153  lock.unlock();
154 
155  return dat;
156  };
157 
158  /**
159  * Checks whether the specified connector is an input connector and compatible with T.
160  *
161  * \param con the connector to check against.
162  *
163  * \return true if compatible.
164  */
165  virtual bool connectable( boost::shared_ptr<WModuleConnector> con )
166  {
167  // NOTE: please consider the following: the input only accepts data which is of type T or higher. So only up casts from
168  // con's type T2 to T are needed/allowed what ever
169 
171  {
172  return false;
173  }
174 
175  // this calls virtual function to achieve the prototype of the WTransferable created with the type specified in
176  // WOutputData< XYZ >
177  boost::shared_ptr< WPrototyped > tProto =
178  dynamic_cast< WModuleOutputConnector* >( con.get() )->getTransferPrototype(); // NOLINT
179 
180  // NOTE: Check the type of the transfered object and whether the connector is an output
181  return dynamic_cast< T* >( tProto.get() ); // NOLINT
182  };
183 
184 protected:
185 private:
186  /**
187  * If true, the returned data will be NULL. Needed because disconnection process is based on multiple steps.
188  */
189  bool m_disconnecting;
190 };
191 
192 template < typename T >
193 void WModuleInputData< T >::disconnect( boost::shared_ptr<WModuleConnector> con, bool removeFromOwnList )
194 {
195  m_disconnecting = true;
196  WModuleInputConnector::disconnect( con, removeFromOwnList );
197  m_disconnecting = false;
198 }
199 
200 template < typename T >
201 typename WModuleInputData< T >::PtrType WModuleInputData< T >::create( boost::shared_ptr< WModule > module, std::string name,
202  std::string description )
203 {
204  typedef typename WModuleInputData< T >::PtrType PTR;
205  typedef typename WModuleInputData< T >::Type TYPE;
206  return PTR( new TYPE( module, name, description ) );
207 }
208 
209 template < typename T >
210 typename WModuleInputData< T >::PtrType WModuleInputData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
211  std::string description )
212 {
213  typename WModuleInputData< T >::PtrType c = create( module, name, description );
214  module->addConnector( c );
215  return c;
216 }
217 
218 #endif // WMODULEINPUTDATA_H
219