Debian Codegraph @

include/ruby/internal/intern/struct.h

raw
1 #ifndef RBIMPL_INTERN_STRUCT_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_STRUCT_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 Public APIs related to ::rb_cStruct.
22 */
23 #include "ruby/internal/attr/nonnull.h"
24 #include "ruby/internal/dllexport.h"
25 #include "ruby/internal/intern/vm.h" /* rb_alloc_func_t */
26 #include "ruby/internal/value.h"
27
28 RBIMPL_SYMBOL_EXPORT_BEGIN()
29
30 /* struct.c */
31
32 /**
33 * Creates an instance of the given struct.
34 *
35 * @param[in] klass The class of the instance to allocate.
36 * @param[in] ... The fields.
37 * @return Allocated instance of `klass`.
38 * @pre `klass` must be a subclass of ::rb_cStruct.
39 * @note Number of variadic arguments must much that of the passed klass'
40 * fields.
41 */
42 VALUE rb_struct_new(VALUE klass, ...);
43
44 /**
45 * Defines a struct class.
46 *
47 * @param[in] name Name of the class.
48 * @param[in] ... Arbitrary number of `const char*`, terminated by
49 * NULL. Each of which are the name of fields.
50 * @exception rb_eNameError `name` is not a constant name.
51 * @exception rb_eTypeError `name` is already taken.
52 * @exception rb_eArgError Duplicated field name.
53 * @return The defined class.
54 * @post Global toplevel constant `name` is defined.
55 * @note `name` is allowed to be a null pointer. This function creates
56 * an anonymous struct class then.
57 *
58 * @internal
59 *
60 * Not seriously checked but it seems this function does not share its
61 * implementation with how `Struct.new` is implemented...?
62 */
63 VALUE rb_struct_define(const char *name, ...);
64
65 RBIMPL_ATTR_NONNULL((2))
66 /**
67 * Identical to rb_struct_define(), except it defines the class under the
68 * specified namespace instead of global toplevel.
69 *
70 * @param[out] space Namespace that the defining class shall reside.
71 * @param[in] name Name of the class.
72 * @param[in] ... Arbitrary number of `const char*`, terminated by
73 * NULL. Each of which are the name of fields.
74 * @exception rb_eNameError `name` is not a constant name.
75 * @exception rb_eTypeError `name` is already taken.
76 * @exception rb_eArgError Duplicated field name.
77 * @return The defined class.
78 * @post `name` is a constant under `space`.
79 * @note In contrast to rb_struct_define(), it doesn't make any sense to
80 * pass a null pointer to this function.
81 */
82 VALUE rb_struct_define_under(VALUE space, const char *name, ...);
83
84 /**
85 * Identical to rb_struct_new(), except it takes the field values as a Ruby
86 * array.
87 *
88 * @param[in] klass The class of the instance to allocate.
89 * @param[in] values Field values.
90 * @return Allocated instance of `klass`.
91 * @pre `klass` must be a subclass of ::rb_cStruct.
92 * @pre `values` must be an instance of struct ::RArray.
93 */
94 VALUE rb_struct_alloc(VALUE klass, VALUE values);
95
96 /**
97 * Mass-assigns a struct's fields.
98 *
99 * @param[out] self An instance of a struct class to squash.
100 * @param[in] values New values.
101 * @return ::RUBY_Qnil.
102 */
103 VALUE rb_struct_initialize(VALUE self, VALUE values);
104
105 /**
106 * Identical to rb_struct_aref(), except it takes ::ID instead of ::VALUE.
107 *
108 * @param[in] self An instance of a struct class.
109 * @param[in] key Key to query.
110 * @exception rb_eTypeError `self` is not a struct.
111 * @exception rb_eNameError No such field.
112 * @return The value stored at `key` in `self`.
113 */
114 VALUE rb_struct_getmember(VALUE self, ID key);
115
116 /**
117 * Queries the list of the names of the fields of the given struct class.
118 *
119 * @param[in] klass A subclass of ::rb_cStruct.
120 * @return The list of the names of the fields of `klass`.
121 */
122 VALUE rb_struct_s_members(VALUE klass);
123
124 /**
125 * Queries the list of the names of the fields of the class of the given struct
126 * object. This is almost the same as calling rb_struct_s_members() over the
127 * class of the receiver.
128 *
129 * @internal
130 *
131 * "Almost"? What exactly is the difference?
132 *
133 * @endinternal
134 *
135 * @param[in] self An instance of a subclass of ::rb_cStruct.
136 * @return The list of the names of the fields.
137 */
138 VALUE rb_struct_members(VALUE self);
139
140 /**
141 * Allocates an instance of the given class. This consequential name is of
142 * course because rb_struct_alloc() not only allocates but also initialises an
143 * instance. The API design is broken.
144 *
145 * @param[in] klass A subclass of ::rb_cStruct.
146 * @return An allocated instance of `klass`, not initialised.
147 */
148 VALUE rb_struct_alloc_noinit(VALUE klass);
149
150 /**
151 * Identical to rb_struct_define(), except it does not define accessor methods.
152 * You have to define them yourself. Forget about the allocator function
153 * parameter; it is for internal use only. Extension libraries are unable to
154 * properly allocate a ruby struct, because `RStruct` is opaque.
155 *
156 * @internal
157 *
158 * Several flags must be set up properly for ::RUBY_T_STRUCT objects, which are
159 * also missing for extension libraries.
160 *
161 * @endinternal
162 *
163 * @param[in] name Name of the class.
164 * @param[in] super Superclass of the defining class.
165 * @param[in] func Must be 0 for extension libraries.
166 * @param[in] ... Arbitrary number of `const char*`, terminated by
167 * NULL. Each of which are the name of fields.
168 * @exception rb_eNameError `name` is not a constant name.
169 * @exception rb_eTypeError `name` is already taken.
170 * @exception rb_eArgError Duplicated field name.
171 * @return The defined class.
172 * @post Global toplevel constant `name` is defined.
173 * @note `name` is allowed to be a null pointer. This function creates
174 * an anonymous struct class then.
175 */
176 VALUE rb_struct_define_without_accessor(const char *name, VALUE super, rb_alloc_func_t func, ...);
177
178 RBIMPL_ATTR_NONNULL((2))
179 /**
180 * Identical to rb_struct_define_without_accessor(), except it defines the
181 * class under the specified namespace instead of global toplevel. It can also
182 * be seen as a routine identical to rb_struct_define_under(), except it does
183 * not define accessor methods.
184 *
185 * @param[out] outer Namespace that the defining class shall reside.
186 * @param[in] class_name Name of the class.
187 * @param[in] super Superclass of the defining class.
188 * @param[in] alloc Must be 0 for extension libraries.
189 * @param[in] ... Arbitrary number of `const char*`, terminated by
190 * NULL. Each of which are the name of fields.
191 * @exception rb_eNameError `class_name` is not a constant name.
192 * @exception rb_eTypeError `class_name` is already taken.
193 * @exception rb_eArgError Duplicated field name.
194 * @return The defined class.
195 * @post `class_name` is a constant under `outer`.
196 * @note In contrast to rb_struct_define_without_accessor(), it doesn't
197 * make any sense to pass a null name.
198 */
199 VALUE rb_struct_define_without_accessor_under(VALUE outer, const char *class_name, VALUE super, rb_alloc_func_t alloc, ...);
200
201 /**
202 * Defines an anonymous data class.
203 *
204 * @endinternal
205 *
206 * @param[in] super Superclass of the defining class. Must be a
207 * descendant of ::rb_cData, or 0 as ::rb_cData.
208 * @param[in] ... Arbitrary number of `const char*`, terminated by
209 * NULL. Each of which are the name of fields.
210 * @exception rb_eArgError Duplicated field name.
211 * @return The defined class.
212 */
213 VALUE rb_data_define(VALUE super, ...);
214
215 RBIMPL_SYMBOL_EXPORT_END()
216
217 #endif /* RBIMPL_INTERN_STRUCT_H */