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 <boost/any.hpp>
14 #include <boost/unordered_map.hpp>
15 
16 #include "IWORKPropertyInfo.h"
17 
18 namespace libetonyek
19 {
20 
24 {
25 public:
27 
28 private:
29  typedef boost::unordered_map<IWORKPropertyID_t, boost::any> Map_t;
30 
31 public:
35 
40  explicit IWORKPropertyMap(const IWORKPropertyMap *parent);
41 
46  IWORKPropertyMap(const IWORKPropertyMap &other);
47 
54 
59  void swap(IWORKPropertyMap &other);
60 
65  void setParent(const IWORKPropertyMap *parent);
66 
76  template<class Property>
77  bool has(bool lookInParent = false) const
78  {
79  const Map_t::const_iterator it = m_map.find(IWORKPropertyInfo<Property>::id);
80  if (m_map.end() != it)
81  return !it->second.empty();
82 
83  if (lookInParent && m_parent)
84  return m_parent->has<Property>(lookInParent);
85 
86  return false;
87  }
88 
89  template<class Property>
90  bool clears(bool lookInParent = false) const
91  {
92  const Map_t::const_iterator it = m_map.find(IWORKPropertyInfo<Property>::id);
93  if (m_map.end() != it)
94  return it->second.empty();
95 
96  if (lookInParent && m_parent)
97  return m_parent->clears<Property>(lookInParent);
98 
99  return false;
100  }
101 
111  template<class Property>
112  const typename IWORKPropertyInfo<Property>::ValueType &get(bool lookInParent = false) const
113  {
114  const Map_t::const_iterator it = m_map.find(IWORKPropertyInfo<Property>::id);
115  if (m_map.end() != it)
116  {
117  if (!it->second.empty())
118  return boost::any_cast<const typename IWORKPropertyInfo<Property>::ValueType &>(it->second);
119  }
120  else if (lookInParent && m_parent)
121  {
122  return m_parent->get<Property>(lookInParent);
123  }
124 
125  throw NotFoundException();
126  }
127 
132  template<class Property>
133  void put(const typename IWORKPropertyInfo<Property>::ValueType &value)
134  {
136  }
137 
143  template<class Property>
144  void clear()
145  {
146  m_map[IWORKPropertyInfo<Property>::id] = boost::any();
147  }
148 
149 private:
150  Map_t m_map;
152 };
153 
154 }
155 
156 #endif // IWORKPROPERTYMAP_H_INCLUDED
157 
158 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
Definition: IWORKBezierElement.cpp:18
const IWORKPropertyMap * m_parent
Definition: IWORKPropertyMap.h:151
void setParent(const IWORKPropertyMap *parent)
Set a new parent for this map.
Definition: IWORKPropertyMap.cpp:47
Represents a (hierarchical) property map.
Definition: IWORKPropertyMap.h:23
void clear()
Clear property.
Definition: IWORKPropertyMap.h:144
void swap(IWORKPropertyMap &other)
Swap the content with another map.
Definition: IWORKPropertyMap.cpp:40
Definition: IWORKPropertyMap.h:26
boost::unordered_map< IWORKPropertyID_t, boost::any > Map_t
Definition: IWORKPropertyMap.h:29
Definition: IWORKPropertyInfo.h:21
IWORKPropertyMap()
Construct an empty map.
Definition: IWORKPropertyMap.cpp:15
Map_t m_map
Definition: IWORKPropertyMap.h:150
bool clears(bool lookInParent=false) const
Definition: IWORKPropertyMap.h:90
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:77
void put(const typename IWORKPropertyInfo< Property >::ValueType &value)
Insert a new value for key key.
Definition: IWORKPropertyMap.h:133
const IWORKPropertyInfo< Property >::ValueType & get(bool lookInParent=false) const
Retrieve the value of a property.
Definition: IWORKPropertyMap.h:112

Generated for libetonyek by doxygen 1.8.10