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 
25 {
26 public:
28 
29 private:
30  typedef std::unordered_map<IWORKPropertyID_t, boost::any> Map_t;
31 
32 public:
36 
41  explicit IWORKPropertyMap(const IWORKPropertyMap *parent);
42 
47  IWORKPropertyMap(const IWORKPropertyMap &other);
48 
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>
135  {
137  }
138 
144  template<class Property>
145  void clear()
146  {
147  m_map[IWORKPropertyInfo<Property>::id] = boost::any();
148  }
149 
150 private:
153 };
154 
155 }
156 
157 #endif // IWORKPROPERTYMAP_H_INCLUDED
158 
159 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
const IWORKPropertyMap * m_parent
Definition: IWORKPropertyMap.h:152
void setParent(const IWORKPropertyMap *parent)
Set a new parent for this map.
Definition: IWORKPropertyMap.cpp:47
Represents a (hierarchical) property map.
Definition: IWORKPropertyMap.h:24
void clear()
Clear property.
Definition: IWORKPropertyMap.h:145
Definition: IWORKToken.h:631
void swap(IWORKPropertyMap &other)
Swap the content with another map.
Definition: IWORKPropertyMap.cpp:40
Definition: IWORKPropertyMap.h:27
Definition: IWORKPropertyInfo.h:21
IWORKPropertyMap()
Construct an empty map.
Definition: IWORKPropertyMap.cpp:15
Map_t m_map
Definition: IWORKPropertyMap.h:151
bool clears(bool lookInParent=false) const
Definition: IWORKPropertyMap.h:91
std::unordered_map< IWORKPropertyID_t, boost::any > Map_t
Definition: IWORKPropertyMap.h:30
IWORKPropertyMap & operator=(const IWORKPropertyMap &other)
Assign the content from another map.
Definition: IWORKPropertyMap.cpp:33
bool has(bool lookInParent=false) const
Check for the presence of a property.
Definition: IWORKPropertyMap.h:78
void put(const typename IWORKPropertyInfo< Property >::ValueType &value)
Insert a new value for key key.
Definition: IWORKPropertyMap.h:134
const IWORKPropertyInfo< Property >::ValueType & get(bool lookInParent=false) const
Retrieve the value of a property.
Definition: IWORKPropertyMap.h:113

Generated for libetonyek by doxygen 1.8.6