1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
25
#ifndef WMODULEINPUTDATA_H
26
#define WMODULEINPUTDATA_H
30
#include <boost/shared_ptr.hpp>
31
#include <boost/thread/locks.hpp>
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"
40
#include "WModuleInputConnector.h"
41
#include "WModuleOutputConnector.h"
44
* Class offering an instantiate-able data connection between modules.
45
* Due to is template style it is possible to bind nearly arbitrary data.
47
template < typename T >
48
class WModuleInputData: public WModuleInputConnector
52
* Pointer to this. For convenience.
54
typedef boost::shared_ptr< WModuleInputData< T > > PtrType;
57
* Reference to this type.
59
typedef WModuleInputData< T >& RefType;
62
* Type of the connector.
64
typedef WModuleInputData< T > Type;
67
* Typedef to the contained transferable.
69
typedef T TransferType;
72
* Convenience method to create a new instance of this in data connector with proper type.
74
* \param module the module owning this instance
75
* \param name the name of this connector.
76
* \param description the description of this connector.
78
* \return the pointer to the created connector.
80
static PtrType create( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
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
86
* \param module the module owning this instance
87
* \param name the name of this connector.
88
* \param description the description of this connector.
90
* \return the pointer to the created connector.
92
static PtrType createAndAdd( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" );
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.
101
WModuleInputData( boost::shared_ptr< WModule > module, std::string name = "", std::string description = "" ):
102
WModuleInputConnector( module, name, description ),
103
m_disconnecting( false )
110
virtual ~WModuleInputData()
115
* Disconnects this connector if connected. If it is not connected: nothing happens.
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.
120
virtual void disconnect( boost::shared_ptr<WModuleConnector> con, bool removeFromOwnList = true );
123
* Gives the currently set data and resets the update flag.
125
* \param reset reset the flag of updated() if true (default).
127
* \return the data currently set. NULL if no data has been sent yet or the connector is unconnected.
129
const boost::shared_ptr< T > getData( bool reset = true )
132
boost::shared_lock<boost::shared_mutex> lock = boost::shared_lock<boost::shared_mutex>( m_connectionListLock );
134
// Only reset change flag of requested
140
// is there something in the list?
141
if( m_disconnecting || m_connected.empty() )
144
return boost::shared_ptr< T >();
148
boost::shared_ptr< T > dat = boost::shared_dynamic_cast< T >(
149
boost::shared_dynamic_cast< WModuleOutputConnector >( *m_connected.begin() )->getRawData()
159
* Checks whether the specified connector is an input connector and compatible with T.
161
* \param con the connector to check against.
163
* \return true if compatible.
165
virtual bool connectable( boost::shared_ptr<WModuleConnector> con )
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
170
if( !WModuleInputConnector::connectable( con ) )
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
180
// NOTE: Check the type of the transfered object and whether the connector is an output
181
return dynamic_cast< T* >( tProto.get() ); // NOLINT
187
* If true, the returned data will be NULL. Needed because disconnection process is based on multiple steps.
189
bool m_disconnecting;
192
template < typename T >
193
void WModuleInputData< T >::disconnect( boost::shared_ptr<WModuleConnector> con, bool removeFromOwnList )
195
m_disconnecting = true;
196
WModuleInputConnector::disconnect( con, removeFromOwnList );
197
m_disconnecting = false;
200
template < typename T >
201
typename WModuleInputData< T >::PtrType WModuleInputData< T >::create( boost::shared_ptr< WModule > module, std::string name,
202
std::string description )
204
typedef typename WModuleInputData< T >::PtrType PTR;
205
typedef typename WModuleInputData< T >::Type TYPE;
206
return PTR( new TYPE( module, name, description ) );
209
template < typename T >
210
typename WModuleInputData< T >::PtrType WModuleInputData< T >::createAndAdd( boost::shared_ptr< WModule > module, std::string name,
211
std::string description )
213
typename WModuleInputData< T >::PtrType c = create( module, name, description );
214
module->addConnector( c );
218
#endif // WMODULEINPUTDATA_H