我正在尝试将自定义 c++ 对象引用传递给简单的 chai 脚本,以便 chai 脚本最终可以读取/访问/调用公共变量和方法。
我不确定这是否可能,也不确定一旦它在 chai 脚本函数内部传递,它是否可以访问类值而无需在 chai 中定义先前的类......任何见解都会非常有帮助,因为我在与此相关的文档中找不到特定部分。
这是一个非常基本的实现。
ChaiScript.h
#pragma once
#include "ChaiScript\include\chaiscript\chaiscript.hpp"
class ChaiScript
{
public:
explicit ChaiScript(
const std::string& script_name);
~ChaiScript();
chaiscript::ChaiScript chai;
private:
};
ChaiScript.cpp
#include "ChaiScript.h"
ChaiScript::ChaiScript(
const std::string& script_name)
{
chai.add(chaiscript::vector_conversion<std::vector<int>>());
chai.eval_file(script_name);
}
ChaiScript::~ChaiScript()
{
}
样本类.h
#pragma once
#include <string>
class SampleClass
{
public:
SampleClass(
const int id,
const int x,
const int y,
const std::string& name);
const int m_id;
int m_x;
int m_y;
std::string m_name;
private:
// Disable copying as we don't want to make a copy each time we pass the class to chai...
SampleClass();
SampleClass(const SampleClass& rhs);
SampleClass& operator=(const SampleClass& rhs);
};
样本类.cpp
#include "SampleClass.h"
SampleClass::SampleClass(
const int id,
const int x,
const int y,
const std::string& name) :
m_id(id),
m_x(x),
m_y(y),
m_name(name)
{
}
主文件
#include "ChaiScript.h"
#include "SampleClass.h"
int main()
{
SampleClass* sample_class = new SampleClass(0, 1, 2, "Test");
ChaiScript* script = new ChaiScript("Scripts\\SampleScript.chai");
// This line is where I start to need help as it does not compile.
script->chai.eval<std::function<void(&SampleClass)>>("ReceiveSampleClass")(&sample_class);
system("pause");
return 0;
}
SampleScript.chai
def ReceiveSampleClass(SampleClass sample)
{
var result = sample.m_x + sample.m_y;
print("2+2=" + to_string(result));
}