Xparam

Xparam(name, default_value)

Xdef_ctorN, Xdef_fun, Xdef_methodの直後に記述することで、そのメソッドのデフォルト引数および名前つき引数の名前を設定することが出来ます。

後ろからXparamの数だけデフォルト引数が設定され、デフォルト引数の値は呼び出した順にその中での先頭から設定されます。

例えば、

class Foo{
public:
    void foo(int p1, const StringPtr& p2, int p3, int p4);
};

というメンバ関数があったときに

XTAL_BIND(Foo){
    Xdef_method(foo);
        Xparam(name, "");
        Xparam(x, 0);
        Xparam(y, 0);
}

とデフォルト引数を指定すると、p2に渡されるデフォルト引数として(名前name, 値””)、p3に渡されるデフォルト引数として(名前x, 値0)、…と設定されます。C++における実際の引数名とXtalにバインドする引数名は一致している必要はありません。

Example

C++

class Rect{
public:
    int x, y, w, h;
 
public:
    Rect(int x, int y, int w, int h) :
        x(x),
        y(y),
        w(w),
        h(h)
    {
    }
 
    void reset(int x, int y, int w, int h){
        this->x = x;
        this->y = y;
        this->w = w;
        this->h = h;
    }
 
    StringPtr to_s() const{
        return Xf("Rect(%d, %d, %d, %d)")->call(x, y, w, h)->to_s();
    }
};
 
XTAL_PREBIND(Rect){
    Xdef_ctor4(int, int, int, int);
        Xparam(x, 0);
        Xparam(y, 0);
        Xparam(w, -1);
        Xparam(h, -1);
}
XTAL_BIND(Rect){
    Xdef_var(x);
    Xdef_var(y);
    Xdef_var(w);
    Xdef_var(h);
 
    Xdef_method(reset);
        Xparam(x, 0);
        Xparam(y, 0);
        Xparam(w, 0);
        Xparam(h, 0);
    Xdef_method(to_s);
}
 
void exec_xtal(){
    xtal::global()->def(Xid(Rect), xtal::cpp_class<Rect>());
    Xsrc((
            // 名前つき引数
            rect : Rect(w:32, h:32);
            rect.p;
            // 普通に先頭二つを指定
            rect.reset(10, 20);
            rect.p;
            rect.w = 24;
            rect.h = 16;
            rect.p;
    ))->call();
}

Output

Rect(0, 0, 32, 32)
Rect(10, 20, 0, 0)
Rect(10, 20, 24, 16)
reference/xparam.txt · 最終更新: 2011/09/04 00:10 by sukai
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0