- Aldebaran documentation |
- C++ Libraries |
- index

libqi-api 2.8.7.4

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
maptypeinterface.hxx
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2013 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6
7 #ifndef _QITYPE_DETAIL_TYPEMAP_HXX_
8 #define _QITYPE_DETAIL_TYPEMAP_HXX_
9
10 namespace qi
11 {
12 // List container
13 template<typename M> class MapTypeInterfaceImpl:
14 public MapTypeInterface
15 {
16 public:
17 using MethodsImpl = DefaultTypeImplMethods<M, TypeByPointerPOD<M>>;
18 MapTypeInterfaceImpl();
19 TypeInterface* elementType() override;
20 TypeInterface* keyType() override;
21 size_t size(void* storage) override;
22 AnyIterator begin(void* storage) override;
23 AnyIterator end(void* storage) override;
24 void insert(void** storage, void* keyStorage, void* valueStorage) override;
25 AnyReference element(void** storage, void* keyStorage, bool autoInsert) override;
26 _QI_BOUNCE_TYPE_METHODS(MethodsImpl);
27 TypeInterface* _keyType;
28 TypeInterface* _elementType;
29 };
30
31 }
32
33 namespace qi {
34 template<typename M>
35 MapTypeInterfaceImpl<M>::MapTypeInterfaceImpl()
36 {
37 this->_keyType = typeOf<typename M::key_type>();
38 this->_elementType = typeOf<typename M::mapped_type>();
39 }
40
41
42 template<typename M> TypeInterface*
43 MapTypeInterfaceImpl<M>::elementType()
44 {
45 return _elementType;
46 }
47
48 template<typename M> TypeInterface*
49 MapTypeInterfaceImpl<M>::keyType()
50 {
51 return _keyType;
52 }
53
54 template<typename M> size_t
55 MapTypeInterfaceImpl<M>::size(void* storage)
56 {
57 M* ptr = (M*)ptrFromStorage(&storage);
58 return ptr->size();
59 }
60
61
62 template<typename M> AnyIterator
63 MapTypeInterfaceImpl<M>::begin(void* storage)
64 {
65 M* ptr = (M*)ptrFromStorage(&storage);
66 return TypeSimpleIteratorImpl<typename M::iterator>::make(ptr->begin());
67 }
68
69 template<typename M> AnyIterator
70 MapTypeInterfaceImpl<M>::end(void* storage)
71 {
72 M* ptr = (M*)ptrFromStorage(&storage);
73 return TypeSimpleIteratorImpl<typename M::iterator>::make(ptr->end());
74 }
75
76 template<typename M> void
77 MapTypeInterfaceImpl<M>::insert(void** storage, void* keyStorage, void* valueStorage)
78 {
79 M* ptr = (M*) ptrFromStorage(storage);
80 typename M::key_type& key = *(typename M::key_type*)_keyType->ptrFromStorage(&keyStorage);
81 typename M::mapped_type& val = *(typename M::mapped_type*)_elementType->ptrFromStorage(&valueStorage);
82 typename M::iterator it = ptr->find(key);
83 if (it == ptr->end())
84 ptr->insert(std::make_pair(key, val));
85 else
86 it->second = val;
87 }
88
89 template<typename M> AnyReference
90 MapTypeInterfaceImpl<M>::element(void** storage, void* keyStorage, bool autoInsert)
91 {
92 //static TypeInterface* elemType = typeOf<typename M::mapped_type>();
93 M* ptr = (M*) ptrFromStorage(storage);
94 typename M::key_type* key = (typename M::key_type*)_keyType->ptrFromStorage(&keyStorage);
95 typename M::iterator it = ptr->find(*key);
96 if (it == ptr->end())
97 {
98 if (!autoInsert)
99 return AnyReference();
100 typename M::mapped_type& e = (*ptr)[*key];
101 return AnyReference::from(e);
102 }
103 else
104 return AnyReference::from(((typename M::mapped_type&)(it->second)));
105 }
106
107
108
109 template<typename K, typename V, typename C, typename A>
110 struct TypeImpl<std::map<K,V, C, A> >: public MapTypeInterfaceImpl<std::map<K, V,C,A> > {};
111
112 }
113 #endif // _QITYPE_DETAIL_TYPEMAP_HXX_
qi::AnyIterator
Definition: anyiterator.hpp:16
qi::TypeImpl
Definition: typeimpl.hxx:300
qi::MapTypeInterfaceImpl::elementType
TypeInterface * elementType() override
Get the types of the values of the map.
Definition: maptypeinterface.hxx:43
qi::TypeSimpleIteratorImpl::make
static AnyIterator make(const T &val)
Definition: listtypeinterface.hxx:58
qi::MapTypeInterfaceImpl::insert
void insert(void **storage, void *keyStorage, void *valueStorage) override
Set a key to a value and creates it if it does not exist.
Definition: maptypeinterface.hxx:77
qi::MapTypeInterfaceImpl::end
AnyIterator end(void *storage) override
Definition: maptypeinterface.hxx:70
qi::MapTypeInterfaceImpl
Definition: maptypeinterface.hxx:13
qi::MapTypeInterfaceImpl::MapTypeInterfaceImpl
MapTypeInterfaceImpl()
Definition: maptypeinterface.hxx:35
qi::detail::AnyReferenceBase::find
AnyReference find(const K &key)
Definition: anyreference.hxx:282
qi::AnyReference
Definition: anyreference.hpp:360
qi::MapTypeInterfaceImpl::size
size_t size(void *storage) override
Return the number of elements in the map.
Definition: maptypeinterface.hxx:55
qi::MapTypeInterface
Definition: typeinterface.hpp:213
qi::MapTypeInterfaceImpl::_keyType
TypeInterface * _keyType
Definition: maptypeinterface.hxx:27
qi::TypeInterface
Definition: typeinterface.hpp:129
qi::MapTypeInterfaceImpl::element
AnyReference element(void **storage, void *keyStorage, bool autoInsert) override
Definition: maptypeinterface.hxx:90
qi::MapTypeInterfaceImpl::keyType
TypeInterface * keyType() override
Get the types of the keys of the map.
Definition: maptypeinterface.hxx:49
qi::MapTypeInterfaceImpl::_QI_BOUNCE_TYPE_METHODS
_QI_BOUNCE_TYPE_METHODS(MethodsImpl)
qi::DefaultTypeImplMethods
Definition: typeimpl.hxx:228
qi::MapTypeInterfaceImpl::begin
AnyIterator begin(void *storage) override
Return an iterator pointing to the first key-value pair of the map.
Definition: maptypeinterface.hxx:63
qi::detail::AnyReferenceBase::from
static AnyReference from(const T &ref)
Definition: anyreference.hxx:64
qi::MapTypeInterfaceImpl::_elementType
TypeInterface * _elementType
Definition: maptypeinterface.hxx:28