GestureRecognitionToolkit  Version: 0.2.5
The Gesture Recognition Toolkit (GRT) is a cross-platform, open-source, c++ machine learning library for real-time gesture recognition.
Dict.h
1 /*
2 GRT MIT License
3 Copyright (c) <2012> <Nicholas Gillian, Media Lab, MIT>
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
5 and associated documentation files (the "Software"), to deal in the Software without restriction,
6 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
8 subject to the following conditions:
9 The above copyright notice and this permission notice shall be included in all copies or substantial
10 portions of the Software.
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
12 LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
13 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
14 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
15 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 */
17 
18 #ifndef GRT_DICT_HEADER
19 #define GRT_DICT_HEADER
20 
21 #include "GRTTypedefs.h"
22 #include "DynamicType.h"
23 
24 GRT_BEGIN_NAMESPACE
25 
31 class Dict {
32  typedef std::map< std::string, DynamicType > dict;
33 public:
37  Dict() {
38  }
39 
44  Dict(const Dict &rhs) {
45  for ( auto const& x : rhs.data ) {
46  this->data[ x.first ] = x.second;
47  }
48  }
49 
53  ~Dict() {
54  }
55 
61  Dict& operator= (const Dict &rhs) {
62  if( this != &rhs ) {
63  this->clear();
64  for ( auto const& x : rhs.data ) {
65  this->data[ x.first ] = x.second;
66  }
67  }
68  return *this;
69  }
70 
76  Dict& operator+ (const Dict &rhs) {
77  if( this != &rhs ) {
78  for ( auto const& x : rhs.data ) {
79  this->data[ x.first ] = x.second;
80  }
81  }
82  return *this;
83  }
84 
90  Dict& operator- (const Dict &rhs) {
91  if( this != &rhs ) {
92  for ( auto const& x : rhs.data ) {
93  if( this->exists( x.first ) ) {
94  this->remove( x.first );
95  }
96  }
97  }
98  return *this;
99  }
100 
108  template<typename T>
109  bool operator() (const std::string &key, T &value) {
110  try {
111  value = get<T>(key);
112  return true;
113  }
114  catch(...) {
115  return false;
116  }
117  }
118 
127  template<typename T>
128  bool add( const std::string &key, const T &value, const bool overwrite = true ) {
129  if( overwrite ) {
130  data[ key ].set( value );
131  return true;
132  }
133  dict::iterator it;
134  it = data.find( key );
135  if( it == data.end() ) {
136  data[ key ].set( value );
137  return true;
138  }
139  return false;
140  }
141 
147  bool remove( const std::string &key ) {
148  dict::iterator it;
149  it = data.find( key );
150  if( it != data.end() ) {
151  data.erase( it );
152  return true;
153  }
154  return false;
155  }
156 
162  bool exists( const std::string &key ) {
163  dict::iterator it;
164  it = data.find( key );
165  if( it != data.end() ) {
166  return true;
167  }
168  return false;
169  }
170 
175  bool clear( ) {
176  data.clear();
177  return true;
178  }
179 
184  bool empty( ) const {
185  return data.empty();
186  }
187 
192  UINT getSize() const {
193  return (UINT)data.size();
194  }
195 
202  keys.reserve( data.size() );
203  for ( auto const& x : data ) {
204  keys.push_back( x.first );
205  }
206  return keys;
207  }
208 
214  template<typename T>
215  T& get( const std::string &key ) {
216  dict::iterator it;
217  it = data.find( key );
218  if( it == data.end() ) {
219  throw "Unknown key: " + key;
220  }
221  return it->second.get< T >();
222  }
223 
230  template<typename T>
231  bool set( const std::string &key, const T &value ) {
232  dict::iterator it;
233  it = data.find( key );
234  if( it == data.end() ) {
235  return false;
236  }
237  return it->second.set( value );
238  }
239 
240 protected:
241  dict data;
242 };
243 
244 GRT_END_NAMESPACE
245 
246 #endif //GRT_HEADER_GUARD
This class implements a flexible dictionary that supports multiple data types. Elements in the dictio...
Definition: Dict.h:31
UINT getSize() const
returns the size of the dictionary, which is the number of elements stored in the dictionary ...
Definition: Dict.h:192
Vector< std::string > getKeys() const
returns a Vector of strings containing the keys in the dictionary, the vector will be empty if the di...
Definition: Dict.h:200
bool exists(const std::string &key)
returns true if the key exists in the current dictionary
Definition: Dict.h:162
bool operator()(const std::string &key, T &value)
Access operator, assigns a reference of the value that matches the key, returning true if the value e...
Definition: Dict.h:109
bool clear()
clears all elements in the dictionary
Definition: Dict.h:175
Dict & operator+(const Dict &rhs)
Addition operator, copies the keys and values from the rhs dictionary to this dictionary, adding them to any existing values in this dictionary.
Definition: Dict.h:76
Dict & operator-(const Dict &rhs)
Subtraction operator, removes any key-value pairs from this dictionary that match the keys in the rhs...
Definition: Dict.h:90
Dict(const Dict &rhs)
Copy Constructor, copies the keys and values from the rhs dictionary to this dictionary.
Definition: Dict.h:44
~Dict()
Default Destructor.
Definition: Dict.h:53
dict data
The internal buffer used to store elements in the dictionary.
Definition: Dict.h:241
Dict & operator=(const Dict &rhs)
Equals operator, copies the keys and values from the rhs dictionary to this dictionary, clearing any elements in this dictionary first.
Definition: Dict.h:61
Definition: Vector.h:41
Dict()
Default Constructor.
Definition: Dict.h:37
bool empty() const
returns true if the dictionary is empty, false otherwise
Definition: Dict.h:184
bool add(const std::string &key, const T &value, const bool overwrite=true)
adds a new key-value pair to the dictionary, if the overwrite option is true then any matching key al...
Definition: Dict.h:128