Commit f4e8f6c6 authored by Kiste's avatar Kiste
Browse files

Remove unneeded stuff

parent 0ec541f2
*.a
*.ninja
*.o
*.so*
*.so
calc.cpp
bulletml.d
xmltest
meson-*
build.ninja
compile_commands.json
.ninja_*
CXX=g++
DMD=dmd
PERL=perl
CPPFLAGS=-g
DFLAGS=-g
all: cpp_test
cpp_test: cpp.o cpp_test.o
$(DMD) cpp_test $(CPPFLAGS) cpp.o
cpp.o: d_cpp_interface.h cpp.cpp
$(CXX) $(CPPFLAGS) -c cpp.cpp
cpp_test.o: cpp_inter.d cpp_test.d
$(DMD) $(DFLAGS) -c cpp_test.d
cpp_inter.d: cpp.cpp
$(PERL) create_d_import.pl cpp.cpp cpp_inter.d
clean:
rm -f *.o cpp_test cpp_inter.d
CXX=sc
DMD=dmd
PERL=perl
CPPFLAGS=-g
DFLAGS=-g
all: cpp_test
cpp_test: cpp.obj cpp_test.obj
$(DMD) cpp_test $(CPPFLAGS) cpp.obj
cpp.obj: d_cpp_interface.h cpp.cpp
$(CXX) $(CPPFLAGS) -c cpp.cpp
cpp_test.obj: cpp_inter.d cpp_test.d
$(DMD) $(DFLAGS) -c cpp_test.d
cpp_inter.d: cpp.cpp
$(PERL) create_d_import.pl cpp.cpp cpp_inter.d
clean:
rm -f *.obj cpp_test cpp_inter.d
from D to C++
If you are more familiar English than Japanese, please read README.en.
・説明。
C++ の遺産(死んでない) を D から触るためのものです。
extern(C++) がサポートされた時、無意味に帰ります。
C++ からは C のインターフェイスを提供して、
D からそれを触るためのインポートライブラリも作らなければならない、
ということを理解して頂ければ何がやりたいかがわかるかと思います。
C 関数の宣言/定義は C++ ソースからプリプロセッサで、
D 関数の宣言は C++ ソースから perl スクリプトで生成されます。
README.en で説明を放棄するために作った図。
generating process
C++ using d_cpp -----------------------> C wrapper
(interface definition) | preprocesser
|
--------------------> D import interface
perl script
accessing process
D client code --> D import interface --> C wrapper -->
C++ using d_cpp --> C++ library code
・ファイルの説明
README:
これ。
Makefile:
サンプルであるところの cpp_test 実行ファイルを作る。
Makefile.win32:
windows での Makefile。
d_cpp_interface.h:
C++ から C の宣言を生成するためのヘッダ。
create_d_import.pl:
D の宣言を自動生成する perl スクリプト。
cpp.cc:
サンプル、ユーザが書かにゃならんのはこの部分です。
cpp_test.d:
D から cpp.cc を実際にいじってみるサンプル。
create_d_cpp_interface.pl:
おまけ。d_cpp_interface.h を生成する。改造したい場合にどうぞ。
cpp.cc の前半が D から使いたいクラスライブラリ、
後半部が D で import するのに必要な作業です。
cpp.cc に C 関数の定義がマクロで生成されます。
このマクロは雑な perl script でパースするため、
途中で絶対改行しないで下さい。
D による import は create_d_import.pl を用いて
import するためのファイルを作成して下さい。
このファイルには宣言しか含まれないので、
コンパイル・リンクする必要はありません。
こんなややこしいものの説明を書く自信が無いので、
cpp.cc を眺めて適当に理解して下さい。
・その他
とてもとてもα版です。ご意見頂けるととても嬉しいです。
以下が実際的な実例となっています。
http://user.ecc.u-tokyo.ac.jp/~s31552/wp/bulletss/
修正 BSD ライセンスに従って配布します。
Copyright (c) 2003, shinichiro.h All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
------------------
shinichiro.h
s31552@mail.ecc.u-tokyo.ac.jp
http://user.ecc.u-tokyo.ac.jp/~s31552/wp/
from D to C++
I'm sorry of my poor English...
* Introduction
This package helps to access C++ library from D. If D compiler
supports such as extern(C++), this package becomes useless.
* Figure
generating process
C++ using d_cpp -----------------------> C wrapper
(interface definition) | preprocesser
|
--------------------> D import interface
perl script
accessing process
D client code --> D import interface --> C wrapper -->
C++ using d_cpp --> C++ library code
* File Description
README:
this file.
Makefile:
linux's makefile.
Makefile.win32:
windows's makefile.
d_cpp_interface.h:
helps C++ using d_cpp to C wrapper
create_d_import.pl:
helps C++ using d_cpp to D import interface
cpp.cc:
sample of C++ using d_cpp and C++ library code.
cpp_test.d:
sample of D client code.
create_d_cpp_interface.pl:
I created d_cpp_interface.h with this script.
* Misc
This package is alpha release. I'm glad if you give me advice.
More complicated sample is available as "barrage 360 deg.",
a BulletML viewer.
- Japanese page
http://user.ecc.u-tokyo.ac.jp/~s31552/wp/bulletss/
- Download
http://user.ecc.u-tokyo.ac.jp/~s31552/wp/bulletss/bulletss.tar.bz2
- BulletML
http://www.asahi-net.or.jp/~cs8k-cyu/bulletml/index_e.html
This package distributes under the modified BSD license
Copyright (c) 2003, shinichiro.h All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
TTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
------------------
shinichiro.h
s31552@mail.ecc.u-tokyo.ac.jp
http://user.ecc.u-tokyo.ac.jp/~s31552/wp/
class C {
public:
C() {}
C(int a) : a_(a) {}
int a() const { return a_; }
void setA(int a) { a_ = a; }
void f2(int, int) {}
void f3(int, int, int) {}
void f4(int, int, int, int) {}
void f5(int, int, int, int, int) {}
void f6(int, int, int, int, int, int) {}
void f7(int, int, int, int, int, int, int) {}
void f8(int, int, int, int, int, int, int, int) {}
void f9(int, int, int, int, int, int, int, int, int) {}
void over_load(int a) { a_ = a; }
void over_load(char a) { a_ = a-48; }
static int sf0() { return 6; }
static int sf1(int) { return 7; }
private:
int a_;
};
template <class T_> class TC {
public:
T_ f() { return 5; }
template <class T2_>
T2_ tf() { return 6; }
};
class I {
public:
int load_callback() {
return callback();
}
int load_callback2(int x, int y) {
return callback2(x, y);
}
virtual int callback() =0;
virtual int callback2(int, int) =0;
};
#include "d_cpp_interface.h"
extern "C" {
D_CPP_CLASS(C, D_C)
D_CPP_NEW_0(C, D_C_new)
D_CPP_NEW_1(C, D_C_new_1, int)
D_CPP_DELETE(C, D_C_delete)
D_CPP_METHOD_0(C, a, D_C_a, int)
D_CPP_METHOD_1(C, setA, D_C_setA, void, int)
D_CPP_METHOD_2(C, f2, D_C_f2, void, int, int)
D_CPP_METHOD_3(C, f3, D_C_f3, void, int, int, int)
D_CPP_METHOD_4(C, f4, D_C_f4, void, int, int, int, int)
D_CPP_METHOD_5(C, f5, D_C_f5, void, int, int, int, int, int)
D_CPP_METHOD_6(C, f6, D_C_f6, void, int, int, int, int, int, int)
D_CPP_METHOD_7(C, f7, D_C_f7, void, int, int, int, int, int, int, int)
D_CPP_METHOD_8(C, f8, D_C_f8, void, int, int, int, int, int, int, int, int)
D_CPP_METHOD_9(C, f9, D_C_f9, void, int, int, int, int, int, int, int, int, int)
D_CPP_METHOD_1(C, over_load, D_C_over_load_int, void, int)
D_CPP_METHOD_1(C, over_load, D_C_over_load_char, void, char)
D_CPP_CLASS(TC<int>, D_TC_int)
D_CPP_NEW_0(TC<int>, D_TC_int_new)
D_CPP_METHOD_0(TC<int>, f, D_TC_int_f, int)
D_CPP_METHOD_0(TC<int>, tf<int>, D_TC_int_tf, int)
D_CPP_DELETE(TC<int>, D_TC_int_delete)
D_CPP_STATIC_METHOD_0(C, sf0, D_C_sf0, int)
D_CPP_STATIC_METHOD_1(C, sf1, D_C_sf1, int, int)
}
// inherit
D_CPP_BASE_CLASS_OPEN(I, D_I)
D_CPP_VIRTUAL_METHOD_0(D_I, callback, int)
D_CPP_VIRTUAL_METHOD_2(D_I, callback2, int, int, int)
D_CPP_BASE_CLASS_CLOSE()
extern "C" {
D_CPP_CLASS(D_I, D_I)
D_CPP_NEW_0(D_I, D_I_new)
D_CPP_METHOD_0(D_I, load_callback, D_I_load_callback, int)
D_CPP_METHOD_2(D_I, load_callback2, D_I_load_callback2, int, int, int)
D_CPP_VIRTUAL_METHOD_SETTER_0(D_I, callback, D_I_setCallbackFunc, int)
D_CPP_VIRTUAL_METHOD_SETTER_2(D_I, callback2, D_I_setCallback2Func, int, int, int)
D_CPP_DELETE(D_I, D_I_delete)
}
import cpp_inter;
import stream;
import string;
extern (C) {
int return7(D_I* i) { return 7; }
int add(D_I* i, int x, int y) { return x+y; }
}
int main() {
D_C* c = D_C_new_1(1);
stdout.writeLine(string.toString(D_C_a(c)));
D_C_delete(c);
c = D_C_new();
D_C_setA(c, 2);
stdout.writeLine(string.toString(D_C_a(c)));
D_C_f2(c, 0, 0);
D_C_f5(c, 0,0,0,0,0);
D_C_f9(c, 0,0,0,0,0,0,0,0,0);
D_C_over_load_int(c, 3);
stdout.writeLine(string.toString(D_C_a(c)));
D_C_over_load_char(c, '4');
stdout.writeLine(string.toString(D_C_a(c)));
D_TC_int* tc = D_TC_int_new();
stdout.writeLine(string.toString(D_TC_int_f(tc)));
stdout.writeLine(string.toString(D_TC_int_tf(tc)));
D_C_delete(c);
D_TC_int_delete(tc);
D_I* i = D_I_new();
D_I_setCallbackFunc(i, &return7);
D_I_setCallback2Func(i, &add);
stdout.writeLine(string.toString(D_I_load_callback(i)));
stdout.writeLine(string.toString(D_I_load_callback2(i, 3, 5)));
D_I_delete(i);
return 0;
}
use strict;
print "#define D_CPP_CLASS(CLASS, CLASS_NAME) ;\n";
print "\n";
for (my $i = 0; $i < 10; $i++) {
print "#define D_CPP_NEW_$i(CLASS, NEW_NAME";
my @args;
my @args2;
for (my $j = 1; $j <= $i; $j++) {
print ", ARG$j";
push @args, "ARG$j arg$j";
push @args2, "arg$j";
}
print ") \\\n";
my $argStr = join ', ', @args;
my $argStr2 = join ', ', @args2;
print "CLASS* NEW_NAME($argStr) { \\\n";
print " return new CLASS($argStr2); \\\n";
print "}\n";
print "\n";
}
print "#define D_CPP_DELETE(CLASS, DELETE_NAME) \\\n";
print "void DELETE_NAME(CLASS* c) { delete c; } \n";
print "\n";
for (my $i = 0; $i < 10; $i++) {
print "#define D_CPP_METHOD_$i(CLASS, METHOD, METHOD_NAME, RETURN";
my $argStr;
my @args2;
for (my $j = 1; $j <= $i; $j++) {
print ", ARG$j";
$argStr .= ", ARG$j arg$j";
push @args2, "arg$j";
}
print ") \\\n";
my $argStr2 = join ', ', @args2;
print "RETURN METHOD_NAME(CLASS* c$argStr) { \\\n";
print " return c->METHOD($argStr2); \\\n";
print "}\n";
print "\n";
}
for (my $i = 0; $i < 10; $i++) {
print "#define D_CPP_STATIC_METHOD_$i(CLASS, METHOD, METHOD_NAME, RETURN";
my @args;
my @args2;
for (my $j = 1; $j <= $i; $j++) {
print ", ARG$j";
push @args, "ARG$j arg$j";
push @args2, "arg$j";
}
print ") \\\n";
my $argStr = join ', ', @args;;
my $argStr2 = join ', ', @args2;
print "RETURN METHOD_NAME($argStr) { \\\n";
print " return CLASS::METHOD($argStr2); \\\n";
print "}\n";
print "\n";
}
print "#define D_CPP_BASE_CLASS_OPEN(BASE, BASE_NAME) \\\n";
print "struct BASE_NAME : public BASE { \n";
for (my $i = 0; $i < 10; $i++) {
print "#define D_CPP_VIRTUAL_METHOD_$i(CLASS, METHOD, RETURN";
my @args;
my @args2;
my @args3;
for (my $j = 1; $j <= $i; $j++) {
print ", ARG$j";
push @args, "ARG$j arg$j";
push @args2, ", arg$j";
push @args3, ", ARG$j";
}
print ") \\\n";
my $argStr = join ', ', @args;;
my $argStr2 = join '', @args2;
my $argStr3 = join '', @args3;
print " virtual RETURN METHOD($argStr) { \\\n";
print " return D_##METHOD##_fp(this $argStr2); \\\n";
print " } \\\n";
print " void D_set_##METHOD(RETURN (*fp) (CLASS* $argStr3)) { \\\n";
print " D_##METHOD##_fp = fp; \\\n";
print " } \\\n";
print " RETURN (*D_##METHOD##_fp) (CLASS*$argStr3); \n";
print "\n";
}
for (my $i = 0; $i < 10; $i++) {
print "#define D_CPP_VIRTUAL_METHOD_SETTER_$i(CLASS, METHOD, SETTER_NAME, RETURN";
my @args;
my @args2;
my @args3;
for (my $j = 1; $j <= $i; $j++) {
print ", ARG$j";
push @args, "ARG$j arg$j";
push @args2, ", arg$j";
push @args3, ", ARG$j";
}
print ") \\\n";
my $argStr = join ', ', @args;;
my $argStr2 = join '', @args2;
my $argStr3 = join '', @args3;
print "void SETTER_NAME(CLASS* c, RETURN (*fp) (CLASS*$argStr3)) { \\\n";
print " c->D_set_##METHOD(fp); \\\n";
print "}\n";
print "\n";
}
print "#define D_CPP_BASE_CLASS_CLOSE() \\\n";
print "};\n";
print "\n";
print "#define D_CPP_D_DECLARE(arg) ; \n";
print "\n";
#!/usr/bin/env perl
use strict;
my @args;
my $in = *STDIN;
if ($ARGV[0]) {
open $in, $ARGV[0];
}
my $out = *STDOUT;
if ($ARGV[1]) {
open $out, "> $ARGV[1]";
}
print $out "extern (C) {\n";
while (<$in>) {
s/const //g;
if (parseArgs("D_CPP_CLASS")) {
my $class = shift @args;
my $name = shift @args;
print $out "alias int $name;\n";
}
if (parseArgs("D_CPP_NEW_\\d")) {
my $class = shift @args;
my $name = shift @args;
my $args = join ', ', @args;
print $out "int* $name($args);\n";
}
if (parseArgs("D_CPP_DELETE")) {
my $class = shift @args;
my $name = shift @args;
print $out "void $name(int*);\n";
}
if (parseArgs("D_CPP_METHOD_\\d")) {
my $class = shift @args;
my $method = shift @args;
my $name = shift @args;
my $ret = shift @args;
my $args = join ', ', @args;
if ($args) {
$args = ", $args";
}
print $out "$ret $name(int* $args);\n";
}
if (parseArgs("D_CPP_STATIC_METHOD_\\d")) {
my $class = shift @args;
my $method = shift @args;
my $name = shift @args;
my $ret = shift @args;
my $args = join ', ', @args;
print $out "$ret $name($args);\n";
}
if (parseArgs("D_CPP_VIRTUAL_METHOD_SETTER_\\d")) {
my $class = shift @args;
my $method = shift @args;
my $name = shift @args;
my $ret = shift @args;
my $args = join ', ', @args;
if ($args) {
$args = ", $args";
}
print $out "void $name(int*, $ret (*fp) (int* $args)); \n";
}
if (parseArgs("D_CPP_D_DECLARE")) {
my $declare = shift @args;
$declare =~ s/^\"//;
$declare =~ s/\"$//;
print $out "$declare\n";
}
}
print $out "}\n";
sub parseArgs {
my $name = shift;
if (/$name\s*\(([^\)]*)\)/) {
@args = split /\s*,\s*/, $1;
return 1;
}
else {
return 0;
}