1

我正在尝试将自定义 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));
}
4

1 回答 1

1

经过一番搜索,我发现了如何做到这一点!

我已将 ChaiScript.cpp 更改为以下内容:

#include "ChaiScript.h"
#include "SampleClass.h"

ChaiScript::ChaiScript(
   const std::string& script_name)
{
   chai.add(chaiscript::vector_conversion<std::vector<int>>());

   chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());

   chaiscript::utility::add_class<SampleClass>(*m,
      "SampleClass",
      { 
         chaiscript::constructor<SampleClass(const int id, const int x, const int y, const std::string & name)>() 
      },
      { 
         {chaiscript::fun(&SampleClass::m_x), "X"} 
      });

   chai.add(m);

   chai.eval_file(script_name);
}

ChaiScript::~ChaiScript()
{

}

我的 SampleScript.chai 也更改为:

def ReceiveSampleClass(SampleClass sample)
{
    var result = sample.X;
    sample.X += 2;
    var result2 = sample.X;
    print("1) " + to_string(result));
    print("2) " + to_string(result2));
}

我缺少的过程是将类添加到模块并将其添加到 chai 脚本。

   chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());

   chaiscript::utility::add_class<SampleClass>(*m,
      "SampleClass",
      { 
         chaiscript::constructor<SampleClass(const int id, const int x, const int y, const std::string & name)>() 
      },
      { 
         {chaiscript::fun(&SampleClass::m_x), "X"} 
      });

   chai.add(m);
于 2019-10-09T19:02:35.887 回答