fw4spl
src/fwData/ProcessObject.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
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 
8 #include "fwData/Exception.hpp"
9 #include "fwData/ProcessObject.hpp"
10 #include "fwData/registry/macros.hpp"
11 
12 #include <fwCore/base.hpp>
13 
14 #include <functional>
15 
16 fwDataRegisterMacro( ::fwData::ProcessObject );
17 
18 namespace fwData
19 {
20 
21 //------------------------------------------------------------------------------
22 
24 {
25 }
26 
27 //------------------------------------------------------------------------------
28 
30 {
31 }
32 
33 //------------------------------------------------------------------------------
34 
35 ::fwData::Object::sptr ProcessObject::getValue(const ParamNameType& name, const ProcessObjectMapType& params)
36 {
37  ::fwData::Object::sptr object;
38  ProcessObjectMapType::const_iterator iter = params.find(name);
39  if(iter != params.end())
40  {
41  object = iter->second;
42  }
43  return object;
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 ::fwData::Object::sptr ProcessObject::getInput(const ParamNameType& name)
49 {
50  return this->getValue(name, m_inputs);
51 }
52 
53 //------------------------------------------------------------------------------
54 
55 ::fwData::Object::sptr ProcessObject::getOutput(const ParamNameType& name)
56 {
57  return this->getValue(name, m_outputs);
58 }
59 
60 //------------------------------------------------------------------------------
61 
62 void ProcessObject::setValue(const ParamNameType& name, ::fwData::Object::sptr object, ProcessObjectMapType& params)
63 {
64  std::pair<ProcessObjectMapType::iterator, bool> res;
65  res = params.insert(ProcessObjectMapType::value_type(name, object));
66  if( !res.second )
67  {
68  res.first->second = object;
69  }
70 }
71 
72 //------------------------------------------------------------------------------
73 
74 void ProcessObject::setInputValue(const ParamNameType& name, ::fwData::Object::sptr object)
75 {
76  this->setValue(name, object, m_inputs);
77 }
78 
79 //------------------------------------------------------------------------------
80 
81 void ProcessObject::setOutputValue(const ParamNameType& name, ::fwData::Object::sptr object)
82 {
83  this->setValue(name, object, m_outputs);
84 }
85 
86 //------------------------------------------------------------------------------
87 
88 ProcessObject::ParamNameVectorType ProcessObject::getParamNames(const ProcessObjectMapType& params) const
89 {
90  ParamNameVectorType names;
91  std::transform( params.begin(), params.end(),
92  std::back_inserter(names),
93  std::bind(&ProcessObjectMapType::value_type::first, std::placeholders::_1) );
94  return names;
95 }
96 
97 //------------------------------------------------------------------------------
98 
99 ProcessObject::ParamNameVectorType ProcessObject::getInputsParamNames() const
100 {
101  return this->getParamNames(m_inputs);
102 }
103 
104 //------------------------------------------------------------------------------
105 
106 ProcessObject::ParamNameVectorType ProcessObject::getOutputsParamNames() const
107 {
108  return this->getParamNames(m_outputs);
109 }
110 
111 //------------------------------------------------------------------------------
112 
114 {
115  this->clearParams(m_outputs);
116 }
117 
118 //------------------------------------------------------------------------------
119 
121 {
122  this->clearParams(m_inputs);
123 }
124 
125 //------------------------------------------------------------------------------
126 
127 void ProcessObject::clearParams(ProcessObjectMapType& params)
128 {
129  params.clear();
130 }
131 
132 //-----------------------------------------------------------------------------
133 
134 void ProcessObject::shallowCopy(const Object::csptr &source )
135 {
136  ProcessObject::csptr other = ProcessObject::dynamicConstCast(source);
137  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
138  "Unable to copy" + (source ? source->getClassname() : std::string("<NULL>"))
139  + " to " + this->getClassname()), !bool(other) );
140  this->fieldShallowCopy( source );
141 
142  m_inputs = other->m_inputs;
143  m_outputs = other->m_outputs;
144 }
145 
146 //-----------------------------------------------------------------------------
147 
148 void ProcessObject::cachedDeepCopy(const Object::csptr &source, DeepCopyCacheType &cache)
149 {
150  ProcessObject::csptr other = ProcessObject::dynamicConstCast(source);
151  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
152  "Unable to copy" + (source ? source->getClassname() : std::string("<NULL>"))
153  + " to " + this->getClassname()), !bool(other) );
154  this->fieldDeepCopy( source, cache );
155 
156  this->clearInputs();
157  this->clearOutputs();
158 
159  for(const ProcessObjectMapType::value_type& elt : other->m_inputs)
160  {
161  m_inputs[elt.first] = ::fwData::Object::copy(elt.second, cache);
162  }
163 
164  for(const ProcessObjectMapType::value_type& elt : other->m_outputs)
165  {
166  m_outputs[elt.first] = ::fwData::Object::copy(elt.second, cache);
167  }
168 }
169 
170 //------------------------------------------------------------------------------
171 
172 } // namespace fwData
173 
FWDATA_API void setValue(const ParamNameType &name,::fwData::Object::sptr object, ProcessObjectMapType &params)
Register value with specified name in params map. If the name does already exist, the matching value ...
Provides the notion of Process Object: object having inputs and outputs.
FWDATA_API void clearOutputs()
Unregister all output parameters.
FWDATA_API void setOutputValue(const ParamNameType &name,::fwData::Object::sptr object)
Register output value with specified name. If the name does already exist, the matching value will be...
ProcessObjectMapType m_outputs
Outputs values map.
FWDATA_API ProcessObject(::fwData::Object::Key key)
Constructor.
virtual FWDATA_API ~ProcessObject()
Destructor.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Implements data exception class.
FWDATA_API ParamNameVectorType getParamNames(const ProcessObjectMapType &params) const
Returns vector of parameters names from params map.
ProcessObjectMapType m_inputs
Inputs values map.
FWDATA_API::fwData::Object::sptr getValue(const ParamNameType &name, const ProcessObjectMapType &params)
Retrieves data associated with specified name in params map (null if non exist).
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
FWDATA_API void clearParams(ProcessObjectMapType &params)
Unregister all parameters in params map.
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.
FWDATA_API void clearInputs()
Unregister all inputs parameters.
FWDATA_API::fwData::Object::sptr getOutput(const ParamNameType &name)
Retrieves the output data associated with specified name (null if non exist).
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
FWDATA_API::fwData::Object::sptr getInput(const ParamNameType &name)
Retrieves the input data associated with specified name (null if non exist).
FWDATA_API void setInputValue(const ParamNameType &name,::fwData::Object::sptr object)
Register input value with specified name. If the name does already exist, the matching value will be ...
Contains the representation of the data objects used in the framework.
FWDATA_API void shallowCopy(const Object::csptr &source) override
Defines shallow copy.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)
FWDATA_API void cachedDeepCopy(const Object::csptr &source, DeepCopyCacheType &cache) override
Defines deep copy.
FWDATA_API ParamNameVectorType getOutputsParamNames() const
Returns vector of output parameters names.
FWDATA_API ParamNameVectorType getInputsParamNames() const
Returns vector of input parameters names.