1 ///@file Wrapper class for Nasal hashes
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
19 #ifndef SG_NASAL_HASH_HXX_
20 #define SG_NASAL_HASH_HXX_
22 #include "from_nasal.hxx"
23 #include "to_nasal.hxx"
25 #include <simgear/structure/map.hxx>
26 #include <boost/iterator/iterator_facade.hpp>
38 template<bool> class Entry;
39 template<bool> class Iterator;
41 typedef Entry<false> reference;
42 typedef Entry<true> const_reference;
43 typedef Iterator<false> iterator;
44 typedef Iterator<true> const_iterator;
47 * Create a new Nasal Hash
49 * @param c Nasal context for creating the hash
51 explicit Hash(naContext c);
54 * Initialize from an existing Nasal Hash
56 * @param hash Existing Nasal Hash
57 * @param c Nasal context for creating new Nasal objects
59 Hash(naRef hash, naContext c);
63 const_iterator begin() const;
64 const_iterator end() const;
69 * @param name Member name
70 * @param ref Reference to Nasal object (naRef)
72 void set(const std::string& name, naRef ref);
75 * Set member to anything convertible using to_nasal
77 * @param name Member name
78 * @param val Value (has to be convertible with to_nasal)
81 void set(const std::string& name, const T& val)
83 set(name, to_nasal(_context, val));
89 * @param key Member key
91 naRef get(naRef key) const;
96 * @param name Member name
98 naRef get(const std::string& name) const;
101 * Get member converted to given type
103 * @tparam T Type to convert to (using from_nasal)
104 * @param name Member name
106 template<class T, class Key>
107 T get(const Key& name) const
109 BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
110 || boost::is_convertible<Key, std::string>::value
113 return from_nasal<T>(_context, get(name));
117 * Get member converted to callable object
119 * @tparam Sig Function signature
120 * @param name Member name
121 * @param key Member key
123 template<class Sig, class Key>
124 typename boost::enable_if< boost::is_function<Sig>,
127 get(const Key& name) const
129 BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
130 || boost::is_convertible<Key, std::string>::value
133 return from_nasal_helper(_context, get(name), static_cast<Sig*>(0));
137 * Returns the number of entries in the hash
142 * Get a list of all keys
144 std::vector<std::string> keys() const;
147 * Create a new child hash (module)
149 * @param name Name of the new hash inside this hash
151 Hash createHash(const std::string& name);
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.
158 void setContext(naContext context);
161 * Get Nasal representation of Hash
163 naRef get_naRef() const;
166 * Get Nasal vector of keys
168 naRef get_naRefKeys() const;
171 template<bool is_const>
175 typedef typename boost::mpl::if_c<
181 Entry(HashPtr hash, naRef key):
186 assert(naIsScalar(key));
189 std::string getKey() const
191 if( !_hash || naIsNil(_key) )
192 return std::string();
194 return from_nasal<std::string>(_hash->_context, _key);
200 if( !_hash || naIsNil(_key) )
203 return _hash->template get<T>(_key);
213 template<bool is_const>
215 public boost::iterator_facade<
218 boost::bidirectional_traversal_tag,
223 typedef typename Entry<is_const>::HashPtr HashPtr;
224 typedef Entry<is_const> value_type;
231 Iterator(HashPtr hash, int index):
237 * Convert from iterator to const_iterator or copy within same type
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,
249 friend class boost::iterator_core_access;
250 template <bool> friend class Iterator;
255 template<bool is_other_const>
256 bool equal(Iterator<is_other_const> const& other) const
258 return _hash == other._hash
259 && _index == other._index;
262 void increment() { ++_index; }
263 void decrement() { --_index; }
265 value_type dereference() const
267 return value_type(_hash, naVec_get(_hash->get_naRefKeys(), _index));
276 mutable naRef _keys; //< Store vector of keys (for iterators)
282 template<class Value>
283 simgear::Map<std::string, Value>
284 from_nasal_helper( naContext c,
286 const simgear::Map<std::string, Value>* )
288 nasal::Hash hash = from_nasal_helper(c, ref, static_cast<nasal::Hash*>(0));
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>();
297 #endif /* SG_NASAL_HASH_HXX_ */