2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** disposable-binding.h
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
2021-09-06 18:50:44 +00:00
|
|
|
** Copyright (C) 2021 Amaryllis Kulla <amaryllis.kulla@protonmail.com>
|
2013-09-01 14:27:21 +00:00
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DISPOSABLEBINDING_H
|
|
|
|
#define DISPOSABLEBINDING_H
|
|
|
|
|
|
|
|
#include "disposable.h"
|
|
|
|
#include "binding-util.h"
|
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
#include "mruby/array.h"
|
|
|
|
|
2013-12-11 19:46:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2014-08-09 16:35:01 +00:00
|
|
|
/* 'Children' are disposables that are disposed together
|
|
|
|
* with their parent. Currently this is only used by Viewport
|
|
|
|
* in RGSS1.
|
|
|
|
* FIXME: Disable this behavior when RGSS2 or 3 */
|
|
|
|
inline void
|
|
|
|
disposableAddChild(mrb_state *mrb, mrb_value disp, mrb_value child)
|
|
|
|
{
|
|
|
|
mrb_sym sym = getMrbData(mrb)->symbols[CSchildren];
|
|
|
|
mrb_value children = mrb_iv_get(mrb, disp, sym);
|
|
|
|
|
|
|
|
if (mrb_nil_p(children))
|
|
|
|
{
|
|
|
|
children = mrb_ary_new(mrb);
|
|
|
|
mrb_iv_set(mrb, disp, sym, children);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assumes children are never removed until destruction */
|
|
|
|
mrb_ary_push(mrb, children, child);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
disposableDisposeChildren(mrb_state *mrb, mrb_value disp)
|
|
|
|
{
|
|
|
|
MrbData *mrbData = getMrbData(mrb);
|
|
|
|
mrb_value children = mrb_iv_get(mrb, disp, mrbData->symbols[CSchildren]);
|
|
|
|
|
|
|
|
if (mrb_nil_p(children))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (mrb_int i = 0; i < RARRAY_LEN(children); ++i)
|
|
|
|
mrb_funcall_argv(mrb, mrb_ary_entry(children, i),
|
2014-10-24 16:29:08 +00:00
|
|
|
mrbData->symbols[CS_mkxp_dispose_alias], 0, 0);
|
2014-08-09 16:35:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 14:27:21 +00:00
|
|
|
template<class C>
|
|
|
|
MRB_METHOD(disposableDispose)
|
|
|
|
{
|
2014-09-23 19:12:58 +00:00
|
|
|
C *d = static_cast<C*>(DATA_PTR(self));
|
2014-08-09 16:35:01 +00:00
|
|
|
|
2014-09-23 19:12:58 +00:00
|
|
|
if (!d)
|
|
|
|
return mrb_nil_value();
|
|
|
|
|
|
|
|
if (d->isDisposed())
|
2014-08-09 16:35:01 +00:00
|
|
|
return mrb_nil_value();
|
|
|
|
|
2014-10-24 16:55:03 +00:00
|
|
|
if (rgssVer == 1)
|
|
|
|
disposableDisposeChildren(mrb, self);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-09-23 19:12:58 +00:00
|
|
|
d->dispose();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return mrb_nil_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
2014-09-23 19:12:58 +00:00
|
|
|
MRB_METHOD(disposableIsDisposed)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-08-09 16:35:01 +00:00
|
|
|
MRB_UNUSED_PARAM;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-09-23 19:12:58 +00:00
|
|
|
C *d = static_cast<C*>(DATA_PTR(self));
|
|
|
|
|
|
|
|
if (!d)
|
|
|
|
return mrb_true_value();
|
|
|
|
|
|
|
|
return mrb_bool_value(d->isDisposed());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
|
|
|
static void disposableBindingInit(mrb_state *mrb, RClass *klass)
|
|
|
|
{
|
|
|
|
mrb_define_method(mrb, klass, "dispose", disposableDispose<C>, MRB_ARGS_NONE());
|
2014-09-23 19:12:58 +00:00
|
|
|
mrb_define_method(mrb, klass, "disposed?", disposableIsDisposed<C>, MRB_ARGS_NONE());
|
2014-10-24 16:29:08 +00:00
|
|
|
|
2014-10-24 16:55:03 +00:00
|
|
|
if (rgssVer == 1)
|
|
|
|
mrb_alias_method(mrb, klass, getMrbData(mrb)->symbols[CS_mkxp_dispose_alias],
|
|
|
|
mrb_intern_lit(mrb, "dispose"));
|
2014-09-23 19:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
|
|
|
inline void
|
|
|
|
checkDisposed(mrb_state *mrb, mrb_value self)
|
|
|
|
{
|
|
|
|
if (mrb_test(disposableIsDisposed<C>(0, self)))
|
|
|
|
raiseDisposedAccess(mrb, self);
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DISPOSABLEBINDING_H
|