fw4spl
MapPropertyImpl.hxx
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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 #ifndef __FWCAMP_CAMP_DETAIL_MAPPROPERTYIMPL_HXX__
8 #define __FWCAMP_CAMP_DETAIL_MAPPROPERTYIMPL_HXX__
9 
10 #include "fwCamp/camp/customtype.hpp"
11 #include <fwCamp/Mapper/ValueMapper.hpp>
12 
13 #include <camp/class.hpp>
14 #include <camp/classbuilder.hpp>
15 #include <camp/userobject.hpp>
16 
17 namespace camp
18 {
19 
20 namespace detail
21 {
22 
23 template <typename A>
24 MapPropertyImpl<A>::MapPropertyImpl(const std::string& name, const A& accessor) :
25  camp::MapProperty(name, camp::mapType<ValueType>())
26  ,
27  m_accessor(accessor)
28 {
29 }
30 
31 //------------------------------------------------------------------------------
32 
33 template <typename A>
34 std::size_t MapPropertyImpl<A>::getSize(const UserObject& object) const
35 {
36  return Mapper::size(map(object));
37 }
38 //------------------------------------------------------------------------------
39 
40 template <typename A>
41 void MapPropertyImpl<A>::set(const UserObject& object, const Value& key, const Value& value) const
42 {
43  const typename Mapper::KeyType& typedKey = key.to< typename Mapper::KeyType >();
44  const typename Mapper::MappedType& typedValue = value.to< typename Mapper::MappedType >();
45 
46  Mapper::set(map(object), typedKey, typedValue );
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 template <typename A>
52 MapProperty::ValuePair MapPropertyImpl<A>::getElement(const UserObject& object, std::size_t index ) const
53 {
54 
55  ValueType p(Mapper::get(map(object), index));
56  return ValuePair(p.first, p.second);
57 }
58 
59 template <typename A>
60 typename MapPropertyImpl<A>::MapType&
61 MapPropertyImpl<A>::map(const UserObject &object) const
62 {
63  return m_accessor.get(object.get<typename A::ClassType>());
64 }
65 
66 /*
67  * Instantiate map properties
68  */
69 template <typename A>
70 struct PropertyMapper<A, camp::mappingType>
71 {
72  typedef MapPropertyImpl<A> Type;
73 };
74 
75 } // namespace detail
76 } // namespace camp
77 
78 #endif /* __FWCAMP_CAMP_DETAIL_MAPPROPERTYIMPL_HXX__*/