7 #include "fwDataCamp/visitor/CompareObjects.hpp" 9 #include <fwCore/Exception.hpp> 11 #include <fwData/camp/mapper.hpp> 13 #include <fwMemory/BufferObject.hpp> 15 #include <boost/functional/hash.hpp> 27 typedef CompareObjects::PropsMapType::value_type PropType;
47 PropType operator()(camp::NoType value)
49 return std::make_pair(
"",
"");
54 PropType operator()(
bool value)
56 return std::make_pair(m_prefix, value ?
"true" :
"false");
61 PropType operator()(
long value)
63 return std::make_pair(m_prefix, ::boost::lexical_cast<std::string>(value));
68 PropType operator()(
double value)
70 return std::make_pair(m_prefix, ::boost::lexical_cast<std::string>(value));
75 PropType operator()(
const std::string& value)
77 return std::make_pair(m_prefix, value);
82 PropType operator()(
const camp::EnumObject& value)
84 return std::make_pair(m_prefix, value.name());
89 PropType operator()(
const camp::UserObject& value)
91 const camp::Class& metaclass = value.getClass();
96 std::string classname = value.call(
"classname").to<std::string>();
98 if (metaclass.name() != classname)
100 const camp::Class& newMetaclass = ::camp::classByName(classname);
102 newMetaclass.visit(visitor);
104 else if(classname ==
"::fwMemory::BufferObject")
112 char* buffer =
static_cast< char*
>(lock.
getBuffer());
113 std::size_t seed = 0;
114 const std::size_t buffsize = bo->
getSize();
115 for(
size_t i = 0; i < buffsize; ++i)
117 ::boost::hash_combine(seed, buffer[i]);
119 return std::make_pair(m_prefix, ::boost::lexical_cast< std::string >(seed));
126 metaclass.visit(visitor);
131 OSLM_INFO(
"try visiting class= '" << metaclass.name() <<
" but a null pointer was found");
147 const ::camp::UserObject& obj,
const std::string& prefix,
SPTR(
PropsMapType)props) :
156 CompareObjects::~CompareObjects()
162 void CompareObjects::visit(
const camp::SimpleProperty& property)
165 const std::string name( property.name() );
167 ::camp::Value elemValue =
property.get(m_campObj);
169 PropType pt = elemValue.visit(visitor);
170 if(!pt.first.empty())
172 m_props->insert(m_props->end(), pt);
178 void CompareObjects::visit(
const camp::EnumProperty& property)
181 const std::string name( property.name() );
182 ::camp::Value elemValue =
property.get(m_campObj);
185 PropType pt = elemValue.visit(visitor);
186 if(!pt.first.empty())
188 m_props->insert(m_props->end(), pt);
197 const std::string name(property.name());
200 std::pair< ::camp::Value, ::camp::Value > value;
202 for (
unsigned int i = 0; i <
property.getSize(m_campObj); ++i)
204 value =
property.getElement(m_campObj, i);
205 mapKey = value.first.to< std::string >();
207 PropType pt = value.second.visit(visitor);
208 if(!pt.first.empty())
210 m_props->insert(m_props->end(), pt);
217 void CompareObjects::visit(
const camp::ArrayProperty& property)
220 const std::string name(property.name());
223 for(
unsigned int i = 0; i <
property.size(m_campObj); ++i)
225 ::camp::Value elemValue =
property.get(m_campObj, i);
226 std::stringstream ss;
227 ss << name <<
"." << i;
229 PropType pt = elemValue.visit(visitor);
230 if(!pt.first.empty())
232 m_props->insert(m_props->end(), pt);
239 void CompareObjects::visit(
const camp::UserProperty& property)
242 const std::string name( property.name() );
244 ::camp::Value elemValue =
property.get( m_campObj );
246 if(m_campObj.call(
"is_a", ::camp::Args(
"::fwData::Object")).to<
bool>())
249 PropType pt = elemValue.visit(visitor);
250 if(!pt.first.empty())
252 m_props->insert(m_props->end(), pt);
259 void CompareObjects::visit(
const camp::Function&
function)
266 std::string CompareObjects::getPath(
const std::string& property)
const 268 return m_prefix + (!m_prefix.empty() ?
"." :
"") + property;
277 std::stringstream ss;
278 ss <<
"Classnames mismatch : '" << objRef->
getClassname() <<
"' (reference object) vs. '" 280 throw ::fwCore::Exception(ss.str());
286 SLM_ASSERT(
"Reference object not defined", m_objRef);
287 m_campObj = ::camp::UserObject(m_objRef.get());
288 const ::camp::Class& classRef = ::camp::classByName(m_objRef->getClassname());
289 classRef.visit(*
this);
290 m_propsRef = std::move(*m_props);
293 SLM_ASSERT(
"Reference object not defined", m_objComp);
294 m_campObj = ::camp::UserObject(m_objComp.get());
295 const ::camp::Class& classComp = ::camp::classByName(m_objComp->getClassname());
296 classComp.visit(*
this);
297 m_propsComp = std::move(*m_props);
300 for(PropsMapType::value_type prop : m_propsComp)
302 if(m_propsRef.find(prop.first) != m_propsRef.end())
304 if(m_propsRef[prop.first] != prop.second)
306 (*m_props)[prop.first] = prop.second;
311 (*m_props)[prop.first] = s_MISSING_PROPERTY;
315 for(PropsMapType::value_type prop : m_propsRef)
317 if(m_propsComp.find(prop.first) == m_propsComp.end() && m_props->find(prop.first) == m_props->end())
319 (*m_props)[prop.first] = s_MISSING_PROPERTY;
SizeType getSize() const
Returns the buffer's size.
FWDATACAMP_API CompareObjects()
Constructor.
#define SLM_TRACE_FUNC()
Trace contextual function signature.
static FWDATACAMP_API const std::string s_MISSING_PROPERTY
Property value for differences map meaning a missing property.
virtual const std::string & getClassname() const override
return full object's classname with its namespace, i.e. fwCore::BaseObject
#define OSLM_INFO(message)
Visitor used to compare two objects.
base class for BufferObject Lock
LockBase< T >::BufferType getBuffer() const
Returns BufferObject's buffer pointer.
This namespace contains data object descriptions used for introspection.
FWDATACAMP_API void compare(std::shared_ptr< ::fwData::Object > objRef, std::shared_ptr< ::fwData::Object > objComp)
Process to the comparison between reference object and compared object. When the process completes...
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
Base class for each data object.
virtual FWMEMORY_API Lock lock()
Return a lock on the BufferObject.
Define Base class for FW4SPL buffers.
std::map< std::string, std::string > PropsMapType
Associates property path (within its parent object) to its value.
#define OSLM_DEBUG(message)