File: create_objs.rb

package info (click to toggle)
ngraph-gtk 6.09.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,044 kB
  • sloc: ansic: 114,209; sh: 5,844; ruby: 1,824; makefile: 590; cpp: 373; xml: 216; sed: 39; awk: 11
file content (891 lines) | stat: -rw-r--r-- 23,364 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
#! /usr/bin/ruby

ADD_QUESTION_MARK_TO_BOOL_FIELD = false

class Field
  attr_reader :name, :func, :argc

  def initialize(name, func, argc)
    @name = name
    @func = func
    @argc = argc
  end
end

class EnumItem
  attr_reader :const, :name, :num
  def initialize(enum, i)
    @const = enum.upcase.gsub(".", "_")
    @num = i
    @name = enum
  end
end

class Enum
  attr_reader :module, :enum, :max

  def initialize(name, enum)
    @module = name.capitalize
    i = 0;
    @enum = []
    enum.each_with_index {|e, i|
      @enum.push(EnumItem.new(e, i))
    }
    @max = enum.size - 1
  end
end

class NgraphObj
  attr_reader :name, :fields
  attr_accessor :abstruct, :singleton
  SINGLETON_METHOD = [
                      ["new", "new", 0],
                      ["[]", "get", 1],
                      ["del", "del", 1],
                      ["each", "each", 0],
                      ["size", "size", 0],
                      ["current", "current", 0],
                     ]

  SINGLETON_METHOD2 = [
                       ["move_up", "move_up", 1],
                       ["move_down", "move_down", 1],
                       ["move_top", "move_top", 1],
                       ["move_last", "move_last", 1],
                       ["exchange", "exchange", 2],
                       ["copy", "copy", 2],
                      ]

  SINGLETON_METHOD3 = [
                       ["exist?", "exist", 0],
                       ["get_field_args", "field_args", 1],
                       ["get_field_type", "field_type", 1],
                       ["get_field_permission", "field_permission", 1],
                       ["derive", "derive", 1],
                      ]

  FUNC_FIELD_COMMON = <<-EOF
	  struct ngraph_instance *inst;

	  inst = check_id(self);
	  if (inst == NULL) {
	    return Qnil;
	  }
	EOF

  FUNC_FIELD_VAL = "  ngraph_returned_value rval;\n"

  FUNC_FIELD_ARY = <<-EOF
	  ngraph_returned_value rval;
	  VALUE ary;
	  int i;
	EOF

  def initialize(name, aliases, cfile, version, parent)
    @name = name
    @fields = []
    @methods = []
    @enum = {}
    @cfile = cfile
    @version = version
    @parent = (parent == "(null)") ? nil : parent
    @abstruct = false
    @singleton = false
    @func = ""
    @alias = if (aliases)
               aliases.split(":").map {|s| s.capitalize}
             else
               []
             end
  end

  def put_indented_str(s)
    @cfile.puts(s.gsub(/^	/, ""))
  end

  def add_singleton_method_func(method, func, argc)
    @cfile.puts("static VALUE")
    @cfile.print("#{@name}_#{func}(VALUE klass")
    if (argc > 0)
      @cfile.print(", ")
      argc.times { |i|
        @cfile.print("VALUE arg#{i}")
        if (i == argc - 1)
          @cfile.puts(")\n{")
        else
          @cfile.print(", ")
        end
      }
    else
      @cfile.puts(")\n{")
    end
    @cfile.print("  return obj_#{func}(klass, ")
    argc.times { |i|
      @cfile.print("arg#{i}, ")
    }
    @cfile.puts(%Q!"#{@name}");\n}!);
  end

  def create_obj
    unless (@abstruct)
      put_indented_str(@func)
      SINGLETON_METHOD.each { |method, func, argc|
        add_singleton_method_func(method, func, argc)
      }
      unless (@singleton)
        SINGLETON_METHOD2.each { |method, func, argc|
          add_singleton_method_func(method, func, argc)
        }
      end
    end
    SINGLETON_METHOD3.each { |method, func, argc|
      add_singleton_method_func(method, func, argc)
    }

    @enum.each { |key, val|
      put_indented_str <<-EOF
	static VALUE
	#{@name}_get_#{val.module}(VALUE klass, VALUE arg)
	{
	  int i;
	  i = VAL2INT(arg);
	  switch (i) {
	EOF
      val.enum.each { |enum|
        @cfile.puts("    case #{enum.num}:")
        @cfile.puts(%Q!      return rb_str_new2("#{enum.name}");!)
      }
      put_indented_str <<-EOF
	  }
	  return Qnil;
	}
	EOF
    }
    put_indented_str <<-EOF
	static void
	create_#{@name}_object(VALUE ngraph_module, VALUE ngraph_class)
	{
	  VALUE obj;
	  VALUE fields;
	#{(@enum.size > 0) ? "  VALUE module;" : ""}
	  obj = rb_define_class_under(ngraph_module, "#{@name.capitalize}", ngraph_class);
	EOF
    @alias.each {|name|
      @cfile.puts(%Q!  rb_define_const(ngraph_module, "#{name}", obj);!)
    }
    unless (@abstruct)
      SINGLETON_METHOD.each { |method, func, argc|
        @cfile.puts(%Q!  rb_define_singleton_method(obj, "#{method}", #{@name}_#{func}, #{argc});!)
      }
      unless (@singleton)
        SINGLETON_METHOD2.each { |method, func, argc|
          @cfile.puts(%Q!  rb_define_singleton_method(obj, "#{method}", #{@name}_#{func}, #{argc});!)
        }
      end
    end
    SINGLETON_METHOD3.each { |method, func, argc|
      @cfile.puts(%Q!  rb_define_singleton_method(obj, "#{method}", #{@name}_#{func}, #{argc});!)
    }
    @cfile.puts("  setup_obj_common(obj);") unless (@abstruct)
    put_indented_str <<-EOF
	  add_obj_const(obj, "#{@name}");

	  fields = rb_ary_new2(#{@fields.size});
	  rb_define_const(obj, "FIELDS", fields);
	EOF

    @fields.each_with_index { |field, i|
      @cfile.puts(%Q!  store_field_names(fields, "#{field}");!)
    }
    @cfile.puts("  OBJ_FREEZE(fields);")
    unless (@abstruct)
      @methods.each { |field|
        @cfile.puts(%Q!  rb_define_method(obj, "#{field.name}", #{field.func}, #{field.argc});!)
      }
    end
    @enum.each { |key, val|
      @cfile.puts(%Q!  module = rb_define_module_under(obj, "#{val.module}");!)
      @cfile.puts(%Q!  rb_extend_object(module, rb_mEnumerable);!)
      @cfile.puts(%Q!  rb_define_singleton_method(module, "[]", #{@name}_get_#{val.module}, 1);!)
      val.enum.each { |enum|
        @cfile.puts(%Q!  rb_define_const(module, "#{enum.const}", INT2FIX(#{enum.num}));!)
      }
    }
    @cfile.puts("}")
  end

  def create_rw_field_func(func, rw, type, field)
    if (rw == "put")
      parm = ", VALUE arg"
      arg = ", arg"
    else
      arg = ""
      parm = ""
    end
    @func += <<-EOF
	static VALUE
	#{func}(VALUE self#{parm})
	{
	  return inst_#{rw}_#{type}(self#{arg}, "#{field}");
	}
	EOF
  end

  def create_put_obj_func(func, field)
    create_rw_field_func(func, "put", "obj", field)
  end

  def create_get_obj_func(func, field)
    create_rw_field_func(func, "get", "obj", field)
  end

  def create_put_str_func(func, field)
    create_rw_field_func(func, "put", "str", field)
  end

  def create_get_str_func(func, field)
    create_rw_field_func(func, "get", "str", field)
  end

  def create_put_int_func(func, field)
    create_rw_field_func(func, "put", "int", field)
  end

  def create_get_int_func(func, field)
    create_rw_field_func(func, "get", "int", field)
  end

  def create_put_double_func(func, field)
    create_rw_field_func(func, "put", "double", field)
  end

  def create_get_double_func(func, field)
    create_rw_field_func(func, "get", "double", field)
  end

  def create_put_bool_func(func, field)
    create_rw_field_func(func, "put", "bool", field)
  end

  def create_get_bool_func(func, field)
    create_rw_field_func(func, "get", "bool", field)
  end

  def create_put_enum_func(func, field, enum)
    @func += <<-EOF
	static VALUE
	#{func}(VALUE self, VALUE arg)
	{
	  return inst_put_enum(self, arg, "#{field}", #{enum.max});
	}
	EOF
  end

  def add_enum(field, ary)
    return @enum[field] if (@enum[field])
    enum = Enum.new(field, ary)
    @enum[field] = enum
    enum
  end

  def create_get_enum_func(func, field)
    create_rw_field_func(func, "get", "int", field)
  end

  def create_put_int_array_func(func, field)
    create_rw_field_func(func, "put", "iarray", field)
  end

  def create_get_int_array_func(func, field)
    create_rw_field_func(func, "get", "iarray", field)
  end

  def create_put_double_array_func(func, field)
    create_rw_field_func(func, "put", "darray", field)
  end

  def create_get_double_array_func(func, field)
    create_rw_field_func(func, "get", "darray", field)
  end

  def create_put_str_array_func(func, field)
    create_rw_field_func(func, "put", "sarray", field)
  end

  def create_get_str_array_func(func, field)
    create_rw_field_func(func, "get", "sarray", field)
  end

  def field2method(field)
    method = field.gsub("-", "_").gsub("%", "prm")
    method = "value" if (method == "@")
    method.downcase
  end

  def add_put_method(ary)
    field = ary[1]
    type = ary[2]
    method = field2method(field)
    func = "#{@name}_field_put_#{method}"
    case type
    when "bool"
      create_put_bool_func(func, field)
    when "int"
      create_put_int_func(func, field)
    when "double"
      create_put_double_func(func, field)
    when "obj"
      create_put_obj_func(func, field)
    when "char*"
      create_put_str_func(func, field)
    when "enum("
      n = ary.size - 4
      enum = add_enum(field, ary[3, n])
      create_put_enum_func(func, field, enum)
    when "int[]"
      create_put_int_array_func(func, field)
    when "double[]"
      create_put_double_array_func(func, field)
    when "char*[]"
      create_put_str_array_func(func, field)
    end
    field = Field.new("#{method}=", func, 1)
    @methods.push(field)
  end

  def add_get_method(ary)
    field = ary[1]
    type = ary[2]
    method = field2method(field)
    func = "#{@name}_field_get_#{method}"
    case type
    when "bool"
      create_get_bool_func(func, field)
      method += "?" if (ADD_QUESTION_MARK_TO_BOOL_FIELD)
    when "int"
      create_get_int_func(func, field)
    when "double"
      create_get_double_func(func, field)
    when "obj"
      create_get_obj_func(func, field)
    when "char*"
      create_get_str_func(func, field)
    when "enum("
      n = ary.size - 4
      add_enum(field, ary[3, n])
      create_get_enum_func(func, field)
    when "int[]"
      create_get_int_array_func(func, field)
    when "double[]"
      create_get_double_array_func(func, field)
    when "char*[]"
      create_get_str_array_func(func, field)
    end
    field = Field.new("#{method}", func, 0)
    @methods.push(field)
  end

  def create_void_exe_func(func, field)
    @func += <<-EOF
	static VALUE
	#{func}(VALUE self)
	{
	  return inst_exe_void_func(self, "#{field}");
	}
	EOF
  end

  def check_args(ary)
    n = ary.size - 4
    case (n)
    when 0
      [-2, []]
    when 1
      if (ary[3][-1] == "]")
        [-2, ary[3, n]]
      elsif (ary[3] == "void")
        [0, []]
      else
        [-1, ary[3, n]]
      end
    else
      [-1, ary[3, n]]
    end
  end

  def create_func_type(func, args)
    @func += "static VALUE\n#{func}(int argc, VALUE *argv, VALUE self)\n{\n"
    @func += "  VALUE arg[#{args.size}];\n"
    @func += "  VALUE tmpstr;\n" if (args.find {|s| s[-1] == "]"})
    @func += "  ngraph_arg *carg;\n"
  end

  def create_arguments(field, args)
    array = false
    @func += (%Q!  rb_scan_args(argc, argv, "0#{args.size}", !)
    args.size.times { |i|
      @func += ("arg + #{i}")
      if (i == args.size - 1)
        @func += (");\n")
      else
        @func += (", ")
      end
    }
    @func += ("  carg = alloca(sizeof(*carg) + sizeof(ngraph_value) * #{args.size});\n")
    @func += (%Q!  if (carg == NULL) {\n    rb_raise(rb_eSysStackError, "%s: cannot allocate enough memory.", rb_obj_classname(self));\n  }\n!)
    @func += ("  carg->num = #{args.size};\n")
    args.each_with_index { |arg, i|
      case arg
      when "int"
        @func += ("  carg->ary[#{i}].i = VAL2INT(arg[#{i}]);\n")
      when "double"
        @func += ("  carg->ary[#{i}].d = VAL2DBL(arg[#{i}]);\n")
      when "char*"
        @func += ("  carg->ary[#{i}].str = VAL2STR(arg[#{i}]);\n")
      when "bool"
        @func += ("  carg->ary[#{i}].i = RTEST(arg[#{i}]) ? 1 : 0;\n")
      when "int[]"
        @func += (%Q!  carg->ary[#{i}].ary = allocate_iarray(self, &tmpstr, arg[#{i}], "#{field}");\n!)
        array = true
      when "double[]"
        @func += (%Q!  carg->ary[#{i}].ary = allocate_darray(self, &tmpstr, arg[#{i}], "#{field}");\n!)
        array = true
      when "char*[]"
        @func += (%Q!  carg->ary[#{i}].ary = allocate_sarray(self, &tmpstr, arg[#{i}], "#{field}");\n!)
        array = true
      end
    }
    array
  end

  def create_finalize_arguments(args)
    args.each_with_index { |arg, i|
      case arg
      when "int[]", "double[]", "char*[]"
        @func += ("  if (carg->ary[#{i}].ary) {\n    rb_free_tmp_buffer(&tmpstr);\n  }\n")
      end
    }
  end

  def add_arg_array(arg, field)
    unless (arg)
      @func += %Q!  carg = allocate_sarray(self, &tmpstr, argv, "#{field}");\n!
      return
    end

    case (arg)
    when "int[]"
      @func += %Q!  carg.ary[0].ary = allocate_iarray(self, &tmpstr, argv, "#{field}");\n!
    when "double[]"
      @func += %Q!  carg.ary[0].ary = allocate_darray(self, &tmpstr, argv, "#{field}");\n!
    when "char*[]"
      @func += %Q!  carg.ary[0].ary = allocate_sarray(self, &tmpstr, argv, "#{field}");\n!
    else
      @func += %Q!  carg.ary[0].ary = allocate_sarray(self, &tmpstr, argv, "#{field}");\n!
    end
    @func += "  carg.num = 1;\n"
  end

  def create_val_func_with_argv(func, field, args, comv)
    varg = (args.size == 0)
    @func += ("static VALUE\n#{func}(VALUE self, VALUE argv)\n{\n")
    @func += ("  VALUE tmpstr;\n")
    @func += (FUNC_FIELD_VAL)
    @func += "  ngraph_arg #{(varg) ? '*' : ''}carg;\n"
    @func += (FUNC_FIELD_COMMON)
    add_arg_array(args[0], field)
    @func += <<-EOF
	  inst->rcode = ngraph_object_get(inst->obj, "#{field}", inst->id, #{varg ? '' : '&'}carg, &rval);
	  rb_free_tmp_buffer(&tmpstr);
	  if (inst->rcode < 0) {
	    return Qnil;
	  }

	  return #{comv};
	}
	EOF
  end

  def create_void_func_with_argv(func, field, args)
    varg = (args.size == 0)
    @func += <<-EOF
	static VALUE
	#{func}(VALUE self, VALUE argv)
	{
	  struct ngraph_instance *inst;
	  ngraph_arg #{(varg) ? '*' : ''}carg;
	  VALUE tmpstr;

	  inst = check_id(self);
	  if (inst == NULL) {
	    return Qnil;
	  }
	EOF
    add_arg_array(args[0], field)
    @func += <<-EOF
	  inst->rcode = ngraph_object_exe(inst->obj, "#{field}", inst->id, #{varg ? '' : '&'}carg);
	  rb_free_tmp_buffer(&tmpstr);
	  if (inst->rcode < 0) {
	    return Qnil;
	  }

	  return self;
	}
	EOF
  end

  def create_array_func_with_argv(func, field, args, comv)
    varg = (args.size == 0)
    @func += ("static VALUE\n#{func}(VALUE self, VALUE argv)\n{")
    @func += ("  VALUE tmpstr;\n")
    @func += (FUNC_FIELD_ARY)
    @func += "  ngraph_arg #{(varg) ? '*' : ''}carg;\n"
    @func += (FUNC_FIELD_COMMON)
    add_arg_array(args[0], field)
    @func += <<-EOF
	  inst->rcode = ngraph_object_get(inst->obj, "#{field}", inst->id, #{varg ? '' : '&'}carg, &rval);
	  rb_free_tmp_buffer(&tmpstr);
	  if (inst->rcode < 0) {
	    return Qnil;
	  }

	  ary = rb_ary_new2(rval.ary.num);
	  for (i = 0; i < rval.ary.num; i++) {
	    rb_ary_store(ary, i, #{comv});
	  }

	  return ary;
	}
	EOF
  end

  def create_val_func_with_args(func, field, args, conv)
    create_func_type(func, args)
    @func += (FUNC_FIELD_VAL)
    @func += (FUNC_FIELD_COMMON)
    array = create_arguments(field, args)

    @func += (%Q!  inst->rcode = ngraph_object_get(inst->obj, "#{field}", inst->id, carg, &rval);\n!)
    @func += (%Q!  rb_free_tmp_buffer(&tmpstr);\n!) if (array)
    @func += <<-EOF
	  if (inst->rcode < 0) {
	    return Qnil;
	  }

	  return #{conv};
	}
	EOF
  end

  def create_void_func_with_args(func, field, args)
    create_func_type(func, args)
    @func += (FUNC_FIELD_COMMON)
    array = create_arguments(field, args)

    @func += (%Q!  inst->rcode = ngraph_object_exe(inst->obj, "#{field}", inst->id, carg);\n!)
    @func += (%Q!  rb_free_tmp_buffer(&tmpstr);\n!) if (array)
    @func += <<-EOF
	  if (inst->rcode < 0) {
	    return Qnil;
	  }

	  return self;
	}
	EOF
  end

  def create_array_func_with_args(func, field, args, comv)
    create_func_type(func, args)
    @func += (FUNC_FIELD_ARY)
    @func += (FUNC_FIELD_COMMON)
    array = create_arguments(field, args)

    @func += (%Q!  inst->rcode = ngraph_object_get(inst->obj, "#{field}", inst->id, carg, &rval);\n!)
    @func += (%Q!  rb_free_tmp_buffer(&tmpstr);\n!) if (array)
    @func += <<-EOF
	  if (inst->rcode < 0) {
	    return Qnil;
	  }

	  ary = rb_ary_new2(rval.ary.num);
	  for (i = 0; i < rval.ary.num; i++) {
	    rb_ary_store(ary, i, #{comv});
	  }

	  return ary;
	}
	EOF
  end

  def create_bool_func_with_args(func, field, args)
    create_val_func_with_args(func, field, args, 'rval.i ? Qtrue : Qfalse')
  end

  def create_int_func_with_args(func, field, args)
    create_val_func_with_args(func, field, args, 'INT2NUM(rval.i)')
  end

  def create_double_func_with_args(func, field, args)
    create_val_func_with_args(func, field, args, 'rb_float_new(rval.d)')
  end

  def create_str_func_with_args(func, field, args)
    create_val_func_with_args(func, field, args, 'utf8_str_new(rval.str ? rval.str : "")')
  end

  def create_bool_func_with_argv(func, field, args)
    create_val_func_with_argv(func, field, args, 'rval.i ? Qtrue : Qfalse')
  end

  def create_int_func_with_argv(func, field, args)
    create_val_func_with_argv(func, field, args, 'INT2NUM(rval.i)')
  end

  def create_double_func_with_argv(func, field, args)
    create_val_func_with_argv(func, field, args, 'rb_float_new(rval.d)')
  end

  def create_str_func_with_argv(func, field, args)
    create_val_func_with_argv(func, field, args, 'utf8_str_new(rval.str ? rval.str : "")')
  end

  def add_bool_func(func, field, argc, args)
    case (argc)
    when 0
      create_get_bool_func(func, field)
    when -2
      create_bool_func_with_argv(func, field, args)
    else
      create_bool_func_with_args(func, field, args)
    end
  end

  def add_int_func(func, field, argc, args)
    case (argc)
    when 0
      create_get_int_func(func, field)
    when -2
      create_int_func_with_argv(func, field, args)
    else
      create_int_func_with_args(func, field, args)
    end
  end

  def add_double_func(func, field, argc, args)
    case (argc)
    when 0
      create_get_double_func(func, field)
    when -2
      create_double_func_with_argv(func, field, args)
    else
      create_double_func_with_args(func, field, args)
    end
  end

  def add_str_func(func, field, argc, args)
    case (argc)
    when 0
      create_get_str_func(func, field)
    when -2
      create_str_func_with_argv(func, field, args)
    else
      create_str_func_with_args(func, field, args)
    end
  end

  def add_int_array_func(func, field, argc, args)
    case (argc)
    when 0
      create_get_int_array_func(func, field)
    when -2
      create_array_func_with_argv(func, field, args, 'INT2NUM(rval.ary.data.ia[i])')
    else
      create_array_func_with_args(func, field, args, 'INT2NUM(rval.ary.data.ia[i])')
    end
  end

  def add_double_array_func(func, field, argc, args)
    case (argc)
    when 0
      create_get_double_array_func(func, field)
    when -2
      create_array_func_with_argv(func, field, args, 'rb_float_new(rval.ary.data.da[i])')
    else
      create_array_func_with_args(func, field, args, 'rb_float_new(rval.ary.data.da[i])')
    end
  end

  def add_str_array_func(func, field, argc, args)
    case (argc)
    when 0
      create_get_str_array_func(func, field)
    when -2
      create_array_func_with_argv(func, field, args, 'utf8_str_new(rval.ary.data.sa[i] ? rval.ary.data.sa[i] : "")')
    else
      create_array_func_with_args(func, field, args, 'utf8_str_new(rval.ary.data.sa[i] ? rval.ary.data.sa[i] : "")')
    end
  end

  def add_void_func(func, field, argc, args)
    case (argc)
    when 0
      create_void_exe_func(func, field)
    when -2
      create_void_func_with_argv(func, field, args)
    else
      create_void_func_with_args(func, field, args)
    end
  end

  def add_func_obj(type, func, field, args)
    rtype = case type
            when "bool("
              "NBFUNC"
            when "int("
              "NIFUNC"
            when "char*("
              "NSFUNC"
            when "int[]("
              "NIAFUNC"
            when "double[]("
              "NDAFUNC"
            when "char*[]("
              "NSAFUNC"
            when "void("
              "NVFUNC"
            else
              "NVFUNC"
            end
    @func += <<-EOF
	static VALUE
	#{func}(VALUE self, VALUE argv)
	{
	  return obj_func_obj(self, argv, "#{field}", #{rtype});
	}
	EOF
  end

  def add_func_method(ary)
    field = ary[1]
    type = ary[2]
    method = field2method(field)
    func = "#{@name}_field_#{method}"
    argc, args = check_args(ary)
    if (ary[3] == "obj")
      argc = -2;
      add_func_obj(type, func, field, args)
    else
      case type
      when "bool("
        add_bool_func(func, field, argc, args)
      when "int("
        add_int_func(func, field, argc, args)
      when "double("
        add_double_func(func, field, argc, args)
      when "char*("
        add_str_func(func, field, argc, args)
      when "int[]("
        add_int_array_func(func, field, argc, args)
      when "double[]("
        add_double_array_func(func, field, argc, args)
      when "char*[]("
        add_str_array_func(func, field, argc, args)
      when "void("
        add_void_func(func, field, argc, args)
      end
    end
    if (ADD_QUESTION_MARK_TO_BOOL_FIELD)
      method += "?" if (type == "bool(")
    end
    field = Field.new("#{method}", func, argc)
    @methods.push(field)
  end

  def add_method(ary)
    case ary[0]
    when "-w-"
      add_put_method(ary)
    when "r--"
      add_get_method(ary)
    when "r-x"
      add_func_method(ary)
    when "rw-"
      add_put_method(ary)
      add_get_method(ary)
    end
  end
end

def create_obj_funcs(file, cfile, name, aliases, version, parent)
  obj = NgraphObj.new(name, aliases, cfile, version, parent)
  singleton = true
  while (true)
    ary = file.gets.chomp.split
    singleton = false if (ary[1] == "next")
    case ary[0]
    when "#"
      break
    when "---"
      obj.abstruct = true if (ary[1] == "init")
    when "--x"
      # can't execute
    when "r--"
      obj.add_method(ary)
    when "-w-"
      #      obj.add_method(ary)
      # only used for backword compatibility.
    when "r-x"
      obj.add_method(ary)
    when "rw-"
      obj.add_method(ary)
    else
      next
    end
    obj.fields.push(ary[1])
  end
  obj.singleton = singleton
  obj.create_obj
  obj
end

objs = []
File.open(ARGV[1], "w") { |cfile|
  File.open(ARGV[0], "r") { |file|
    while (l = file.gets)
      str_ary = l.chomp.split
      case (str_ary[0])
      when "object:"
        name = str_ary[1]
        next
      when "alias:"
        aliases = str_ary[1]
        next
      when "version:"
        version = str_ary[1]
        next
      when "parent:"
        parent = str_ary[1]
      else
        next
      end
      obj = create_obj_funcs(file, cfile, name, aliases, version, parent)
      objs.push(obj)
    end
  }
  str = <<-EOF
	static void
	create_ngraph_classes(VALUE ngraph_module, VALUE ngraph_class)
	{
	  VALUE objs;

	  objs = rb_ary_new2(#{objs.size});
	  rb_define_const(ngraph_module, "OBJECTS", objs);
	EOF
  cfile.puts(str.gsub(/^	/, ""))
  objs.each_with_index {|obj, i|
    cfile.puts("  create_#{obj.name}_object(ngraph_module, ngraph_class);\n")
    cfile.puts(%Q!  rb_ary_store(objs, #{i}, ID2SYM(rb_intern("#{obj.name.capitalize}")));\n!)
  }
  cfile.puts("  OBJ_FREEZE(objs);\n")
  cfile.puts("}")
}