Ruby 3.2.1p31 (2023-02-08 revision 31819e82c88c6f8ecfaeb162519bfa26a14b21fd)
Data Fields
RTypedData Struct Reference

"Typed" user data. More...

#include <rtypeddata.h>

Data Fields

struct RBasic basic
 The part that all ruby objects have in common.
 
const rb_data_type_ttype
 This field stores various information about how Ruby should handle a data.
 
VALUE typed_flag
 This has to be always 1.
 
void * data
 Pointer to the actual C level struct that you want to wrap.
 

Detailed Description

"Typed" user data.

By using this, extension libraries can wrap a C struct to make it visible from Ruby. For instance if you have a struct timeval, and you want users to use it,

static inline const rb_data_type_t timeval_type = {
// Note that unspecified fields are 0-filled by default.
.wrap_struct_name = "timeval",
.function = {
.dmark = nullptr, // no need to mark
.dfree = RUBY_TYPED_DEFAULT_FREE, // use ruby_xfree()
.dsize = [](auto) {
return sizeof(struct timeval);
},
},
};
extern "C" void
Init_timeval(void)
{
auto klass = rb_define_class("YourName", rb_cObject);
rb_define_alloc_func(klass, [](auto klass) {
struct timeval *t;
klass, struct timeval, &timeval_type, t);
if (auto i = gettimeofday(t, nullptr); i == -1) {
rb_sys_fail("gettimeofday(3)");
}
else {
return ret;
}
});
}
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:888
void rb_sys_fail(const char *mesg)
Converts a C errno into a Ruby exception, then raises it.
Definition error.c:3272
VALUE rb_cObject
Documented in include/ruby/internal/globals.h.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:79
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
Definition rtypeddata.h:489
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:197

Definition at line 340 of file rtypeddata.h.

Field Documentation

◆ basic

struct RBasic RTypedData::basic

The part that all ruby objects have in common.

Definition at line 343 of file rtypeddata.h.

◆ data

void * RTypedData::data

Pointer to the actual C level struct that you want to wrap.

Definition at line 362 of file rtypeddata.h.

◆ type

const rb_data_type_t * RTypedData::type

This field stores various information about how Ruby should handle a data.

This roughly resembles a Ruby level class (apart from method definition etc.)

Definition at line 350 of file rtypeddata.h.

◆ typed_flag

VALUE RTypedData::typed_flag

This has to be always 1.

Definition at line 359 of file rtypeddata.h.


The documentation for this struct was generated from the following files: