fw4spl
src/fwData/Node.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2016.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 //FIXME :This needs to be include first for GCC
8 #include <fwCom/Signal.hpp>
9 #include <fwCom/Signal.hxx>
10 //
11 #include "fwData/Node.hpp"
12 #include "fwData/registry/macros.hpp"
13 #include "fwData/Exception.hpp"
14 #include "fwData/Port.hpp"
15 
16 fwDataRegisterMacro( ::fwData::Node );
17 
18 namespace fwData
19 {
20 
21 const ::fwCom::Signals::SignalKeyType Node::s_UPDATED_SIG = "updated";
22 
23 //------------------------------------------------------------------------------
24 
26  m_sigUpdated(UpdatedSignalType::New())
27 {
28  m_signals( s_UPDATED_SIG, m_sigUpdated);
29 
30 
31 }
32 
33 //------------------------------------------------------------------------------
34 
36 {
38 }
39 
40 //------------------------------------------------------------------------------
41 
42 void Node::addInputPort(const ::fwData::Port::sptr& port)
43 {
44  m_inputs.push_back(port);
45 }
46 
47 //------------------------------------------------------------------------------
48 
49 void Node::addOutputPort(const ::fwData::Port::sptr& port)
50 {
51  m_outputs.push_back(port);
52 }
53 
54 //------------------------------------------------------------------------------
55 
57 {
58  return m_inputs;
59 }
60 
61 //------------------------------------------------------------------------------
62 
64 {
65  return m_outputs;
66 }
67 
68 //------------------------------------------------------------------------------
69 
70 void Node::setObject(const ::fwData::Object::sptr& object )
71 {
72  m_object = object;
73 }
74 
75 //------------------------------------------------------------------------------
76 
77 ::fwData::Object::sptr Node::getObject() const
78 {
79  return m_object;
80 }
81 
82 //------------------------------------------------------------------------------
83 
84 Port::sptr Node::findPort(const std::string &identifier, bool modeInput) const
85 {
86  if ( modeInput)
87  {
88  for ( const auto& input : m_inputs)
89  {
90  if ( input->getIdentifier() == identifier)
91  {
92  return input;
93  }
94  }
95  }
96  else
97  {
98  for ( const auto& output : m_outputs )
99  {
100  if ( output->getIdentifier() == identifier)
101  {
102  return output;
103  }
104  }
105  }
106  return Port::sptr();
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 void Node::shallowCopy(const Object::csptr &_source )
112 {
113  Node::csptr other = Node::dynamicConstCast(_source);
114  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
115  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
116  + " to " + this->getClassname()), !other );
117  this->fieldShallowCopy( _source );
118 
119  m_inputs.clear();
120  m_outputs.clear();
121 
122  if( other->getObject())
123  {
124  ::fwTools::Object::sptr object = ::fwData::factory::New( other->getObject()->getClassname() );
125  OSLM_ASSERT("The instantiation of '"<<other->getObject()->getClassname()<< "' failed", object );
126  m_object = ::fwData::Object::dynamicCast(object);
127  m_object->shallowCopy( other->m_object );
128  }
129  for(const ::fwData::Port::sptr& port : other->m_inputs)
130  {
131  this->addInputPort( ::fwData::Object::copy(port) );
132  }
133  for(const ::fwData::Port::sptr& port : other->m_outputs)
134  {
135  this->addOutputPort( ::fwData::Object::copy(port) );
136  }
137 }
138 
139 //------------------------------------------------------------------------------
140 
141 void Node::cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache)
142 {
143  Node::csptr other = Node::dynamicConstCast(_source);
144  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
145  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
146  + " to " + this->getClassname()), !bool(other) );
147  this->fieldDeepCopy( _source, cache );
148 
149  m_inputs.clear();
150  m_outputs.clear();
151 
152  m_object = ::fwData::Object::copy( other->m_object, cache );
153 
154  for(const ::fwData::Port::sptr &port : other->m_inputs)
155  {
156  ::fwData::Port::sptr newPort;
157  newPort = ::fwData::Object::copy(port, cache);
158  this->addInputPort(newPort);
159  }
160  for(const ::fwData::Port::sptr &port : other->m_outputs)
161  {
162  ::fwData::Port::sptr newPort;
163  newPort = ::fwData::Object::copy(port, cache);
164  this->addOutputPort(newPort);
165  }
166 }
167 
168 //------------------------------------------------------------------------------
169 
170 } // namespace fwData
virtual FWDATA_API ~Node()
Destructor.
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
FWDATA_API void setObject(const ::fwData::Object::sptr &object)
Set object to the node.
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
FWDATA_API void addOutputPort(const ::fwData::Port::sptr &port)
Add an output port.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
FWDATA_API::fwData::Object::sptr getObject() const
Get node object.
Implements data exception class.
This class defines a node. A node is represented by input and output ports.
FWDATA_API void shallowCopy(const Object::csptr &_source) override
Defines shallow copy.
std::vector< ::fwData::Port::sptr > PortContainer
Port container.
FWDATA_API PortContainer & getOutputPorts()
Get the container of output ports.
::fwData::Object::sptr m_object
node object
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_UPDATED_SIG
Updated signal key.
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
PortContainer m_outputs
Output port container.
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Defines deep copy.
FWDATA_API Port::sptr findPort(const std::string &_identifier, bool _modeInput) const
Get the port with given identifier.
FWDATA_API PortContainer & getInputPorts()
Get the container of input ports.
UpdatedSignalType::sptr m_sigUpdated
Updated signal.
Contains the representation of the data objects used in the framework.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
PortContainer m_inputs
Input port container.
FWDATA_API void addInputPort(const ::fwData::Port::sptr &port)
Add an input port.
FWDATA_API Node(::fwData::Object::Key key)
Constructor.