minid.bind
This module contains scary template stuff to make it possible to wrap D functions,
classes, and structs and expose them as functions and types in MiniD.
BUGS:
When wrapping structs, or when using functions which take structs as arguments, if a
struct type has any private fields, it will fail. Unfortunately D doesn't seem to
provide any way to step around the private members, and also trying to get the .init
of most structs causes a forward declaration error, so it's a bug that forces another
bug.
Arrays of wrapped structs/classes are not yet supported.
License:
Copyright (c) 2007 Jarrett Billingsley
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
- struct
WrapModule
;
- Used to wrap a module that is exposed to MiniD. A module can contain any number of functions, classes, structs,
and other values. They are added to the module using this struct's methods.
Examples:
WrapModule("foo.bar", myContext) // name it foo.bar, load it into myContext
.func!(funcOne)() // wrap native function funcOne; the empty parens here are necessary
.func!(funcTwo)()
.custom("x", 6)() // set member "x" to the value 6. This can be any convertible type.
.type(WrapClass!(MyClass) // wrap the native class MyClass.
.method!(MyClass.method)() // A method. Again, the empty parens are necessary.
.property!(MyClass.property)()); // a property
- static WrapModule
opCall
(char[] name, MDContext context);
- Creates an instance of this struct so you can start wrapping a module.
Params:
char[] name |
The name of the module to expose to MiniD. This can be a multi-part name, with dots (like "foo.bar"). |
MDContext context |
The MiniD context to load this module into. |
Returns:
An instance of this struct ready to have members added.
- typeof(this)
func
(alias f,char[] name = NameOfFunc!(f),funcType = typeof(f))();
typeof(this)
func
');
(alias f,funcType)();
- Wrap a function and insert it into this module's namespace. This must be a non-member D function.
Params:
f |
An alias to the function you want to wrap. |
name |
The name to call it in MiniD. Defaults to the D function name. |
funcType |
The type of the function to wrap. This defaults to typeof(f), but you'll need to specify this
explicitly if you're wrapping an overloaded function, in order to select the proper overload. |
Returns:
A chaining reference to this module.
- typeof(this)
custom
(T)(char[] name, T value);
- Insert an arbitrary key-value pair into the module. The value can be any convertible type.
Params:
name |
The name to give this value in the module. |
value |
The value to insert. |
Returns:
A chaining reference to this module.
- typeof(this)
type
(T)(T value);
- Insert a wrapped class or struct
type
into the module.
Params:
value |
The wrapped class or struct
type
to insert. This should be an instance of the WrapClass struct, and should have
already had its members added to it. |
name |
The name to give this
type
in the MiniD module. Defaults to "", which means it will use the D name of the
type
. |
Returns:
A chaining reference to this module.
- struct
WrapClass
(ClassType,Ctors...);
- Used to wrap a class or struct type that is exposed to MiniD. Since classes and structs are very similar, both can be
wrapped by this one template. They can have any number of constructors, methods, and properties, and structs will have all their
fields wrapped in opIndex and opIndexAssign metamethods so that they can be accessed from within MiniD.
Params:
ClassType |
The type of the class or struct to wrap. |
Ctors |
An optional list of function types which represent the constructors (or in the case of structs, static opCalls) which
should be wrapped. If this is left out, it will attempt to wrap the default (no-parameter) constructor. |
BUGS:
As explained in the module header, wrapping structs with private fields fails. You could try making a wrapper type for it, and
then wrapping that :X
Examples:
WrapClass!(MyClass, void function(int), void function(int, float)) // class type, followed by ctor types
.method!(MyClass.methodOne)() // wrap a method
.property!(MyClass.position)() // wrap a property
.addToNamespace(someNamespace); // register it into a namespace
- typeof(*this)
opCall
();
- Creates an instance of this struct so you can start wrapping your class or struct.
Returns:
An instance of this struct.
- template
method
(alias func,char[] name = NameOfFunc!(func),funcType = typeof(func))
template
method
(alias func,funcType)
- Wrap a class or struct
method
.
Params:
func |
An alias to the
method
to wrap. If your class type is "MyClass" and the
method
is "foo", use "MyClass.foo" (without the quotes)
as the parameter. |
name |
The name that should be used on the MiniD side to represent the
method
. Defaults to the D
method
name. |
funcType |
The type of the
method
to wrap. This defaults to typeof(func), but you'll need to specify this
explicitly if you're wrapping an overloaded
method
, in order to select the proper overload. |
Returns:
A chaining reference to this struct.
- template
property
(alias func,char[] name = NameOfFunc!(func),funcType = typeof(func))
template
property
(alias func,funcType)
- Wrap a
property
. MiniD doesn't have explicit support for properties, but the MiniD standard library follows a certain convention, which is
compatible with the D
property
convention. In MiniD, a
property
is represented as a function which sets the value when called with one
parameter, and which gets the value when called with none, just like in D. A wrapped
property
will call the getter and/or setter appropriately
when used from MiniD. The getter and setter are automatically determined, so you don't have to wrap them separately. It will also work if
your
property
is read-only or write-only.
Params:
func |
An alias to the
property
to wrap, just like with method wrapping. |
name |
The name that should be used on the MiniD side to represent the
property
. Defaults to the D
property
name. |
funcType |
The type of the
property
to wrap. This defaults to typeof(func), but you might have to use an explicit type if you have
another class method with the same name as the
property
but with a different number of parameters. This can be either
the type of the setter or the getter. |
Returns:
A chaining reference to this struct.
- void
makeGlobal
(MDContext ctx, dchar[] name = "");
- Registers this class as a global variable in the given context.
Params:
MDContext ctx |
The context to load this class into. |
dchar[] name |
The name to give the class in MiniD. Defaults to "", which means the class's D name will be used as the MiniD name. |
- void
addToNamespace
(MDNamespace ns, dchar[] name = "");
- Adds this class to some namespace.
Params:
MDNamespace ns |
The namespace to put this class in. |
dchar[] name |
The name to give the class in MiniD. Defaults to "", which means the class's D name will be used as the MiniD name. |
- void
WrapFunc
(alias func,char[] name = NameOfFunc!(func),funcType = typeof(&func))(MDNamespace ns);
template
WrapFunc
(alias func,funcType)
- Wraps a free function and places it into a given namespace.
Params:
func |
An alias to the function you want to wrap. |
name |
The name to call it in MiniD. Defaults to the D function name. |
funcType |
The type of the function to wrap. This defaults to typeof(func), but you'll need to specify this
explicitly if you're wrapping an overloaded function, in order to select the proper overload. |
ns |
The namespace to put the function in. |
- void
WrapGlobalFunc
(alias func,char[] name = NameOfFunc!(func),funcType = typeof(&func))(MDContext context);
template
WrapGlobalFunc
(alias func,funcType)
- Wraps a free function and places it into the global namespace of a given context.
Params:
func |
An alias to the function you want to wrap. |
name |
The name to call it in MiniD. Defaults to the D function name. |
funcType |
The type of the function to wrap. This defaults to typeof(func), but you'll need to specify this
explicitly if you're wrapping an overloaded function, in order to select the proper overload. |
context |
The context into whose global namespace this function will be placed. |
- template
MinArgs
(alias func,funcType = void)
- Given an alias to a function, this metafunction will give the minimum legal number of arguments it can be called with.
Even works for aliases to class methods.
- template
InitsOf
(T...)
- Given a type tuple, this metafunction will give an expression tuple of all the .init values for each type.
- template
FieldNames
(S,int idx = 0)
- Given a struct type, gives a tuple of strings of the names of fields in the struct.
|