Crazy Eddie's GUI System  0.8.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
NamedDefinitionCollator.h
1 /***********************************************************************
2  filename: NamedDefinitionCollator.h
3  created: Mon Jun 11 2012
4  author: Paul D Turner <paul@cegui.org.uk>
5 *************************************************************************/
6 /***************************************************************************
7  * Copyright (C) 2004 - 2012 Paul D Turner & The CEGUI Development Team
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  ***************************************************************************/
28 #ifndef _CEGUINamedDefinitionCollator_h_
29 #define _CEGUINamedDefinitionCollator_h_
30 
31 #include "CEGUI/Base.h"
32 #include <vector>
33 #include <algorithm>
34 
35 #if defined(_MSC_VER)
36 # pragma warning(push)
37 # pragma warning(disable : 4251)
38 #endif
39 
40 namespace CEGUI
41 {
47 template<typename K, typename V>
49 {
50 public:
51  typedef V value_type;
52 
54  size_t size() const
55  { return d_values.size(); }
56 
58  V& at(size_t idx)
59  { return d_values.at(idx).second; }
60 
62  const V& at(size_t idx) const
63  { return d_values.at(idx).second; }
64 
71  void set(const K& key, const V& val)
72  {
73  typename ValueArray::iterator i =
74  std::find_if(d_values.begin(), d_values.end(), pred(key));
75 
76  if (i != d_values.end())
77  d_values.erase(i);
78 
79  d_values.push_back(std::make_pair(key, val));
80  }
81 
82 protected:
83  typedef std::pair<K, V> Entry;
84  typedef std::vector<Entry CEGUI_VECTOR_ALLOC(Entry)> ValueArray;
85 
86  struct pred
87  {
88  const K& d_k;
89  pred(const K& k) : d_k(k) {}
90  bool operator()(const Entry& e)
91  { return e.first == d_k; }
92  };
93 
94  ValueArray d_values;
95 
96 public:
101  {
102  public:
103  const_iterator(typename ValueArray::const_iterator i) :
104  d_iter(i) {}
105 
106  const_iterator(const const_iterator& iter) :
107  d_iter(iter.d_iter) {}
108 
109  const V& operator*() const
110  { return d_iter->second; }
111 
112  const V* operator->() const
113  { return &**this; }
114 
115  bool operator==(const const_iterator& iter) const
116  { return d_iter == iter.d_iter; }
117 
118  bool operator!=(const const_iterator& iter) const
119  { return !operator==(iter); }
120 
121  const_iterator& operator++()
122  {
123  ++d_iter;
124  return *this;
125  }
126 
127  const_iterator& operator++(int)
128  {
129  const_iterator& tmp = *this;
130  ++*this;
131  return tmp;
132  }
133 
134  const_iterator& operator--()
135  {
136  --d_iter;
137  return *this;
138  }
139 
140  const_iterator& operator--(int)
141  {
142  const_iterator& tmp = *this;
143  --*this;
144  return tmp;
145  }
146 
147  const_iterator& operator=(const const_iterator& iter)
148  {
149  d_iter = iter.d_iter;
150  return *this;
151  }
152 
153  protected:
154  typename ValueArray::const_iterator d_iter;
155  };
156 
157  const_iterator begin() const
158  { return const_iterator(d_values.begin()); }
159 
160  const_iterator end() const
161  { return const_iterator(d_values.end()); }
162 
163  const_iterator find(const K& key) const
164  {
165  return const_iterator(std::find_if(d_values.begin(),
166  d_values.end(),
167  pred(key)));
168  }
169 
170 };
171 
172 }
173 
174 #if defined(_MSC_VER)
175 # pragma warning(pop)
176 #endif
177 
178 #endif
179