mruby/c VM Source Code release 3.4
Loading...
Searching...
No Matches
class.h File Reference

Class related functions. More...

#include "value.h"
#include "keyvalue.h"
#include "error.h"
#include "_autogen_builtin_class.h"
Include dependency graph for class.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  RClass
 Class object. More...
struct  RBuiltinClass
 Built-in class object. More...
struct  RBuiltinNoMethodClass
 Built-in No method class object. More...
struct  RInstance
 Instance object. More...
struct  RMethod
 Method management structure. More...
struct  MRBC_DEFINE_METHOD_LIST
 for mrbc_define_method_list function. More...

Macros

#define MRBC_TRAVERSE_NEST_LEVEL   3
#define MRBC_CLASS(cls)
#define MRBC_INSTANCE_DATA_PTR(v, t)
#define mrbc_class_object   ((struct RClass*)(&mrbc_class_Object))

Typedefs

typedef struct RClass mrbc_class
 Class object.
typedef struct RClass mrb_class
typedef struct RInstance mrbc_instance
 Instance object.
typedef struct RInstance mrb_instance
typedef struct RMethod mrbc_method
 Method management structure.

Functions

static mrbc_classfind_class_by_object (const mrbc_value *obj)
static void mrbc_define_destructor (mrbc_class *cls, void(*destructor)(mrbc_value *))
static mrbc_valuemrbc_instance_getiv_p (mrbc_value *obj, mrbc_sym sym_id)
static void mrbc_define_method_list (struct VM *vm, mrbc_class *cls, const struct MRBC_DEFINE_METHOD_LIST list[], int list_size)

Variables

struct RClass *const mrbc_class_tbl []

Detailed Description

Class related functions.

Copyright (C) 2015- Kyushu Institute of Technology.
Copyright (C) 2015- Shimane IT Open-Innovation Center.

This file is distributed under BSD 3-Clause License.

Definition in file class.h.

Macro Definition Documentation

◆ MRBC_CLASS

#define MRBC_CLASS ( cls)
Value:
((mrbc_class *)(&mrbc_class_##cls))
struct RClass mrbc_class
Class object.

Get a built-in class (pointer)

Parameters
clsClass name. (e.g. Array)
Returns
Pointer to the class.

Example

mrbc_class *cls = MRBC_CLASS(Array); // get a Array class
mrbc_class *cls = MRBC_CLASS(String); // get a String class
#define MRBC_CLASS(cls)
Definition class.h:51

Definition at line 51 of file class.h.

◆ mrbc_class_object

#define mrbc_class_object   ((struct RClass*)(&mrbc_class_Object))

Definition at line 200 of file class.h.

◆ MRBC_INSTANCE_DATA_PTR

#define MRBC_INSTANCE_DATA_PTR ( v,
t )
Value:
((t *)((v)->instance->data))

Get a pointer to mrbc_instance->data converted to specified type.

Parameters
vPointer to mrbc_instance.
tType of return pointer.

Example

// store a int value.
// get
int n = *MRBC_INSTANCE_DATA_PTR(v, int);
// store a pointer to statically allocated memory.
*MRBC_INSTANCE_DATA_PTR(v, struct STATIC_STRUCT *) = STATIC_STRUCT;
// get
struct STATIC_STRUCT *p = *MRBC_INSTANCE_DATA_PTR(v, struct STATIC_STRUCT *);
#define MRBC_INSTANCE_DATA_PTR(v, t)
Definition class.h:75

Definition at line 75 of file class.h.

◆ MRBC_TRAVERSE_NEST_LEVEL

#define MRBC_TRAVERSE_NEST_LEVEL   3

Definition at line 34 of file class.h.

Typedef Documentation

◆ mrb_class

typedef struct RClass mrb_class

Definition at line 100 of file class.h.

◆ mrb_instance

typedef struct RInstance mrb_instance

Definition at line 163 of file class.h.

◆ mrbc_class

typedef struct RClass mrbc_class

Class object.

◆ mrbc_instance

typedef struct RInstance mrbc_instance

Instance object.

◆ mrbc_method

typedef struct RMethod mrbc_method

Method management structure.

Function Documentation

◆ find_class_by_object()

mrbc_class * find_class_by_object ( const mrbc_value * obj)
inlinestatic

find class by object

Parameters
objpointer to object
Returns
pointer to mrbc_class

Definition at line 235 of file class.h.

Here is the caller graph for this function:

◆ mrbc_define_destructor()

void mrbc_define_destructor ( mrbc_class * cls,
void(* destructor )(mrbc_value *) )
inlinestatic

Define the destructor

Parameters
clsclass.
destructordestructor.

Define the destructor. This function can only be defined for destruction of mrbc_instance.

Definition at line 265 of file class.h.

◆ mrbc_define_method_list()

void mrbc_define_method_list ( struct VM * vm,
mrbc_class * cls,
const struct MRBC_DEFINE_METHOD_LIST list[],
int list_size )
inlinestatic

define method by method list.

Parameters
vmdummy.
clstarget class.
listmethod list.
list_sizesize of method list.

Code example

static const struct MRBC_DEFINE_METHOD_LIST method_list[] = {
{ "method1", c_method1 },
{ "method2", c_method2 },
// ...
};
mrbc_class *cls = mrbc_define_class(0, "MyClass", 0);
mrbc_define_method_list(0, cls, method_list, sizeof(method_list)/sizeof(method_list[0]));
mrbc_class * mrbc_define_class(struct VM *vm, const char *name, mrbc_class *super)
Definition class.c:118
static void mrbc_define_method_list(struct VM *vm, mrbc_class *cls, const struct MRBC_DEFINE_METHOD_LIST list[], int list_size)
Definition class.h:311
for mrbc_define_method_list function.
Definition class.h:189

This function is used to define multiple methods at once. It is useful for defining many methods.

Definition at line 311 of file class.h.

Here is the call graph for this function:

◆ mrbc_instance_getiv_p()

mrbc_value * mrbc_instance_getiv_p ( mrbc_value * obj,
mrbc_sym sym_id )
inlinestatic

instance variable getter

Parameters
objtarget object.
sym_idkey symbol ID.
Returns
pointer to value or NULL.

Definition at line 281 of file class.h.

Here is the call graph for this function:

Variable Documentation

◆ mrbc_class_tbl

struct RClass* const mrbc_class_tbl[]
extern

Builtin class table.

Note
must be same order as mrbc_vtype.
See also
mrbc_vtype in value.h

Definition at line 37 of file class.c.