Source

pyd / infrastructure / pyd / struct_wrap.d

/*
Copyright 2006, 2007 Kirk McDonald

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/**
  Contains utilities for wrapping D structs.
  */
module pyd.struct_wrap;

import deimos.python.Python;

import pyd.def;
import pyd.class_wrap;
import pyd.exception;
import pyd.make_object;

// It is intended that all of these templates accept a pointer-to-struct type
// as a template parameter, rather than the struct type itself.

template wrapped_member(T, string name, _M=void) {
    alias wrapped_class_type!(T) type;
    alias wrapped_class_object!(T) obj;
    static if (is(_M == void)) {
        mixin("alias typeof(T."~name~") M;");
    } else {
        alias _M M;
    }
    extern(C)
    PyObject* get(PyObject* self, void* closure) {
        return exception_catcher(delegate PyObject*() {
            T t = (cast(obj*)self).d_obj;
            mixin("return d_to_python(t."~name~");");
        });
    }

    extern(C)
    int set(PyObject* self, PyObject* value, void* closure) {
        return exception_catcher(delegate int() {
            T t = (cast(obj*)self).d_obj;
            mixin("t."~name~" = python_to_d!(M)(value);");
            return 0;
        });
    }
}

/**
Wrap a member variable of a class or struct.

Params:
name = The name of the member to wrap
Options = Optional parameters. Takes Docstring!(docstring), PyName!(pyname), 
and Mode!(mode)
pyname = The name of the member as it will appear in Python. Defaults to name
mode = specifies whether this member is readable, writable. possible values 
are "r", "w", "rw". Defaults to "rw".
docstring = The function's docstring. Defaults to "".
*/
struct Member(string name, Options...) {
    alias Args!("","", name, "rw",Options) args;
    mixin _Member!(name, args.pyname, args.mode, args.docstring);
}

template _Member(string realname, string pyname, string mode, string docstring) {
    static const bool needs_shim = false;
    static void call(string classname, T) () {
        pragma(msg, "struct.member: " ~ pyname);
        static PyGetSetDef empty = {null, null, null, null, null};
        alias wrapped_prop_list!(T) list;
        list[$-1].name = (pyname ~ "\0").dup.ptr;
        static if(countUntil(mode, "r") != -1) {
            list[$-1].get = &wrapped_member!(T, realname).get;
        }
        static if(countUntil(mode, "w") != -1) {
            list[$-1].set = &wrapped_member!(T, realname).set;
        }
        list[$-1].doc = (docstring~"\0").dup.ptr;
        list[$-1].closure = null;
        list ~= empty;
        wrapped_class_type!(T).tp_getset = list.ptr;
    }
}

/// Wrap a struct.
alias wrap_class wrap_struct;