]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalHash.cxx
Now the real fix for old compilers...
[simgear.git] / simgear / nasal / cppbind / NasalHash.cxx
1 // 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 #include "NasalHash.hxx"
20 #include "to_nasal.hxx"
21
22 #include <cassert>
23
24 namespace nasal
25 {
26
27   //----------------------------------------------------------------------------
28   Hash::Hash(naContext c):
29     _hash(naNewHash(c)),
30     _context(c),
31     _keys(naNil())
32   {
33
34   }
35
36   //----------------------------------------------------------------------------
37   Hash::Hash(naRef hash, naContext c):
38     _hash(hash),
39     _context(c),
40     _keys(naNil())
41   {
42     assert( naIsHash(_hash) );
43   }
44
45   //----------------------------------------------------------------------------
46   Hash::iterator Hash::begin()
47   {
48     return iterator(this, 0);
49   }
50
51   //----------------------------------------------------------------------------
52   Hash::iterator Hash::end()
53   {
54     return iterator(this, size());
55   }
56
57   //----------------------------------------------------------------------------
58   Hash::const_iterator Hash::begin() const
59   {
60     return const_iterator(this, 0);
61   }
62
63   //----------------------------------------------------------------------------
64   Hash::const_iterator Hash::end() const
65   {
66     return const_iterator(this, size());
67   }
68
69   //----------------------------------------------------------------------------
70   void Hash::set(const std::string& name, naRef ref)
71   {
72     naHash_set(_hash, to_nasal(_context, name), ref);
73     _keys = naNil();
74   }
75
76   //----------------------------------------------------------------------------
77   naRef Hash::get(naRef key) const
78   {
79     naRef result;
80     return naHash_get(_hash, key, &result) ? result : naNil();
81   }
82
83   //----------------------------------------------------------------------------
84   naRef Hash::get(const std::string& name) const
85   {
86     return get( to_nasal(_context, name) );
87   }
88
89   //----------------------------------------------------------------------------
90   int Hash::size() const
91   {
92     return naVec_size(get_naRefKeys());
93   }
94
95   //----------------------------------------------------------------------------
96   std::vector<std::string> Hash::keys() const
97   {
98     return from_nasal<std::vector<std::string> >(_context, get_naRefKeys());
99   }
100
101   //----------------------------------------------------------------------------
102   Hash Hash::createHash(const std::string& name)
103   {
104     Hash hash(_context);
105     set(name, hash);
106     return hash;
107   }
108
109   //----------------------------------------------------------------------------
110   void Hash::setContext(naContext context)
111   {
112     _context = context;
113   }
114
115   //----------------------------------------------------------------------------
116   naRef Hash::get_naRef() const
117   {
118     return _hash;
119   }
120
121   //----------------------------------------------------------------------------
122   naRef Hash::get_naRefKeys() const
123   {
124     if( naIsNil(_keys) && naIsHash(_hash) )
125     {
126       _keys = naNewVector(_context);
127       naHash_keys(_keys, _hash);
128     }
129
130     return _keys;
131   }
132
133 } // namespace nasal