| 1 |
#ifndef RBIMPL_RSTRUCT_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_RSTRUCT_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Routines to manipulate struct RStruct. |
| 22 |
* @note The struct RStruct itself is opaque. |
| 23 |
*/ |
| 24 |
#include "ruby/internal/attr/artificial.h" |
| 25 |
#include "ruby/internal/dllexport.h" |
| 26 |
#include "ruby/internal/value.h" |
| 27 |
#include "ruby/internal/value_type.h" |
| 28 |
#include "ruby/internal/arithmetic/long.h" |
| 29 |
#include "ruby/internal/arithmetic/int.h" |
| 30 |
#if !defined RUBY_EXPORT && !defined RUBY_NO_OLD_COMPATIBILITY |
| 31 |
# include "ruby/backward.h" |
| 32 |
#endif |
| 33 |
|
| 34 |
/** |
| 35 |
* @private |
| 36 |
* |
| 37 |
* @deprecated This macro once was a thing in the old days, but makes no sense |
| 38 |
* any longer today. Exists here for backwards compatibility |
| 39 |
* only. You can safely forget about it. |
| 40 |
* |
| 41 |
* @internal |
| 42 |
* |
| 43 |
* Declaration of rb_struct_ptr() is at include/ruby/backward.h. |
| 44 |
*/ |
| 45 |
#define RSTRUCT_PTR(st) rb_struct_ptr(st) |
| 46 |
/** @cond INTERNAL_MACRO */ |
| 47 |
#define RSTRUCT_LEN RSTRUCT_LEN |
| 48 |
#define RSTRUCT_SET RSTRUCT_SET |
| 49 |
#define RSTRUCT_GET RSTRUCT_GET |
| 50 |
/** @endcond */ |
| 51 |
|
| 52 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 53 |
/** |
| 54 |
* Returns the number of struct members. |
| 55 |
* |
| 56 |
* @param[in] st An instance of RStruct. |
| 57 |
* @return The number of members of `st`. |
| 58 |
* @pre `st` must be of ::RUBY_T_STRUCT. |
| 59 |
*/ |
| 60 |
VALUE rb_struct_size(VALUE st); |
| 61 |
|
| 62 |
/** |
| 63 |
* Resembles `Struct#[]`. |
| 64 |
* |
| 65 |
* @param[in] st An instance of RStruct. |
| 66 |
* @param[in] k Index a.k.a. key of the struct. |
| 67 |
* @exception rb_eTypeError `k` is neither Numeric, Symbol, nor String. |
| 68 |
* @exception rb_eIndexError Numerical index out of range. |
| 69 |
* @exception rb_eNameError No such key. |
| 70 |
* @return The member stored at `k` in `st`. |
| 71 |
* @pre `st` must be of ::RUBY_T_STRUCT. |
| 72 |
*/ |
| 73 |
VALUE rb_struct_aref(VALUE st, VALUE k); |
| 74 |
|
| 75 |
/** |
| 76 |
* Resembles `Struct#[]=`. |
| 77 |
* |
| 78 |
* @param[out] st An instance of RStruct. |
| 79 |
* @param[in] k Index a.k.a. key of the struct. |
| 80 |
* @param[in] v Value to store. |
| 81 |
* @exception rb_eTypeError `k` is neither Numeric, Symbol, nor String. |
| 82 |
* @exception rb_eIndexError Numerical index out of range. |
| 83 |
* @exception rb_eNameError No such key. |
| 84 |
* @return Passed `v`. |
| 85 |
* @pre `st` must be of ::RUBY_T_STRUCT. |
| 86 |
* @post `v` is stored at `k` in `st`. |
| 87 |
*/ |
| 88 |
VALUE rb_struct_aset(VALUE st, VALUE k, VALUE v); |
| 89 |
RBIMPL_SYMBOL_EXPORT_END() |
| 90 |
|
| 91 |
RBIMPL_ATTR_ARTIFICIAL() |
| 92 |
/** @copydoc rb_struct_size() */ |
| 93 |
static inline long |
| 94 |
RSTRUCT_LEN(VALUE st) |
| 95 |
{ |
| 96 |
RBIMPL_ASSERT_TYPE(st, RUBY_T_STRUCT); |
| 97 |
|
| 98 |
return RB_NUM2LONG(rb_struct_size(st)); |
| 99 |
} |
| 100 |
|
| 101 |
RBIMPL_ATTR_ARTIFICIAL() |
| 102 |
/** @copydoc rb_struct_aset() */ |
| 103 |
static inline VALUE |
| 104 |
RSTRUCT_SET(VALUE st, int k, VALUE v) |
| 105 |
{ |
| 106 |
RBIMPL_ASSERT_TYPE(st, RUBY_T_STRUCT); |
| 107 |
|
| 108 |
return rb_struct_aset(st, INT2NUM(k), (v)); |
| 109 |
} |
| 110 |
|
| 111 |
RBIMPL_ATTR_ARTIFICIAL() |
| 112 |
/** @copydoc rb_struct_aref() */ |
| 113 |
static inline VALUE |
| 114 |
RSTRUCT_GET(VALUE st, int k) |
| 115 |
{ |
| 116 |
RBIMPL_ASSERT_TYPE(st, RUBY_T_STRUCT); |
| 117 |
|
| 118 |
return rb_struct_aref(st, INT2NUM(k)); |
| 119 |
} |
| 120 |
|
| 121 |
#endif /* RBIMPL_RSTRUCT_H */ |