]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalHash.hxx
Fix for old versions of Boost.
[simgear.git] / simgear / nasal / cppbind / NasalHash.hxx
1 ///@file Wrapper class for Nasal hashes
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #ifndef SG_NASAL_HASH_HXX_
20 #define SG_NASAL_HASH_HXX_
21
22 #include "from_nasal.hxx"
23 #include "to_nasal.hxx"
24
25 #include <simgear/structure/map.hxx>
26 #include <boost/iterator/iterator_facade.hpp>
27
28 namespace nasal
29 {
30
31   /**
32    * A Nasal Hash
33    */
34   class Hash
35   {
36     public:
37
38       template<bool> class Entry;
39       template<bool> class Iterator;
40
41       typedef Entry<false>      reference;
42       typedef Entry<true>       const_reference;
43       typedef Iterator<false>   iterator;
44       typedef Iterator<true>    const_iterator;
45
46       /**
47        * Create a new Nasal Hash
48        *
49        * @param c   Nasal context for creating the hash
50        */
51       explicit Hash(naContext c);
52
53       /**
54        * Initialize from an existing Nasal Hash
55        *
56        * @param hash  Existing Nasal Hash
57        * @param c     Nasal context for creating new Nasal objects
58        */
59       Hash(naRef hash, naContext c);
60
61       iterator begin();
62       iterator end();
63       const_iterator begin() const;
64       const_iterator end() const;
65
66       /**
67        * Set member
68        *
69        * @param name    Member name
70        * @param ref     Reference to Nasal object (naRef)
71        */
72       void set(const std::string& name, naRef ref);
73
74       /**
75        * Set member to anything convertible using to_nasal
76        *
77        * @param name    Member name
78        * @param val     Value (has to be convertible with to_nasal)
79        */
80       template<class T>
81       void set(const std::string& name, const T& val)
82       {
83         set(name, to_nasal(_context, val));
84       }
85
86       /**
87        * Get member
88        *
89        * @param key    Member key
90        */
91       naRef get(naRef key) const;
92
93       /**
94        * Get member
95        *
96        * @param name    Member name
97        */
98       naRef get(const std::string& name) const;
99
100       /**
101        * Get member converted to given type
102        *
103        * @tparam T      Type to convert to (using from_nasal)
104        * @param name    Member name
105        */
106       template<class T, class Key>
107       T get(const Key& name) const
108       {
109         BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
110                            || boost::is_convertible<Key, std::string>::value
111                            ));
112
113         return from_nasal<T>(_context, get(name));
114       }
115
116       /**
117        * Get member converted to callable object
118        *
119        * @tparam Sig    Function signature
120        * @param name    Member name
121        * @param key     Member key
122        */
123       template<class Sig, class Key>
124       typename boost::enable_if< boost::is_function<Sig>,
125                                  boost::function<Sig>
126                                >::type
127       get(const Key& name) const
128       {
129         BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
130                            || boost::is_convertible<Key, std::string>::value
131                            ));
132
133         return from_nasal_helper(_context, get(name), static_cast<Sig*>(0));
134       }
135
136       /**
137        * Returns the number of entries in the hash
138        */
139       int size() const;
140
141       /**
142        * Get a list of all keys
143        */
144       std::vector<std::string> keys() const;
145
146       /**
147        * Create a new child hash (module)
148        *
149        * @param name  Name of the new hash inside this hash
150        */
151       Hash createHash(const std::string& name);
152
153       /**
154        * Set a new Nasal context. Eg. in FlightGear the context changes every
155        * frame, so if using the same Hash instance for multiple frames you have
156        * to update the context before using the Hash object.
157        */
158       void setContext(naContext context);
159
160       /**
161        * Get Nasal representation of Hash
162        */
163       naRef get_naRef() const;
164
165       /**
166        * Get Nasal vector of keys
167        */
168       naRef get_naRefKeys() const;
169
170       /// Hash entry
171       template<bool is_const>
172       class Entry
173       {
174         public:
175           typedef typename boost::mpl::if_c<
176             is_const,
177             Hash const*,
178             Hash*
179           >::type HashPtr;
180
181           Entry(HashPtr hash, naRef key):
182             _hash(hash),
183             _key(key)
184           {
185             assert(hash);
186             assert(naIsScalar(key));
187           }
188
189           std::string getKey() const
190           {
191             if( !_hash || naIsNil(_key) )
192               return std::string();
193
194             return from_nasal<std::string>(_hash->_context, _key);
195           }
196
197           template<class T>
198           T getValue() const
199           {
200             if( !_hash || naIsNil(_key) )
201               return T();
202
203             return _hash->template get<T>(_key);
204           }
205
206         private:
207           HashPtr _hash;
208           naRef _key;
209
210       };
211
212       /// Hash iterator
213       template<bool is_const>
214       class Iterator:
215         public boost::iterator_facade<
216           Iterator<is_const>,
217           Entry<is_const>,
218           boost::bidirectional_traversal_tag,
219           Entry<is_const>
220         >
221       {
222         public:
223           typedef typename Entry<is_const>::HashPtr HashPtr;
224           typedef Entry<is_const>                   value_type;
225
226           Iterator():
227             _hash(NULL),
228             _index(0)
229           {}
230
231           Iterator(HashPtr hash, int index):
232             _hash(hash),
233             _index(index)
234           {}
235
236           /**
237            * Convert from iterator to const_iterator or copy within same type
238            */
239           template<bool is_other_const>
240           Iterator( Iterator<is_other_const> const& other,
241                     typename boost::enable_if_c< is_const || !is_other_const,
242                                                  void*
243                                                >::type = NULL ):
244             _hash(other._hash),
245             _index(other._index)
246           {}
247
248         private:
249           friend class boost::iterator_core_access;
250           template <bool> friend class Iterator;
251
252           HashPtr _hash;
253           int _index;
254
255           template<bool is_other_const>
256           bool equal(Iterator<is_other_const> const& other) const
257           {
258             return _hash == other._hash
259                 && _index == other._index;
260           }
261
262           void increment() { ++_index; }
263           void decrement() { --_index; }
264
265           value_type dereference() const
266           {
267             return value_type(_hash, naVec_get(_hash->get_naRefKeys(), _index));
268           }
269       };
270
271     protected:
272
273       naRef _hash;
274       naContext _context;
275
276       mutable naRef _keys; //< Store vector of keys (for iterators)
277
278   };
279
280 } // namespace nasal
281
282 template<class Value>
283 simgear::Map<std::string, Value>
284 from_nasal_helper( naContext c,
285                    naRef ref,
286                    const simgear::Map<std::string, Value>* )
287 {
288   nasal::Hash hash = from_nasal_helper(c, ref, static_cast<nasal::Hash*>(0));
289
290   simgear::Map<std::string, Value> map;
291   for(nasal::Hash::const_iterator it = hash.begin(); it != hash.end(); ++it)
292     map[ it->getKey() ] = it->getValue<Value>();
293
294   return map;
295 }
296
297 #endif /* SG_NASAL_HASH_HXX_ */