IWORKPropertyMap.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libetonyek project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #ifndef IWORKPROPERTYMAP_H_INCLUDED
11 #define IWORKPROPERTYMAP_H_INCLUDED
12
13 #include <unordered_map>
14
15 #include <boost/any.hpp>
16
17 #include "IWORKPropertyInfo.h"
18
19 namespace libetonyek
20 {
21
24 class IWORKPropertyMap
25 {
26 public:
27 class NotFoundException {};
28
29 private:
30 typedef std::unordered_map<IWORKPropertyID_t, boost::any> Map_t;
31
32 public:
35 IWORKPropertyMap();
36
41 explicit IWORKPropertyMap(const IWORKPropertyMap *parent);
42
47 IWORKPropertyMap(const IWORKPropertyMap &other);
48
54 IWORKPropertyMap &operator=(const IWORKPropertyMap &other);
55
60 void swap(IWORKPropertyMap &other);
61
66 void setParent(const IWORKPropertyMap *parent);
67
77 template<class Property>
78 bool has(bool lookInParent = false) const
79 {
80 const auto it = m_map.find(IWORKPropertyInfo<Property>::id);
81 if (m_map.end() != it)
82 return !it->second.empty();
83
84 if (lookInParent && m_parent)
85 return m_parent->has<Property>(lookInParent);
86
87 return false;
88 }
89
90 template<class Property>
91 bool clears(bool lookInParent = false) const
92 {
93 const auto it = m_map.find(IWORKPropertyInfo<Property>::id);
94 if (m_map.end() != it)
95 return it->second.empty();
96
97 if (lookInParent && m_parent)
98 return m_parent->clears<Property>(lookInParent);
99
100 return false;
101 }
102
112 template<class Property>
113 const typename IWORKPropertyInfo<Property>::ValueType &get(bool lookInParent = false) const
114 {
115 const auto it = m_map.find(IWORKPropertyInfo<Property>::id);
116 if (m_map.end() != it)
117 {
118 if (!it->second.empty())
119 return boost::any_cast<const typename IWORKPropertyInfo<Property>::ValueType &>(it->second);
120 }
121 else if (lookInParent && m_parent)
122 {
123 return m_parent->get<Property>(lookInParent);
124 }
125
126 throw NotFoundException();
127 }
128
133 template<class Property>
134 void put(const typename IWORKPropertyInfo<Property>::ValueType &value)
135 {
136 m_map[IWORKPropertyInfo<Property>::id] = value;
137 }
138
144 template<class Property>
145 void clear()
146 {
147 m_map[IWORKPropertyInfo<Property>::id] = boost::any();
148 }
149
150 private:
151 Map_t m_map;
152 const IWORKPropertyMap *m_parent;
153 };
154
155 }
156
157 #endif // IWORKPROPERTYMAP_H_INCLUDED
158
159 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
libetonyek
Definition: IWORKBezierElement.cpp:20
libetonyek::IWORKPropertyMap::m_parent
const IWORKPropertyMap * m_parent
Definition: IWORKPropertyMap.h:152
libetonyek::IWORKPropertyMap::setParent
void setParent(const IWORKPropertyMap *parent)
Set a new parent for this map.
Definition: IWORKPropertyMap.cpp:47
libetonyek::IWORKPropertyMap
Represents a (hierarchical) property map.
Definition: IWORKPropertyMap.h:24
libetonyek::IWORKPropertyMap::clear
void clear()
Clear property.
Definition: IWORKPropertyMap.h:145
libetonyek::IWORKToken::value
Definition: IWORKToken.h:624
libetonyek::IWORKPropertyMap::swap
void swap(IWORKPropertyMap &other)
Swap the content with another map.
Definition: IWORKPropertyMap.cpp:40
IWORKPropertyInfo.h
libetonyek::IWORKPropertyMap::get
const IWORKPropertyInfo< Property >::ValueType & get(bool lookInParent=false) const
Retrieve the value of a property.
Definition: IWORKPropertyMap.h:113
libetonyek::IWORKPropertyMap::NotFoundException
Definition: IWORKPropertyMap.h:27
libetonyek::IWORKPropertyInfo
Definition: IWORKPropertyInfo.h:21
libetonyek::IWORKPropertyMap::IWORKPropertyMap
IWORKPropertyMap()
Construct an empty map.
Definition: IWORKPropertyMap.cpp:15
libetonyek::IWORKPropertyMap::m_map
Map_t m_map
Definition: IWORKPropertyMap.h:151
libetonyek::IWORKPropertyMap::Map_t
std::unordered_map< IWORKPropertyID_t, boost::any > Map_t
Definition: IWORKPropertyMap.h:30
libetonyek::IWORKPropertyMap::operator=
IWORKPropertyMap & operator=(const IWORKPropertyMap &other)
Assign the content from another map.
Definition: IWORKPropertyMap.cpp:33
libetonyek::IWORKPropertyMap::has
bool has(bool lookInParent=false) const
Check for the presence of a property.
Definition: IWORKPropertyMap.h:78
libetonyek::IWORKPropertyMap::clears
bool clears(bool lookInParent=false) const
Definition: IWORKPropertyMap.h:91
libetonyek::IWORKPropertyMap::put
void put(const typename IWORKPropertyInfo< Property >::ValueType &value)
Insert a new value for key key.
Definition: IWORKPropertyMap.h:134