
main :- dynobj, statobj.

dynobj :-
    format('~N~nTesting creating and accessing a dynamic C++ object~n',[]),
    create(Obj),
    testi(Obj),
    testt(Obj).

statobj :-
    format('~N~nTesting accessing a static C++ object~n',[]),
    stat_obj(Obj),
    testi(Obj),
    testt(Obj).

testi(Obj) :-
    format('~N~nTesting setting a C "long" value as a long and reading it as long and as a term~n',[]),
    X = 4711,
    format('~NCalling Obj.set_i(~w)\n', [X]),
    set_i(Obj, X),
    format('~NCalling Obj.get_i()..', []),
    get_i(Obj, Y),
    format('. got back the (C long) ~w..', [Y]),
    ( X == Y ->
        format('..OK~n', [])
    ; otherwise ->
        format('..ERROR~n', [])
    ),
    format('~NCalling Obj.get_t()..', []),
    get_t(Obj, Z),
    format('. got back the (term) ~w..', [Z]),
    ( X == Z ->
        format('..OK~n', [])
    ; otherwise ->
        format('..ERROR~n', [])
    ).


testt(Obj) :-
    format('~N~nTesting setting a C "long"  value as a term and reading it as long and as a term~n',[]),
    X = -59837,
    format('~NCalling Obj.set_t(~w)\n', [X]),
    set_t(Obj, X),

    format('~NCalling Obj.get_t()..', []),
    get_t(Obj, Y),
    format('. got back the (term) ~w..', [Y]),
    ( X == Y ->
        format('..OK~n', [])
    ; otherwise ->
        format('..ERROR~n', [])
    ),
    format('~NCalling Obj.get_i()..', []),
    get_i(Obj, Z),
    format('. got back the (C long) ~w..', [Z]),
    ( X == Z ->
        format('..OK~n', [])
    ; otherwise ->
        format('..ERROR~n', [])
    ).


foreign(cpptest_create, create([-integer])).
foreign(cpptest_stat_obj, stat_obj([-integer])).
foreign(cpptest_get_i, get_i(+integer,[-integer])).
foreign(cpptest_set_i, set_i(+integer,+integer)).
foreign(cpptest_get_t, get_t(+integer,[-term])).
foreign(cpptest_set_t, set_t(+integer,+term)).

foreign_resource(cplus, [
	cpptest_create,
	cpptest_stat_obj,
	cpptest_get_i,
	cpptest_set_i,
	cpptest_get_t,
	cpptest_set_t
]).

:- load_foreign_resource(cplus).
