//----------------------------------------------------------------------------
Hash::Hash(naContext c):
- _hash( naNewHash(c) ),
- _context(c)
+ _hash(naNewHash(c)),
+ _context(c),
+ _keys(naNil())
{
}
//----------------------------------------------------------------------------
Hash::Hash(naRef hash, naContext c):
_hash(hash),
- _context(c)
+ _context(c),
+ _keys(naNil())
{
assert( naIsHash(_hash) );
}
+ //----------------------------------------------------------------------------
+ Hash::iterator Hash::begin()
+ {
+ return iterator(this, 0);
+ }
+
+ //----------------------------------------------------------------------------
+ Hash::iterator Hash::end()
+ {
+ return iterator(this, size());
+ }
+
+ //----------------------------------------------------------------------------
+ Hash::const_iterator Hash::begin() const
+ {
+ return const_iterator(this, 0);
+ }
+
+ //----------------------------------------------------------------------------
+ Hash::const_iterator Hash::end() const
+ {
+ return const_iterator(this, size());
+ }
+
//----------------------------------------------------------------------------
void Hash::set(const std::string& name, naRef ref)
{
naHash_set(_hash, to_nasal(_context, name), ref);
+ _keys = naNil();
}
//----------------------------------------------------------------------------
- naRef Hash::get(const std::string& name)
+ naRef Hash::get(naRef key) const
{
naRef result;
- return naHash_get(_hash, to_nasal(_context, name), &result) ? result
- : naNil();
+ return naHash_get(_hash, key, &result) ? result : naNil();
+ }
+
+ //----------------------------------------------------------------------------
+ naRef Hash::get(const std::string& name) const
+ {
+ return get( to_nasal(_context, name) );
+ }
+
+ //----------------------------------------------------------------------------
+ int Hash::size() const
+ {
+ return naVec_size(get_naRefKeys());
}
//----------------------------------------------------------------------------
std::vector<std::string> Hash::keys() const
{
- naRef keys = naNewVector(_context);
- naHash_keys(keys, _hash);
- return from_nasal<std::vector<std::string> >(_context, keys);
+ return from_nasal<std::vector<std::string> >(_context, get_naRefKeys());
}
//----------------------------------------------------------------------------
return _hash;
}
+ //----------------------------------------------------------------------------
+ naRef Hash::get_naRefKeys() const
+ {
+ if( naIsNil(_keys) && naIsHash(_hash) )
+ {
+ _keys = naNewVector(_context);
+ naHash_keys(_keys, _hash);
+ }
+
+ return _keys;
+ }
+
} // namespace nasal
#include "to_nasal.hxx"
#include <simgear/structure/map.hxx>
+#include <boost/iterator/iterator_facade.hpp>
namespace nasal
{
{
public:
+ template<bool> class Entry;
+ template<bool> class Iterator;
+
+ typedef Entry<false> reference;
+ typedef Entry<true> const_reference;
+ typedef Iterator<false> iterator;
+ typedef Iterator<true> const_iterator;
+
/**
* Create a new Nasal Hash
*
* @param c Nasal context for creating the hash
*/
- Hash(naContext c);
+ explicit Hash(naContext c);
/**
* Initialize from an existing Nasal Hash
*/
Hash(naRef hash, naContext c);
+ iterator begin();
+ iterator end();
+ const_iterator begin() const;
+ const_iterator end() const;
+
/**
* Set member
*
set(name, to_nasal(_context, val));
}
+ /**
+ * Get member
+ *
+ * @param key Member key
+ */
+ naRef get(naRef key) const;
+
/**
* Get member
*
* @param name Member name
*/
- naRef get(const std::string& name);
+ naRef get(const std::string& name) const;
/**
* Get member converted to given type
* @tparam T Type to convert to (using from_nasal)
* @param name Member name
*/
- template<class T>
- T get(const std::string& name)
+ template<class T, class Key>
+ T get(const Key& name) const
{
+ BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
+ || boost::is_convertible<Key, std::string>::value
+ ));
+
return from_nasal<T>(_context, get(name));
}
*
* @tparam Sig Function signature
* @param name Member name
+ * @param key Member key
*/
- template<class Sig>
+ template<class Sig, class Key>
typename boost::enable_if< boost::is_function<Sig>,
boost::function<Sig>
>::type
- get(const std::string& name)
+ get(const Key& name) const
{
+ BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
+ || boost::is_convertible<Key, std::string>::value
+ ));
+
return from_nasal_helper(_context, get(name), static_cast<Sig*>(0));
}
+ /**
+ * Returns the number of entries in the hash
+ */
+ int size() const;
+
/**
* Get a list of all keys
*/
*/
naRef get_naRef() const;
+ /**
+ * Get Nasal vector of keys
+ */
+ naRef get_naRefKeys() const;
+
+ /// Hash entry
+ template<bool is_const>
+ class Entry
+ {
+ public:
+ typedef typename boost::mpl::if_c<
+ is_const,
+ Hash const*,
+ Hash*
+ >::type HashPtr;
+
+ Entry(HashPtr hash, naRef key):
+ _hash(hash),
+ _key(key)
+ {
+ assert(hash);
+ assert(!naIsNil(key));
+ }
+
+ std::string getKey() const
+ {
+ if( !_hash || naIsNil(_key) )
+ return std::string();
+
+ return from_nasal<std::string>(_hash->_context, _key);
+ }
+
+ template<class T>
+ T getValue() const
+ {
+ if( !_hash || naIsNil(_key) )
+ return T();
+
+ return _hash->get<T>(_key);
+ }
+
+ private:
+ HashPtr _hash;
+ naRef _key;
+
+ };
+
+ /// Hash iterator
+ template<bool is_const>
+ class Iterator:
+ public boost::iterator_facade<
+ Iterator<is_const>,
+ Entry<is_const>,
+ boost::bidirectional_traversal_tag,
+ Entry<is_const>
+ >
+ {
+ public:
+ typedef typename Entry<is_const>::HashPtr HashPtr;
+ typedef Entry<is_const> value_type;
+
+ Iterator():
+ _hash(NULL),
+ _index(0)
+ {}
+
+ Iterator(HashPtr hash, int index):
+ _hash(hash),
+ _index(index)
+ {}
+
+ /**
+ * Convert from iterator to const_iterator or copy within same type
+ */
+ template<bool is_other_const>
+ Iterator( Iterator<is_other_const> const& other,
+ typename boost::enable_if_c< is_const || !is_other_const,
+ void*
+ >::type = NULL ):
+ _hash(other._hash),
+ _index(other._index)
+ {}
+
+ private:
+ friend class boost::iterator_core_access;
+ template <bool> friend class Iterator;
+
+ HashPtr _hash;
+ int _index;
+
+ template<bool is_other_const>
+ bool equal(Iterator<is_other_const> const& other) const
+ {
+ return _hash == other._hash
+ && _index == other._index;
+ }
+
+ void increment() { ++_index; }
+ void decrement() { --_index; }
+
+ value_type dereference() const
+ {
+ return value_type(_hash, naVec_get(_hash->get_naRefKeys(), _index));
+ }
+ };
+
protected:
naRef _hash;
naContext _context;
+ mutable naRef _keys; //< Store vector of keys (for iterators)
+
};
} // namespace nasal
naRef ref,
const simgear::Map<std::string, Value>* )
{
-
nasal::Hash hash = from_nasal_helper(c, ref, static_cast<nasal::Hash*>(0));
std::vector<std::string> const& keys = hash.keys();