Skip to content

Commit

Permalink
modules/system: Split helpers header into smaller bits
Browse files Browse the repository at this point in the history
There is a very good reason for this, I think...
  • Loading branch information
IceDragon200 committed Jul 15, 2016
1 parent 9b8033a commit eafea43
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
39 changes: 4 additions & 35 deletions modules/system/include/moon/mrb/helpers.hxx
Original file line number Diff line number Diff line change
@@ -1,40 +1,9 @@
#ifndef MOON_MRB_HELPERS
#define MOON_MRB_HELPERS

#include <mruby.h>
#include <mruby/data.h>
#include <mruby/string.h>
#include <mruby/variable.h>
#include "moon/mrb/helpers/class.hxx"
#include "moon/mrb/helpers/variable.hxx"
#include "moon/mrb/helpers/error.hxx"
#include "moon/mrb/helpers/data.hxx"

#define IVget(_name_) mrb_iv_get(mrb, self, mrb_intern_lit(mrb, _name_))
#define IVset(_name_, _value_) mrb_iv_set(mrb, self, mrb_intern_lit(mrb, _name_), _value_)

#define KEY_SHADER "@shader"
#define KEY_VBO "@vbo"
#define KEY_ORIGIN "@origin"

#define MOON_GET_CLASS(__name__) mrb_class_get_under(mrb, mrb_module_get(mrb, "Moon"), __name__)
#define MOON_GET_MODULE(__name__) mrb_module_get_under(mrb, mrb_module_get(mrb, "Moon"), __name__)
;

static inline mrb_value
moon_iv_get(mrb_state *mrb, mrb_value self, const char *name)
{
mrb_value iv_value = mrb_iv_get(mrb, self, mrb_intern_cstr(mrb, name));
if (mrb_nil_p(iv_value)) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "Cannot use a nil %S", mrb_str_new_cstr(mrb, name));
}
return iv_value;
}

static inline void
moon_data_cleanup(mrb_state *mrb, mrb_value self)
{
void *ptr = DATA_PTR(self);
const struct mrb_data_type* type = DATA_TYPE(self);
if (ptr && type) {
type->dfree(mrb, ptr);
}
DATA_PTR(self) = NULL;
}
#endif
10 changes: 10 additions & 0 deletions modules/system/include/moon/mrb/helpers/class.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef MOON_MRB_CLASS_HELPERS
#define MOON_MRB_CLASS_HELPERS

#include <mruby.h>
#include <mruby/class.h>

#define MOON_GET_CLASS(__name__) mrb_class_get_under(mrb, mrb_module_get(mrb, "Moon"), __name__)
#define MOON_GET_MODULE(__name__) mrb_module_get_under(mrb, mrb_module_get(mrb, "Moon"), __name__)

#endif
18 changes: 18 additions & 0 deletions modules/system/include/moon/mrb/helpers/data.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef MOON_MRB_DATA_HELPERS
#define MOON_MRB_DATA_HELPERS

#include <mruby.h>
#include <mruby/data.h>

static inline void
moon_data_cleanup(mrb_state* mrb, mrb_value self)
{
void *ptr = DATA_PTR(self);
const struct mrb_data_type* type = DATA_TYPE(self);
if (ptr && type) {
type->dfree(mrb, ptr);
}
DATA_PTR(self) = NULL;
}

#endif
8 changes: 8 additions & 0 deletions modules/system/include/moon/mrb/helpers/error.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef MOON_MRB_ERROR_HELPERS
#define MOON_MRB_ERROR_HELPERS

#include "moon/mrb/helpers/class.hxx"

#define MOON_E_FILE_NOT_FOUND MOON_GET_CLASS("FileNotFoundError")

#endif
23 changes: 23 additions & 0 deletions modules/system/include/moon/mrb/helpers/variable.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef MOON_MRB_VARIABLE_HELPERS
#define MOON_MRB_VARIABLE_HELPERS

#include <mruby.h>
#include <mruby/variable.h>
#include <mruby/string.h>

#define IVget(_name_) mrb_iv_get(mrb, self, mrb_intern_lit(mrb, _name_))
#define IVset(_name_, _value_) mrb_iv_set(mrb, self, mrb_intern_lit(mrb, _name_), _value_)

static inline mrb_value
moon_iv_get(mrb_state* mrb, mrb_value self, const char* name)
{
mrb_value iv_value = mrb_iv_get(mrb, self, mrb_intern_cstr(mrb, name));
if (mrb_nil_p(iv_value)) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "Cannot use a nil %S", mrb_str_new_cstr(mrb, name));
}
return iv_value;
}

#define KEY_VBO "@vbo"

#endif
2 changes: 1 addition & 1 deletion modules/system/include/moon/mrb_err.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
#include <mruby/class.h>
#include <mruby/value.h>

extern bool mmrb_check_class(mrb_state *mrb, mrb_value obj, struct RClass *klass, bool quiet);
extern bool mmrb_check_class(mrb_state *mrb, mrb_value obj, struct RClass* klass, bool quiet);

#endif
5 changes: 5 additions & 0 deletions modules/system/mrblib/file_not_found_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Moon
# Error raised when a resource cannot be found on disk
class FileNotFoundError < RuntimeError
end
end

0 comments on commit eafea43

Please sign in to comment.