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>
38 * Create a new Nasal Hash
40 * @param c Nasal context for creating the hash
45 * Initialize from an existing Nasal Hash
47 * @param hash Existing Nasal Hash
48 * @param c Nasal context for creating new Nasal objects
50 Hash(naRef hash, naContext c);
55 * @param name Member name
56 * @param ref Reference to Nasal object (naRef)
58 void set(const std::string& name, naRef ref);
61 * Set member to anything convertible using to_nasal
63 * @param name Member name
64 * @param val Value (has to be convertible with to_nasal)
67 void set(const std::string& name, const T& val)
69 set(name, to_nasal(_context, val));
75 * @param name Member name
77 naRef get(const std::string& name);
80 * Get member converted to given type
82 * @tparam T Type to convert to (using from_nasal)
83 * @param name Member name
86 T get(const std::string& name)
88 return from_nasal<T>(_context, get(name));
92 * Get member converted to callable object
94 * @tparam Sig Function signature
95 * @param name Member name
98 typename boost::enable_if< boost::is_function<Sig>,
101 get(const std::string& name)
103 return from_nasal_helper(_context, get(name), static_cast<Sig*>(0));
107 * Get a list of all keys
109 std::vector<std::string> keys() const;
112 * Create a new child hash (module)
114 * @param name Name of the new hash inside this hash
116 Hash createHash(const std::string& name);
119 * Set a new Nasal context. Eg. in FlightGear the context changes every
120 * frame, so if using the same Hash instance for multiple frames you have
121 * to update the context before using the Hash object.
123 void setContext(naContext context);
126 * Get Nasal representation of Hash
128 naRef get_naRef() const;
139 template<class Value>
140 simgear::Map<std::string, Value>
141 from_nasal_helper( naContext c,
143 const simgear::Map<std::string, Value>* )
146 nasal::Hash hash = from_nasal_helper(c, ref, static_cast<nasal::Hash*>(0));
147 std::vector<std::string> const& keys = hash.keys();
149 simgear::Map<std::string, Value> map;
150 for(size_t i = 0; i < keys.size(); ++i)
151 map[ keys[i] ] = hash.get<Value>(keys[i]);
156 #endif /* SG_NASAL_HASH_HXX_ */