Wolframe, 0.0.3

keymap.hpp
Go to the documentation of this file.
1 /************************************************************************
2 Copyright (C) 2011 - 2014 Project Wolframe.
3 All rights reserved.
4 
5 This file is part of Project Wolframe.
6 
7 Commercial Usage
8 Licensees holding valid Project Wolframe Commercial licenses may
9 use this file in accordance with the Project Wolframe
10 Commercial License Agreement provided with the Software or,
11 alternatively, in accordance with the terms contained
12 in a written agreement between the licensee and Project Wolframe.
13 
14 GNU General Public License Usage
15 Alternatively, you can redistribute this file and/or modify it
16 under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19 
20 Wolframe is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
27 
28 If you have questions regarding the use of this file, please contact
29 Project Wolframe.
30 
31 ************************************************************************/
34 
35 #ifndef _Wolframe_TYPES_KEYMAP_HPP_INCLUDED
36 #define _Wolframe_TYPES_KEYMAP_HPP_INCLUDED
37 #include "types/traits.hpp"
38 #include <map>
39 #include <string>
40 #include <algorithm>
41 #include <boost/algorithm/string.hpp>
42 #include <boost/utility/enable_if.hpp>
43 #include <boost/type_traits.hpp>
44 #include <boost/detail/select_type.hpp>
45 
46 namespace _Wolframe {
47 namespace types {
48 
51 struct keystring
52  :public std::string
53 {
54  keystring( const char* o)
55  :std::string( boost::to_upper_copy( std::string(o))){}
56  keystring( const std::string& o)
57  :std::string( boost::to_upper_copy( o)){}
58  keystring( const keystring& o)
59  :std::string( o){}
61 };
62 
65 template <typename ValueType>
66 struct keymap
67  :public std::map<keystring,ValueType>
68 {
69  typedef std::map<keystring,ValueType> Parent;
70 
71  keymap(){}
72  keymap( const keymap& o)
73  :std::map<keystring,ValueType>(o){}
74  keymap( const std::map<std::string,ValueType>& o)
75  {
76  std::copy( o.begin(), o.end(), std::inserter( *this, this->end()));
77  }
78 
79  void insert( const keystring& key, const ValueType& value)
80  {
81  if (Parent::find( key) != this->end())
82  {
83  throw std::runtime_error( std::string( "duplicate definition of '") + key + "'");
84  }
85  Parent::operator[](key) = value;
86  }
87 
88  void insert( const keymap& m)
89  {
90  typename Parent::const_iterator ki = m.begin(), ke = m.end();
91  for (; ki != ke; ++ki) insert( ki->first, ki->second);
92  }
93 
94  void join( const keymap& m)
95  {
96  typename Parent::const_iterator ki = m.begin(), ke = m.end();
97  for (; ki != ke; ++ki)
98  {
99  typename Parent::const_iterator fi = Parent::find( ki->first);
100  if (fi != this->end())
101  {
102  if (ki->second != fi->second)
103  {
104  throw std::runtime_error( std::string( "duplicate definition of '") + ki->first + "'");
105  }
106  }
107  else
108  {
109  Parent::operator[](ki->first) = ki->second;
110  }
111  }
112  }
113 
114  template <class KeyList>
115  typename boost::enable_if_c<
117  ,KeyList>::type
118  getkeys() const
119  {
120  KeyList rt;
121  typename Parent::const_iterator ki = Parent::begin(), ke = Parent::end();
122  for (; ki != ke; ++ki) rt.push_back( ki->first);
123  return rt;
124  }
125 };
126 
127 }} //namespace
128 #endif
129 
Constructor for implementing implicit type reductions in key maps that store the key as upper case st...
Definition: keymap.hpp:51
Map with case insensitive strings as keys.
Definition: keymap.hpp:66
void join(const keymap &m)
Definition: keymap.hpp:94
keymap(const std::map< std::string, ValueType > &o)
Definition: keymap.hpp:74
void insert(const keymap &m)
Definition: keymap.hpp:88
keymap(const keymap &o)
Definition: keymap.hpp:72
Type traits.
std::map< keystring, ValueType > Parent
Definition: keymap.hpp:69
void insert(const keystring &key, const ValueType &value)
Definition: keymap.hpp:79
keystring(const std::string &o)
Definition: keymap.hpp:56
keymap()
Definition: keymap.hpp:71
keystring(const keystring &o)
Definition: keymap.hpp:58
boost::enable_if_c< types::traits::is_back_insertion_sequence< KeyList >::value,KeyList >::type getkeys() const
Definition: keymap.hpp:118
keystring()
Definition: keymap.hpp:60
Enable-if template for deciding wheter elements can be added to the class T with 'push_back' as for e...
Definition: traits.hpp:45
keystring(const char *o)
Definition: keymap.hpp:54