cppbind: convert maps from nasal.

This commit is contained in:
Thomas Geymayer
2014-05-18 13:33:10 +02:00
parent 289777bd99
commit 6929ba75c0
3 changed files with 37 additions and 0 deletions

View File

@@ -54,6 +54,14 @@ namespace nasal
: naNil();
}
//----------------------------------------------------------------------------
std::vector<std::string> Hash::keys() const
{
naRef keys = naNewVector(_context);
naHash_keys(keys, _hash);
return from_nasal<std::vector<std::string> >(_context, keys);
}
//----------------------------------------------------------------------------
Hash Hash::createHash(const std::string& name)
{

View File

@@ -22,6 +22,8 @@
#include "from_nasal.hxx"
#include "to_nasal.hxx"
#include <simgear/structure/map.hxx>
namespace nasal
{
@@ -101,6 +103,11 @@ namespace nasal
return from_nasal_helper(_context, get(name), static_cast<Sig*>(0));
}
/**
* Get a list of all keys
*/
std::vector<std::string> keys() const;
/**
* Create a new child hash (module)
*
@@ -129,4 +136,21 @@ namespace nasal
} // namespace nasal
template<class Value>
simgear::Map<std::string, Value>
from_nasal_helper( naContext c,
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();
simgear::Map<std::string, Value> map;
for(size_t i = 0; i < keys.size(); ++i)
map[ keys[i] ] = hash.get<Value>(keys[i]);
return map;
}
#endif /* SG_NASAL_HASH_HXX_ */

View File

@@ -168,6 +168,11 @@ int main(int argc, char* argv[])
r = to_nasal(c, hash);
VERIFY( naIsHash(r) );
simgear::StringMap string_map = from_nasal<simgear::StringMap>(c, r);
VERIFY( string_map.at("vec") == "string" )
VERIFY( string_map.at("name") == "my-name" )
VERIFY( string_map.at("string") == "blub" )
VERIFY( hash.get<std::string>("name") == "my-name" );
VERIFY( naIsString(hash.get("name")) );