Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JavaScript中的关联数组映射到C+中的字符串映射+;使用Swig?_Javascript_C++_Swig_Stdmap - Fatal编程技术网

如何将JavaScript中的关联数组映射到C+中的字符串映射+;使用Swig?

如何将JavaScript中的关联数组映射到C+中的字符串映射+;使用Swig?,javascript,c++,swig,stdmap,Javascript,C++,Swig,Stdmap,非常类似于我想用SWIG包装一个函数,它将strings的map映射到strings: void foo(std::map<std::string, std::string> const& args); 将正确调用,不符合签名的值将被忽略 如何为JavaScript实现这一点 我尝试了相同的方法(当然)并生成了包装器get,但当我尝试调用foo时,如下所示: my_module.foo({'a':'b', 'c':'d'}); 我明白了 api.i #pragma onc

非常类似于我想用SWIG包装一个函数,它将
string
s的
map
映射到
string
s:

void foo(std::map<std::string, std::string> const& args);
将正确调用,不符合签名的值将被忽略

如何为JavaScript实现这一点

我尝试了相同的方法(当然)并生成了包装器get,但当我尝试调用
foo
时,如下所示:

my_module.foo({'a':'b', 'c':'d'});
我明白了

api.i

#pragma once

#include <string>
#include <map>
#include <iostream>

static void foo(std::string const& value) noexcept {
  std::cout << value << std::endl;
}

static void bar(std::map<std::string, std::string> const& args) noexcept {
    for (auto && e : args) {
        std::cout << e.first << ": " << e.second << std::endl;
    }
}
%module api 
%include "std_string.i"
%include "std_map.i"
namespace std {
    %template(map_string_string) map<string, string>;
}
%{
    #include <api.h>
%}
%include "api.h"
最后,这是我测试它们的方式:

node -e "api = require('api.node'); api.foo('some string'); api.bar({'a':'b'});"
python3 -c "import api; api.foo('hello'); api.bar({'a':'b','c':'d'})"

在这两种情况下,-Python和JavaScript-
api.foo()
都按预期执行
api.bar()
可以在Python上执行,但在JavaScript中会抛出我发布的错误。

我觉得当前版本的
swig
(3.0.12)没有内置支持将JavaScript对象或原语映射到
映射。我认为你必须编写自己的转换器,它需要一个JavaScript对象并将其转换成C++ <代码> map < /C>。有关这方面的帮助,请参阅。

请提供一个完整的示例,正如您在回答中提到的内森·宾克特(Nathan Binkert)所做的那样。如果没有一个有效的例子,读者不得不浪费大量的时间来重现这个问题。你当然是对的,我已经添加了我目前正在使用的代码片段
#pragma once

#include <string>
#include <map>
#include <iostream>

static void foo(std::string const& value) noexcept {
  std::cout << value << std::endl;
}

static void bar(std::map<std::string, std::string> const& args) noexcept {
    for (auto && e : args) {
        std::cout << e.first << ": " << e.second << std::endl;
    }
}
%module api 
%include "std_string.i"
%include "std_map.i"
namespace std {
    %template(map_string_string) map<string, string>;
}
%{
    #include <api.h>
%}
%include "api.h"
swig -c++ -python -o api_wrap_python.cxx api.i 
g++ -c api_wrap_python.cxx \
    -I/usr/include/python3.6m -I . \
    -fPIC -std=gnu++11
g++ -shared api_wrap_python.o -o _api.so

swig -c++ -javascript -node -o api_wrap_js.cxx api.i
g++ -c api_wrap_js.cxx \
    -I /usr/include/node -I . \
    -std=gnu++11 -fPIC -DBUILDING_NODE_EXTENSION
g++ -shared api_wrap_js.o -o api.node
node -e "api = require('api.node'); api.foo('some string'); api.bar({'a':'b'});"
python3 -c "import api; api.foo('hello'); api.bar({'a':'b','c':'d'})"