88
99static const rb_data_type_t semian_simple_integer_type ;
1010
11- static int * get_value (VALUE self ) {
12- const char * name = to_s (rb_iv_get (self , "@name" ));
13- if (name == NULL ) {
14- rb_raise (rb_eArgError , "could not get object name" );
15- }
16-
17- key_t key = generate_key (name );
11+ static semian_simple_integer_shared_t * get_value (VALUE self ) {
12+ semian_simple_integer_t * res ;
13+ TypedData_Get_Struct (self , semian_simple_integer_t , & semian_simple_integer_type , res );
1814
1915 const int permissions = 0664 ;
20- int shmid = shmget (key , sizeof (int ), IPC_CREAT | permissions );
16+ int shmid = shmget (res -> key , sizeof (semian_simple_integer_shared_t ), IPC_CREAT | permissions );
2117 if (shmid == -1 ) {
2218 rb_raise (rb_eArgError , "could not create shared memory (%s)" , strerror (errno ));
2319 }
@@ -27,7 +23,7 @@ static int* get_value(VALUE self) {
2723 rb_raise (rb_eArgError , "could not get shared memory (%s)" , strerror (errno ));
2824 }
2925
30- return (int * )val ;
26+ return (semian_simple_integer_shared_t * )val ;
3127}
3228
3329void Init_SimpleInteger ()
@@ -37,8 +33,8 @@ void Init_SimpleInteger()
3733 VALUE cSimpleInteger = rb_const_get (cSimple , rb_intern ("Integer" ));
3834
3935 rb_define_alloc_func (cSimpleInteger , semian_simple_integer_alloc );
40- rb_define_method (cSimpleInteger , "initialize_simple_integer" , semian_simple_integer_initialize , 0 );
41- rb_define_method (cSimpleInteger , "increment" , semian_simple_integer_increment , 1 );
36+ rb_define_method (cSimpleInteger , "initialize_simple_integer" , semian_simple_integer_initialize , 1 );
37+ rb_define_method (cSimpleInteger , "increment" , semian_simple_integer_increment , - 1 );
4238 rb_define_method (cSimpleInteger , "reset" , semian_simple_integer_reset , 0 );
4339 rb_define_method (cSimpleInteger , "value" , semian_simple_integer_value_get , 0 );
4440 rb_define_method (cSimpleInteger , "value=" , semian_simple_integer_value_set , 1 );
@@ -52,39 +48,53 @@ VALUE semian_simple_integer_alloc(VALUE klass)
5248}
5349
5450
55- VALUE semian_simple_integer_initialize (VALUE self )
51+ VALUE semian_simple_integer_initialize (VALUE self , VALUE name )
5652{
57- int * data = get_value (self );
58- * data = 0 ;
53+ semian_simple_integer_t * res ;
54+ TypedData_Get_Struct (self , semian_simple_integer_t , & semian_simple_integer_type , res );
55+ res -> key = generate_key (to_s (name ));
56+
57+ semian_simple_integer_shared_t * data = get_value (self );
58+ data -> val = 0 ;
5959
6060 return self ;
6161}
6262
63- VALUE semian_simple_integer_increment (VALUE self , VALUE val ) {
64- int * data = get_value (self );
65- * data += rb_num2int (val );
63+ VALUE semian_simple_integer_increment (int argc , VALUE * argv , VALUE self ) {
64+ // This is definitely the worst API ever.
65+ // https://silverhammermba.github.io/emberb/c/#parsing-arguments
66+ VALUE val ;
67+ rb_scan_args (argc , argv , "01" , & val );
68+
69+ semian_simple_integer_shared_t * data = get_value (self );
70+
71+ if (NIL_P (val )) {
72+ data -> val += 1 ;
73+ } else {
74+ data -> val += RB_NUM2INT (val );
75+ }
6676
67- return RB_INT2NUM (* data );
77+ return RB_INT2NUM (data -> val );
6878}
6979
7080VALUE semian_simple_integer_reset (VALUE self ) {
71- int * data = get_value (self );
72- * data = 0 ;
81+ semian_simple_integer_shared_t * data = get_value (self );
82+ data -> val = 0 ;
7383
74- return RB_INT2NUM (* data );
84+ return RB_INT2NUM (data -> val );
7585}
7686
7787VALUE semian_simple_integer_value_get (VALUE self ) {
78- int * data = get_value (self );
79- return RB_INT2NUM (* data );
88+ semian_simple_integer_shared_t * data = get_value (self );
89+ return RB_INT2NUM (data -> val );
8090}
8191
8292VALUE semian_simple_integer_value_set (VALUE self , VALUE val ) {
83- int * data = get_value (self );
93+ semian_simple_integer_shared_t * data = get_value (self );
8494
8595 // TODO(michaelkipper): Check for respond_to?(:to_i) before calling.
8696 VALUE to_i = rb_funcall (val , rb_intern ("to_i" ), 0 );
87- * data = RB_NUM2INT (to_i );
97+ data -> val = RB_NUM2INT (to_i );
8898
89- return RB_INT2NUM (* data );
99+ return RB_INT2NUM (data -> val );
90100}
0 commit comments