On Tue, 2007-07-17 at 11:13 +1000, skaller wrote:
> On Mon, 2007-07-16 at 16:40 -0700, Raoul Duke wrote:

> First make a binding like:

Now I'll give some explanation, because this uses
quite a lot of features :)

> ///////////// foobind.flx ///////////////////////
> #import <flx.flxh>
> header foo_h = '#include "Foo.h"';

This tags a header inclusion. The quoted text will be
emitted into a generated *.hpp file if the tag foo_h is
activated.

> type foo = "Foo*" requires foo_h, package "foo";

This makes Felix type foo a pointer to a Foo object.
So 'foo' is a first class value itself, but it is
passing around a pointer to the underlying class.

The requires clause says "if the type 'foo' is used
anywhere in the program, then:

requires foo_h causes Felix to  emit foo_h text into *.hpp

requires package "foo" causes Felix to emit "foo" into
*.resh, the resource file for the program.

The resh file lists all the external resources needed by
a program. It is just a list of resource names. These
resources are located by flx_pkgconfig by, for example:

        config/foo.fpc

which provides the platform specific resource details
See below for the foo.fpc file.

The idea here is that Felix can find all the libraries
you need to run a program without any command line
switches. The description of the resource is split
into an abstract, platform independent resource,
the word 'foo', and a platform specific description,
the file config/foo.fpc.

> gen _ctor_foo: int -> foo = "new Foo($1)";

Two tricks here. First, Felix functions aren't allowed
to have side-effects .. but creating an object is a side-effect.
So instead of 'fun' we use 'gen' which is short for generator,
meaning the function has state and may have side-effects.

Generators are treated like functions except they're replaced
in expressions, for example:

        f(g x, y) // where g is a generator

is replaced by

        var tmp = g x;
        f(tmp, y);

to ensure optimisations etc execute the generators serially
and exactly once.

The second trick here is the magic name _ctor_foo. A function
with such a name is invoked by 'foo' not _ctor_foo, where
foo is a typename .. _ctor_ is just shorthand for 'constructor'.

So this is how you write a constructor wrapper: you name it
_ctor_type where 'type' is the name of the type.

> proc del: foo = "delete $1;";

So we can delete a foo. $1 is the first and only
argument.

> fun getID: foo -> int = $1.getId()";

a wrapper for the getID() method,
$1 is the first and only argument.

> /////// foobar.flx /////////////////
> #import <flx.flxh>
> include "foobind";

This 'includes' the binding code we made above.
Doing it this way uses a cached parse tree for foobind,
which is named 'foobind.par'. If there's no such file,
Felix creates it after parsing foobind.flx recursively.


> proc flx_main()

A little known fact about Felix .. flx_main() is the name
of the mainline of the program. Usually we don't bother
we just use initialisation side-effects to write programs.

> and put this config for Unix:
> 
> //// config/foo.fpc /////////////////
> provides_dlib: -lfoo_dynamic
> provides_slib: -lfoo_static
> //////////////////////////////////////


This is the resource descriptor file.
It says that to use the shared library for 'foo',
use the linker switch -lfoo_dynamic.

To do static linkage, use the linker switch -lfoo_static.

YMMV, these switches are platform dependent: depend on
your OS, where you put the libraries, and the compiler
used to link them.


> Now you need to remove (or rename) the 'main'
> function in Foo.cpp, then compile something like
> (RTFM here .. ):
> 
> g++ -fPIC -shared -o rtl/foo_dynamic.so foo.cpp
> g++ -o rtl/foo_static.o foo.cpp
> ranlib -o rtl/foo_static.a rtl/foo_static.o

Ahem .. WOOPS .. on Unix the -o should be

        -o rtl/libfoo_dynamic.so

since UNix is utterly stupid and puts 'lib' in front
of library names when searching.


-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to