summaryrefslogtreecommitdiff
path: root/internal/class.h
diff options
context:
space:
mode:
authorJean Boussier <byroot@ruby-lang.org>2023-02-15 10:42:52 +0100
committerJean Boussier <jean.boussier@gmail.com>2023-02-15 15:24:22 +0100
commit7413079dae81e46aefc948cd8872497567945791 (patch)
tree31c1118b1bd5d751940571505ff5db1058d612e4 /internal/class.h
parentbac4d2eefa079168968841079727fe2289b6ab6e (diff)
Encapsulate RCLASS_ATTACHED_OBJECT
Right now the attached object is stored as an instance variable and all the call sites that either get or set it have to know how it's stored. It's preferable to hide this implementation detail behind accessors so that it is easier to change how it's stored.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/7308
Diffstat (limited to 'internal/class.h')
-rw-r--r--internal/class.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/internal/class.h b/internal/class.h
index 02a2e38ffe..b777548174 100644
--- a/internal/class.h
+++ b/internal/class.h
@@ -8,9 +8,11 @@
* file COPYING are met. Consult the file for details.
* @brief Internal header for Class.
*/
+#include "id.h"
#include "id_table.h" /* for struct rb_id_table */
#include "internal/serial.h" /* for rb_serial_t */
#include "internal/static_assert.h"
+#include "internal/variable.h" /* for rb_class_ivar_set */
#include "ruby/internal/stdbool.h" /* for bool */
#include "ruby/intern.h" /* for rb_alloc_func_t */
#include "ruby/ruby.h" /* for struct RBasic */
@@ -99,6 +101,7 @@ STATIC_ASSERT(sizeof_rb_classext_t, sizeof(struct RClass) + sizeof(rb_classext_t
#define RCLASS_SUBCLASSES(c) (RCLASS_EXT(c)->subclasses)
#define RCLASS_SUPERCLASS_DEPTH(c) (RCLASS_EXT(c)->superclass_depth)
#define RCLASS_SUPERCLASSES(c) (RCLASS_EXT(c)->superclasses)
+#define RCLASS_ATTACHED_OBJECT(c) (rb_attr_get(c, id__attached__))
#define RICLASS_IS_ORIGIN FL_USER0
#define RCLASS_CLONED FL_USER1
@@ -197,4 +200,14 @@ RCLASS_SET_CLASSPATH(VALUE klass, VALUE classpath, bool permanent)
RCLASS_EXT(klass)->permanent_classpath = permanent;
}
+static inline VALUE
+RCLASS_SET_ATTACHED_OBJECT(VALUE klass, VALUE attached_object)
+{
+ assert(BUILTIN_TYPE(klass) == T_CLASS);
+ assert(FL_TEST(klass, FL_SINGLETON));
+
+ rb_class_ivar_set(klass, id__attached__, attached_object);
+ return attached_object;
+}
+
#endif /* INTERNAL_CLASS_H */