mbed I/F binding for mruby
Dependents: mruby_mbed_web mirb_mbed
mbed-mruby
How to use
Class
include/mruby/data.h@1:8ccd1d494a4b, 2015-04-13 (annotated)
- Committer:
- mzta
- Date:
- Mon Apr 13 05:20:15 2015 +0000
- Revision:
- 1:8ccd1d494a4b
- Parent:
- 0:158c61bb030f
- code refactoring.; - add SPI, SPISlave, I2C class to mruby-mbed (Incomplete).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mzta | 0:158c61bb030f | 1 | /* |
mzta | 0:158c61bb030f | 2 | ** mruby/data.h - Data class |
mzta | 0:158c61bb030f | 3 | ** |
mzta | 0:158c61bb030f | 4 | ** See Copyright Notice in mruby.h |
mzta | 0:158c61bb030f | 5 | */ |
mzta | 0:158c61bb030f | 6 | |
mzta | 0:158c61bb030f | 7 | #ifndef MRUBY_DATA_H |
mzta | 0:158c61bb030f | 8 | #define MRUBY_DATA_H |
mzta | 0:158c61bb030f | 9 | |
mzta | 0:158c61bb030f | 10 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 11 | extern "C" { |
mzta | 0:158c61bb030f | 12 | #endif |
mzta | 0:158c61bb030f | 13 | |
mzta | 0:158c61bb030f | 14 | typedef struct mrb_data_type { |
mzta | 0:158c61bb030f | 15 | const char *struct_name; |
mzta | 0:158c61bb030f | 16 | void (*dfree)(mrb_state *mrb, void*); |
mzta | 0:158c61bb030f | 17 | } mrb_data_type; |
mzta | 0:158c61bb030f | 18 | |
mzta | 0:158c61bb030f | 19 | struct RData { |
mzta | 0:158c61bb030f | 20 | MRB_OBJECT_HEADER; |
mzta | 0:158c61bb030f | 21 | struct iv_tbl *iv; |
mzta | 0:158c61bb030f | 22 | const mrb_data_type *type; |
mzta | 0:158c61bb030f | 23 | void *data; |
mzta | 0:158c61bb030f | 24 | }; |
mzta | 0:158c61bb030f | 25 | |
mzta | 0:158c61bb030f | 26 | MRB_API struct RData *mrb_data_object_alloc(mrb_state *mrb, struct RClass* klass, void *datap, const mrb_data_type *type); |
mzta | 0:158c61bb030f | 27 | |
mzta | 0:158c61bb030f | 28 | #define Data_Wrap_Struct(mrb,klass,type,ptr)\ |
mzta | 0:158c61bb030f | 29 | mrb_data_object_alloc(mrb,klass,ptr,type) |
mzta | 0:158c61bb030f | 30 | |
mzta | 0:158c61bb030f | 31 | #define Data_Make_Struct(mrb,klass,strct,type,sval,data) do { \ |
mzta | 0:158c61bb030f | 32 | sval = mrb_malloc(mrb, sizeof(strct)); \ |
mzta | 0:158c61bb030f | 33 | { static const strct zero = { 0 }; *sval = zero; };\ |
mzta | 0:158c61bb030f | 34 | data = Data_Wrap_Struct(mrb,klass,type,sval);\ |
mzta | 0:158c61bb030f | 35 | } while (0) |
mzta | 0:158c61bb030f | 36 | |
mzta | 0:158c61bb030f | 37 | #define RDATA(obj) ((struct RData *)(mrb_ptr(obj))) |
mzta | 0:158c61bb030f | 38 | #define DATA_PTR(d) (RDATA(d)->data) |
mzta | 0:158c61bb030f | 39 | #define DATA_TYPE(d) (RDATA(d)->type) |
mzta | 0:158c61bb030f | 40 | MRB_API void mrb_data_check_type(mrb_state *mrb, mrb_value, const mrb_data_type*); |
mzta | 0:158c61bb030f | 41 | MRB_API void *mrb_data_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_type*); |
mzta | 0:158c61bb030f | 42 | #define DATA_GET_PTR(mrb,obj,dtype,type) (type*)mrb_data_get_ptr(mrb,obj,dtype) |
mzta | 0:158c61bb030f | 43 | MRB_API void *mrb_data_check_get_ptr(mrb_state *mrb, mrb_value, const mrb_data_type*); |
mzta | 0:158c61bb030f | 44 | #define DATA_CHECK_GET_PTR(mrb,obj,dtype,type) (type*)mrb_data_check_get_ptr(mrb,obj,dtype) |
mzta | 0:158c61bb030f | 45 | |
mzta | 0:158c61bb030f | 46 | /* obsolete functions and macros */ |
mzta | 0:158c61bb030f | 47 | #define mrb_data_check_and_get(mrb,obj,dtype) mrb_data_get_ptr(mrb,obj,dtype) |
mzta | 0:158c61bb030f | 48 | #define mrb_get_datatype(mrb,val,type) mrb_data_get_ptr(mrb, val, type) |
mzta | 0:158c61bb030f | 49 | #define mrb_check_datatype(mrb,val,type) mrb_data_get_ptr(mrb, val, type) |
mzta | 0:158c61bb030f | 50 | #define Data_Get_Struct(mrb,obj,type,sval) do {\ |
mzta | 0:158c61bb030f | 51 | *(void**)&sval = mrb_data_get_ptr(mrb, obj, type); \ |
mzta | 0:158c61bb030f | 52 | } while (0) |
mzta | 0:158c61bb030f | 53 | |
mzta | 0:158c61bb030f | 54 | static inline void |
mzta | 0:158c61bb030f | 55 | mrb_data_init(mrb_value v, void *ptr, const mrb_data_type *type) |
mzta | 0:158c61bb030f | 56 | { |
mzta | 0:158c61bb030f | 57 | mrb_assert(mrb_type(v) == MRB_TT_DATA); |
mzta | 0:158c61bb030f | 58 | DATA_PTR(v) = ptr; |
mzta | 0:158c61bb030f | 59 | DATA_TYPE(v) = type; |
mzta | 0:158c61bb030f | 60 | } |
mzta | 0:158c61bb030f | 61 | |
mzta | 0:158c61bb030f | 62 | #if defined(__cplusplus) |
mzta | 0:158c61bb030f | 63 | } /* extern "C" { */ |
mzta | 0:158c61bb030f | 64 | #endif |
mzta | 0:158c61bb030f | 65 | |
mzta | 0:158c61bb030f | 66 | #endif /* MRUBY_DATA_H */ |
mzta | 0:158c61bb030f | 67 |