これは学校の事です。
問題は次のとおりです。
私たちはボンバーマンのクローンに取り組んでおり、スクリプト インターフェイスを実装して、ユーザーが独自の「人工知能」を作成できるようにする必要があります。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'],
);
わかりやすくするために、typemaps ファイルを除外しました。
モジュールは正しく構築されており、Perl で SaibApi クラスをインスタンス化できます。ここでの問題は、明らかにクラスを c++ と perl で同時にインスタンス化する方法がないことです。私たちがやろうとしているのは、単純なメソッドを介して C++ コード (マップ、プレーヤーなどを含む多数の std::list など) からオブジェクトを取得できるようにする単純な API をユーザーに提供することです。
例えば:
#! /usr/bin/perl -w
#use SaibApi;
my $ai = new SaibApi();
my @map = ai->GetMap();
# some more code here...
私たちは多くの調査を行いましたが、XS に関するドキュメントは少し不足しています。私たちは今立ち往生しています。c++ と perl で同時にクラスをインスタンス化して、メソッド_lol
を使用せずに c++ の部分で設定し、perl の部分で出力できるようにするにはどうすればよいでしょうか? SetLol()
不可能な場合、代替手段は何ですか?