【发布时间】:2014-05-27 13:52:33
【问题描述】:
这是学校的事情。
问题来了:
我们正在开发一个炸弹人克隆,我们必须实现一个脚本界面,允许用户制作自己的“人工智能”。 我们选择使用 Perl。现在,我像这样构建 perl 模块:
这里是文件(测试目的):
SaibApi.xs:
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
#include "ppport.h"
#include "SaibApi.hpp"
MODULE = SaibApi PACKAGE = SaibApi
SaibApi *
SaibApi::new()
void
SaibApi::DESTROY()
void
SaibApi::PrintLol()
void
SaibApi::PrintPvar()
void
SaibApi::setLol(int arg)
SaibApi.hpp
#ifndef SAIBAPI_HPP_
# define SAIBAPI_HPP_
#include <iostream>
class SaibApi {
public:
SaibApi() {}
~SaibApi() {}
void PrintLol() { std::cout << "lol\n"; }
void PrintPvar() { std::cout << _lol << "\n"; }
void setLol(int arg) {_lol = arg;}
private:
int _lol;
};
#endif /* !SAIBAPI_HPP_ */
Makefike.PL:
use 5.014002;
use ExtUtils::MakeMaker;
my $CC = 'g++';
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
NAME => 'SaibApi',
VERSION_FROM => 'lib/SaibApi.pm', # finds $VERSION
PREREQ_PM => {}, # e.g., Module::Name => 1.1
($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => 'lib/SaibApi.pm', # retrieve abstract from module
AUTHOR => 'Arkeopix <arkeopix@>') : ()),
LIBS => [''], # e.g., '-lm'
DEFINE => '', # e.g., '-DHAVE_SOMETHING'
INC => '-I.', # e.g., '-I. -I/usr/include/other'
# Un-comment this if you add C files to link with later:
# OBJECT => '$(O_FILES)', # link all the C files too
CC => $CC,
LD => '$(CC)',
XSOPT => '-C++',
TYPEMAPS => ['perlobject.map'],
);
为了清楚起见,我排除了类型映射文件。
模块已正确构建,我可以在 perl 中实例化 SaibApi 类。 现在我们的问题是,显然没有办法同时在 c++ 和 perl 中实例化该类。我们正在尝试做的是给用户一个简单的 API,允许通过简单的方法从我们的 C++ 代码(包含地图、玩家等的大量 std::list)中获取对象。
例如:
#! /usr/bin/perl -w
#use SaibApi;
my $ai = new SaibApi();
my @map = ai->GetMap();
# some more code here...
我们进行了大量研究,但有关 XS 的文档有点稀缺。我们现在被困住了。如何同时在 c++ 和 perl 中实例化该类,允许在 c++ 部分中设置 _lol 并在 perl 部分中打印它而无需使用 SetLol() 方法?如果不可能,有什么替代方案?
【问题讨论】:
-
我自己会选择 Lua。它是为这类事情设计的