1 // 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 #include "NasalHash.hxx"
20 #include "to_nasal.hxx"
27 //----------------------------------------------------------------------------
28 Hash::Hash(naContext c):
36 //----------------------------------------------------------------------------
37 Hash::Hash(naRef hash, naContext c):
42 assert( naIsHash(_hash) );
45 //----------------------------------------------------------------------------
46 Hash::iterator Hash::begin()
48 return iterator(this, 0);
51 //----------------------------------------------------------------------------
52 Hash::iterator Hash::end()
54 return iterator(this, size());
57 //----------------------------------------------------------------------------
58 Hash::const_iterator Hash::begin() const
60 return const_iterator(this, 0);
63 //----------------------------------------------------------------------------
64 Hash::const_iterator Hash::end() const
66 return const_iterator(this, size());
69 //----------------------------------------------------------------------------
70 void Hash::set(const std::string& name, naRef ref)
72 naHash_set(_hash, to_nasal(_context, name), ref);
76 //----------------------------------------------------------------------------
77 naRef Hash::get(naRef key) const
80 return naHash_get(_hash, key, &result) ? result : naNil();
83 //----------------------------------------------------------------------------
84 naRef Hash::get(const std::string& name) const
86 return get( to_nasal(_context, name) );
89 //----------------------------------------------------------------------------
90 int Hash::size() const
92 return naVec_size(get_naRefKeys());
95 //----------------------------------------------------------------------------
96 std::vector<std::string> Hash::keys() const
98 return from_nasal<std::vector<std::string> >(_context, get_naRefKeys());
101 //----------------------------------------------------------------------------
102 Hash Hash::createHash(const std::string& name)
109 //----------------------------------------------------------------------------
110 void Hash::setContext(naContext context)
115 //----------------------------------------------------------------------------
116 naRef Hash::get_naRef() const
121 //----------------------------------------------------------------------------
122 naRef Hash::get_naRefKeys() const
124 if( naIsNil(_keys) && naIsHash(_hash) )
126 _keys = naNewVector(_context);
127 naHash_keys(_keys, _hash);