cauterize 0.0.1.pre13 → 0.0.1.pre14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +33 -33
  3. data/.rspec +1 -1
  4. data/.travisci.yml +4 -4
  5. data/Gemfile +4 -4
  6. data/LICENSE.txt +21 -21
  7. data/README.md +177 -177
  8. data/Rakefile +54 -54
  9. data/bin/cauterize +61 -61
  10. data/cauterize.gemspec +27 -27
  11. data/example/Cauterize +68 -68
  12. data/example/build.sh +25 -25
  13. data/example/c_example_support/empty_main.c +1 -1
  14. data/example/c_example_support/example_project_config.c +11 -11
  15. data/example/c_example_support/example_project_config.h +15 -15
  16. data/example/ruby_ex.rb +14 -14
  17. data/lib/cauterize.rb +117 -117
  18. data/lib/cauterize/base_type.rb +78 -78
  19. data/lib/cauterize/builders.rb +41 -41
  20. data/lib/cauterize/builders/c/buildable.rb +91 -91
  21. data/lib/cauterize/builders/c/builtin.rb +43 -43
  22. data/lib/cauterize/builders/c/composite.rb +57 -57
  23. data/lib/cauterize/builders/c/enumeration.rb +60 -60
  24. data/lib/cauterize/builders/c/fixed_array.rb +74 -74
  25. data/lib/cauterize/builders/c/group.rb +143 -143
  26. data/lib/cauterize/builders/c/scalar.rb +34 -34
  27. data/lib/cauterize/builders/c/variable_array.rb +98 -98
  28. data/lib/cauterize/builders/cs/buildable.rb +59 -59
  29. data/lib/cauterize/builders/cs/builtin.rb +22 -22
  30. data/lib/cauterize/builders/cs/composite.rb +21 -21
  31. data/lib/cauterize/builders/cs/csarray.rb +32 -32
  32. data/lib/cauterize/builders/cs/enumeration.rb +21 -21
  33. data/lib/cauterize/builders/cs/fixed_array.rb +27 -27
  34. data/lib/cauterize/builders/cs/group.rb +32 -32
  35. data/lib/cauterize/builders/cs/scalar.rb +10 -10
  36. data/lib/cauterize/builders/cs/variable_array.rb +40 -34
  37. data/lib/cauterize/builders/doc/buildable.rb +30 -30
  38. data/lib/cauterize/builders/doc/builtin.rb +21 -21
  39. data/lib/cauterize/builders/doc/composite.rb +23 -23
  40. data/lib/cauterize/builders/doc/enumeration.rb +24 -24
  41. data/lib/cauterize/builders/doc/fixed_array.rb +26 -26
  42. data/lib/cauterize/builders/doc/group.rb +29 -29
  43. data/lib/cauterize/builders/doc/scalar.rb +21 -21
  44. data/lib/cauterize/builders/doc/variable_array.rb +26 -26
  45. data/lib/cauterize/builders/ruby/buildable.rb +11 -11
  46. data/lib/cauterize/builders/ruby/builtin.rb +25 -25
  47. data/lib/cauterize/builders/ruby/composite.rb +26 -26
  48. data/lib/cauterize/builders/ruby/enumeration.rb +27 -27
  49. data/lib/cauterize/builders/ruby/fixed_array.rb +20 -20
  50. data/lib/cauterize/builders/ruby/group.rb +35 -35
  51. data/lib/cauterize/builders/ruby/scalar.rb +19 -19
  52. data/lib/cauterize/builders/ruby/variable_array.rb +22 -22
  53. data/lib/cauterize/builtin.rb +62 -62
  54. data/lib/cauterize/c_builder.rb +103 -103
  55. data/lib/cauterize/cauterize.rb +76 -76
  56. data/lib/cauterize/composite.rb +64 -64
  57. data/lib/cauterize/cs_builder.rb +58 -58
  58. data/lib/cauterize/doc_builder.rb +34 -34
  59. data/lib/cauterize/enumeration.rb +98 -98
  60. data/lib/cauterize/fixed_array.rb +50 -50
  61. data/lib/cauterize/formatter.rb +67 -67
  62. data/lib/cauterize/group.rb +81 -81
  63. data/lib/cauterize/representation.rb +32 -32
  64. data/lib/cauterize/ruby_builder.rb +44 -44
  65. data/lib/cauterize/scalar.rb +53 -53
  66. data/lib/cauterize/snake_case.rb +21 -21
  67. data/lib/cauterize/variable_array.rb +55 -55
  68. data/lib/cauterize/version.rb +3 -3
  69. data/spec/base_type_spec.rb +251 -251
  70. data/spec/builders/c/buildable_spec.rb +25 -25
  71. data/spec/builders/c/builtin_spec.rb +22 -22
  72. data/spec/builders/c/composite_spec.rb +50 -50
  73. data/spec/builders/c/enumeration_spec.rb +95 -95
  74. data/spec/builders/c/fixed_array_spec.rb +36 -36
  75. data/spec/builders/c/group_spec.rb +140 -140
  76. data/spec/builders/c/scalar_spec.rb +26 -26
  77. data/spec/builders/c/variable_array_spec.rb +48 -48
  78. data/spec/builders/cs/buildable_spec.rb +8 -8
  79. data/spec/builders/cs/composite_spec.rb +32 -32
  80. data/spec/builders/cs/enumeration_spec.rb +33 -33
  81. data/spec/builders/cs/fixed_array_spec.rb +41 -41
  82. data/spec/builders/cs/group_spec.rb +56 -56
  83. data/spec/builders/cs/scalar_spec.rb +7 -7
  84. data/spec/builders/cs/variable_array_spec.rb +49 -45
  85. data/spec/builders/doc/buildable_spec.rb +25 -25
  86. data/spec/builders_spec.rb +57 -57
  87. data/spec/builtin_spec.rb +43 -43
  88. data/spec/c_builder_spec.rb +176 -176
  89. data/spec/cauterize_spec.rb +15 -15
  90. data/spec/composite_spec.rb +77 -77
  91. data/spec/cs_builder_spec.rb +115 -115
  92. data/spec/doc_builder_spec.rb +260 -260
  93. data/spec/enumeration_spec.rb +145 -145
  94. data/spec/fixed_array_spec.rb +61 -61
  95. data/spec/group_spec.rb +111 -111
  96. data/spec/ruby_builder_spec.rb +83 -83
  97. data/spec/ruby_generated_spec.rb +735 -735
  98. data/spec/scalar_spec.rb +44 -44
  99. data/spec/spec_helper.rb +122 -122
  100. data/spec/support/shared_examples_for_array_buildables.rb +22 -22
  101. data/spec/support/shared_examples_for_c_buildables.rb +103 -103
  102. data/spec/support/shared_examples_for_sane_c_buildables.rb +22 -22
  103. data/spec/support/shared_examples_for_stubbed_functions.rb +18 -18
  104. data/spec/support/spec_sample_model.rb +74 -74
  105. data/spec/test_main.c +13 -13
  106. data/spec/variable_array_spec.rb +73 -73
  107. data/support/c/src/cauterize.c +75 -75
  108. data/support/c/src/cauterize.h +60 -60
  109. data/support/c/src/cauterize_debug.h +29 -29
  110. data/support/c/src/cauterize_util.h +8 -8
  111. data/support/c/test/greatest.h +536 -536
  112. data/support/c/test/test.c +166 -166
  113. data/support/cs/src/CauterizeCompositeFormatter.cs +0 -0
  114. data/support/cs/src/CauterizeContainerFormatter.cs +0 -0
  115. data/support/cs/src/CauterizeEnumFormatter.cs +0 -0
  116. data/support/cs/src/CauterizeException.cs +15 -15
  117. data/support/cs/src/CauterizeFixedArrayFormatter.cs +2 -2
  118. data/support/cs/src/CauterizeFormatter.cs +0 -0
  119. data/support/cs/src/CauterizeGroupFormatter.cs +0 -0
  120. data/support/cs/src/CauterizeTypeFormatterFactory.cs +0 -0
  121. data/support/cs/src/CauterizeTypes.cs +46 -12
  122. data/support/cs/src/CauterizeVariableArrayFormatter.cs +1 -1
  123. data/support/cs/src/ICauterizeTypeFormatter.cs +0 -0
  124. data/support/cs/src/OrderAttribute.cs +0 -0
  125. data/support/cs/src/PrimitiveSupport.cs +12 -0
  126. data/support/cs/test/CauterizeCompositeFormatterTest.cs +0 -0
  127. data/support/cs/test/CauterizeEnumFormatterTest.cs +0 -0
  128. data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +0 -0
  129. data/support/cs/test/CauterizeFormatterTest.cs +0 -0
  130. data/support/cs/test/CauterizeGroupFormatterTest.cs +0 -0
  131. data/support/cs/test/CauterizeIntegrationTest.cs +0 -0
  132. data/support/cs/test/CauterizePrimitiveFormatterTest.cs +0 -0
  133. data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +0 -0
  134. data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +0 -0
  135. data/support/cs/test/OrderAttributeTest.cs +0 -0
  136. data/support/ruby/src/cauterize_ruby_baseclasses.rb +367 -367
  137. data/support/ruby/src/cauterize_ruby_builtins.rb +148 -148
  138. metadata +20 -20
@@ -1,60 +1,60 @@
1
- module Cauterize
2
- module Builders
3
- module C
4
- class Enumeration < Buildable
5
- def render
6
- "enum #{@blueprint.name.to_s}"
7
- end
8
-
9
- def declare(formatter, sym)
10
- formatter << "#{render} #{sym};"
11
- end
12
-
13
- def preprocessor_defines(formatter)
14
- formatter << "#define #{max_enc_len_cpp_sym} (#{rep_builder.max_enc_len_cpp_sym})"
15
- end
16
-
17
- def packer_defn(formatter)
18
- rep = "enum_representation"
19
- rep_builder.declare(formatter, rep)
20
-
21
- formatter << "#{rep} = (#{rep_builder.render})(*src);"
22
- formatter << "return #{rep_builder.packer_sym}(dst, &#{rep});"
23
- end
24
-
25
- def unpacker_defn(formatter)
26
- rep = "enum_representation"
27
- rep_builder.declare(formatter, rep)
28
-
29
- formatter << "CAUTERIZE_STATUS_T s = #{rep_builder.unpacker_sym}(src, &#{rep});"
30
- formatter << "if (CA_OK != s)"
31
- formatter.braces do
32
- formatter << "return s;"
33
- end
34
-
35
- formatter << "else"
36
- formatter.braces do
37
- formatter << "*dst = (#{render})#{rep};"
38
- formatter << "return CA_OK;"
39
- end
40
- end
41
-
42
- def enum_defn(formatter)
43
- formatter << render
44
- formatter.braces do
45
- @blueprint.values.values.each do |v|
46
- formatter << "#{v.name.to_s.up_snake} = #{v.value},"
47
- end
48
- end
49
- formatter.append(";")
50
- end
51
-
52
- private
53
-
54
- def rep_builder; Builders.get(:c, @blueprint.representation) end
55
- end
56
- end
57
- end
58
- end
59
-
60
- Cauterize::Builders.register(:c, Cauterize::Enumeration, Cauterize::Builders::C::Enumeration)
1
+ module Cauterize
2
+ module Builders
3
+ module C
4
+ class Enumeration < Buildable
5
+ def render
6
+ "enum #{@blueprint.name.to_s}"
7
+ end
8
+
9
+ def declare(formatter, sym)
10
+ formatter << "#{render} #{sym};"
11
+ end
12
+
13
+ def preprocessor_defines(formatter)
14
+ formatter << "#define #{max_enc_len_cpp_sym} (#{rep_builder.max_enc_len_cpp_sym})"
15
+ end
16
+
17
+ def packer_defn(formatter)
18
+ rep = "enum_representation"
19
+ rep_builder.declare(formatter, rep)
20
+
21
+ formatter << "#{rep} = (#{rep_builder.render})(*src);"
22
+ formatter << "return #{rep_builder.packer_sym}(dst, &#{rep});"
23
+ end
24
+
25
+ def unpacker_defn(formatter)
26
+ rep = "enum_representation"
27
+ rep_builder.declare(formatter, rep)
28
+
29
+ formatter << "CAUTERIZE_STATUS_T s = #{rep_builder.unpacker_sym}(src, &#{rep});"
30
+ formatter << "if (CA_OK != s)"
31
+ formatter.braces do
32
+ formatter << "return s;"
33
+ end
34
+
35
+ formatter << "else"
36
+ formatter.braces do
37
+ formatter << "*dst = (#{render})#{rep};"
38
+ formatter << "return CA_OK;"
39
+ end
40
+ end
41
+
42
+ def enum_defn(formatter)
43
+ formatter << render
44
+ formatter.braces do
45
+ @blueprint.values.values.each do |v|
46
+ formatter << "#{v.name.to_s.up_snake} = #{v.value},"
47
+ end
48
+ end
49
+ formatter.append(";")
50
+ end
51
+
52
+ private
53
+
54
+ def rep_builder; Builders.get(:c, @blueprint.representation) end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ Cauterize::Builders.register(:c, Cauterize::Enumeration, Cauterize::Builders::C::Enumeration)
@@ -1,74 +1,74 @@
1
- module Cauterize
2
- module Builders
3
- module C
4
- class FixedArray < Buildable
5
- def render
6
- "struct #{@blueprint.name}"
7
- end
8
-
9
- def declare(formatter, sym)
10
- formatter << "#{render} #{sym};"
11
- end
12
-
13
- def preprocessor_defines(formatter)
14
- formatter << "#define #{length_sym} (#{@blueprint.array_size})"
15
- formatter << "#define #{max_enc_len_cpp_sym} (#{length_sym} * #{ty_bldr.max_enc_len_cpp_sym})"
16
- end
17
-
18
- def packer_defn(formatter)
19
- formatter << "CAUTERIZE_STATUS_T err;"
20
- formatter << "size_t i;"
21
- formatter.blank_line
22
-
23
- # store each used item in the array
24
- formatter << "for (i = 0; i < #{length_sym}; i++)"
25
- formatter.braces do
26
- formatter << "if (CA_OK != (err = #{ty_bldr.packer_sym}(dst, &src->data[i]))) { return err; }"
27
- end
28
- formatter.blank_line
29
-
30
- formatter << "return CA_OK;"
31
- end
32
-
33
- def unpacker_defn(formatter)
34
- formatter << "CAUTERIZE_STATUS_T err;"
35
- formatter << "size_t i;"
36
- formatter.blank_line
37
-
38
- # store each used item in the array
39
- formatter << "for (i = 0; i < #{length_sym}; i++)"
40
- formatter.braces do
41
- formatter << "if (CA_OK != (err = #{ty_bldr.unpacker_sym}(src, &dst->data[i]))) { return err; }"
42
- end
43
- formatter.blank_line
44
-
45
- formatter << "return CA_OK;"
46
- end
47
-
48
- def struct_proto(formatter)
49
- formatter << (render + ";")
50
- end
51
-
52
- def struct_defn(formatter)
53
- formatter << render
54
- formatter.braces do
55
- formatter << "#{ty_bldr.render} data[#{@blueprint.array_size}];"
56
- end
57
- formatter.append(";")
58
- end
59
-
60
- private
61
-
62
- def length_sym
63
- "FIXED_ARRAY_LENGTH_#{@blueprint.name}"
64
- end
65
-
66
- def ty_bldr
67
- @ty_bldr ||= Builders.get(:c, @blueprint.array_type)
68
- end
69
- end
70
- end
71
- end
72
- end
73
-
74
- Cauterize::Builders.register(:c, Cauterize::FixedArray, Cauterize::Builders::C::FixedArray)
1
+ module Cauterize
2
+ module Builders
3
+ module C
4
+ class FixedArray < Buildable
5
+ def render
6
+ "struct #{@blueprint.name}"
7
+ end
8
+
9
+ def declare(formatter, sym)
10
+ formatter << "#{render} #{sym};"
11
+ end
12
+
13
+ def preprocessor_defines(formatter)
14
+ formatter << "#define #{length_sym} (#{@blueprint.array_size})"
15
+ formatter << "#define #{max_enc_len_cpp_sym} (#{length_sym} * #{ty_bldr.max_enc_len_cpp_sym})"
16
+ end
17
+
18
+ def packer_defn(formatter)
19
+ formatter << "CAUTERIZE_STATUS_T err;"
20
+ formatter << "size_t i;"
21
+ formatter.blank_line
22
+
23
+ # store each used item in the array
24
+ formatter << "for (i = 0; i < #{length_sym}; i++)"
25
+ formatter.braces do
26
+ formatter << "if (CA_OK != (err = #{ty_bldr.packer_sym}(dst, &src->data[i]))) { return err; }"
27
+ end
28
+ formatter.blank_line
29
+
30
+ formatter << "return CA_OK;"
31
+ end
32
+
33
+ def unpacker_defn(formatter)
34
+ formatter << "CAUTERIZE_STATUS_T err;"
35
+ formatter << "size_t i;"
36
+ formatter.blank_line
37
+
38
+ # store each used item in the array
39
+ formatter << "for (i = 0; i < #{length_sym}; i++)"
40
+ formatter.braces do
41
+ formatter << "if (CA_OK != (err = #{ty_bldr.unpacker_sym}(src, &dst->data[i]))) { return err; }"
42
+ end
43
+ formatter.blank_line
44
+
45
+ formatter << "return CA_OK;"
46
+ end
47
+
48
+ def struct_proto(formatter)
49
+ formatter << (render + ";")
50
+ end
51
+
52
+ def struct_defn(formatter)
53
+ formatter << render
54
+ formatter.braces do
55
+ formatter << "#{ty_bldr.render} data[#{@blueprint.array_size}];"
56
+ end
57
+ formatter.append(";")
58
+ end
59
+
60
+ private
61
+
62
+ def length_sym
63
+ "FIXED_ARRAY_LENGTH_#{@blueprint.name}"
64
+ end
65
+
66
+ def ty_bldr
67
+ @ty_bldr ||= Builders.get(:c, @blueprint.array_type)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ Cauterize::Builders.register(:c, Cauterize::FixedArray, Cauterize::Builders::C::FixedArray)
@@ -1,143 +1,143 @@
1
- module Cauterize
2
- module Builders
3
- module C
4
- class Maxer
5
- # Produce a preprocessor string that results in the maximum size of the passed
6
- # in symbols. Uses the CA_MAX macro from the Cauterize support C files.
7
- def self.max_str(*syms)
8
- a = syms.to_a
9
- pre = syms.to_a.inject("") {|s, v| s + "CA_MAX(" + v.to_s + ", "}
10
- return pre + "0" + (")" * a.length)
11
- end
12
- end
13
-
14
- class Group < Buildable
15
- def initialize(blueprint)
16
- super(blueprint)
17
- @tag_enum = blueprint.tag_enum
18
- end
19
-
20
- def render
21
- "struct #{@blueprint.name.to_s}"
22
- end
23
-
24
- def declare(formatter, sym)
25
- formatter << "#{render} #{sym};"
26
- end
27
-
28
- def preprocessor_defines(formatter)
29
- field_lens = @blueprint.fields.values.map do |field|
30
- if field.type.nil?
31
- nil
32
- else
33
- Builders.get(:c, field.type).max_enc_len_cpp_sym
34
- end
35
- end.compact
36
-
37
- formatter << ["#define #{max_enc_len_cpp_sym}",
38
- "(#{enum_builder.max_enc_len_cpp_sym} +",
39
- Maxer.max_str(*field_lens)].join(" ") + ")"
40
- end
41
-
42
- def packer_defn(formatter)
43
- formatter << "CAUTERIZE_STATUS_T err;"
44
-
45
- # pack the tag
46
- formatter << "if (CA_OK != (err = #{enum_builder.packer_sym}(dst, &src->tag))) { return err; }"
47
-
48
- # pack the fields
49
- formatter << "switch (src->tag)"
50
- formatter.braces do
51
- having_data.each do |field|
52
- bldr = Builders.get(:c, field.type)
53
- formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
54
- formatter << "if (CA_OK != (err = #{bldr.packer_sym}(dst, &src->data.#{field.name}))) { return err; }"
55
- formatter << "break;"
56
- end
57
-
58
- format_no_data_stubs(formatter)
59
-
60
- formatter.backdent "default:"
61
- formatter << "return CA_ERR_INVALID_TYPE_TAG;"
62
- formatter << "break;"
63
- end
64
- formatter << "return CA_OK;"
65
- end
66
-
67
- def unpacker_defn(formatter)
68
- enum_builder = Builders.get(:c, @tag_enum)
69
-
70
- formatter << "CAUTERIZE_STATUS_T err;"
71
-
72
- # unpack the tag
73
- formatter << "if (CA_OK != (err = #{enum_builder.unpacker_sym}(src, &dst->tag))) { return err; }"
74
-
75
- # pack the fields
76
- formatter << "switch (dst->tag)"
77
- formatter.braces do
78
- having_data.each do |field|
79
- bldr = Builders.get(:c, field.type)
80
- formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
81
- formatter << "if (CA_OK != (err = #{bldr.unpacker_sym}(src, &dst->data.#{field.name}))) { return err; }"
82
- formatter << "break;"
83
- end
84
-
85
- format_no_data_stubs(formatter)
86
-
87
- formatter.backdent "default:"
88
- formatter << "return CA_ERR_INVALID_TYPE_TAG;"
89
- formatter << "break;"
90
- end
91
- formatter << "return CA_OK;"
92
- end
93
-
94
- def struct_proto(formatter)
95
- formatter << (render + ";")
96
- end
97
-
98
- def struct_defn(formatter)
99
- formatter << render
100
- formatter.braces do
101
- Builders.get(:c, @tag_enum).declare(formatter, "tag")
102
- formatter << "union"
103
- formatter.braces do
104
- @blueprint.fields.values.each do |field|
105
- if field.type
106
- Builders.get(:c, field.type).declare(formatter, field.name)
107
- else
108
- formatter << "/* No data associated with '#{field.name}'. */"
109
- end
110
- end
111
- end
112
- formatter.append(" data;")
113
- end
114
- formatter.append(";")
115
- end
116
-
117
- private
118
-
119
- def having_data
120
- @blueprint.fields.values.reject {|v| v.type.nil?}
121
- end
122
-
123
- def no_data
124
- @blueprint.fields.values.reject {|v| not v.type.nil?}
125
- end
126
-
127
- def format_no_data_stubs(formatter)
128
- if 0 < no_data.length
129
- formatter.backdent "/* No data associated with the remaining tags. */"
130
- no_data.each do |field|
131
- formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
132
- end
133
- formatter << "break;"
134
- end
135
- end
136
-
137
- def enum_builder; Builders.get(:c, @tag_enum) end
138
- end
139
- end
140
- end
141
- end
142
-
143
- Cauterize::Builders.register(:c, Cauterize::Group, Cauterize::Builders::C::Group)
1
+ module Cauterize
2
+ module Builders
3
+ module C
4
+ class Maxer
5
+ # Produce a preprocessor string that results in the maximum size of the passed
6
+ # in symbols. Uses the CA_MAX macro from the Cauterize support C files.
7
+ def self.max_str(*syms)
8
+ a = syms.to_a
9
+ pre = syms.to_a.inject("") {|s, v| s + "CA_MAX(" + v.to_s + ", "}
10
+ return pre + "0" + (")" * a.length)
11
+ end
12
+ end
13
+
14
+ class Group < Buildable
15
+ def initialize(blueprint)
16
+ super(blueprint)
17
+ @tag_enum = blueprint.tag_enum
18
+ end
19
+
20
+ def render
21
+ "struct #{@blueprint.name.to_s}"
22
+ end
23
+
24
+ def declare(formatter, sym)
25
+ formatter << "#{render} #{sym};"
26
+ end
27
+
28
+ def preprocessor_defines(formatter)
29
+ field_lens = @blueprint.fields.values.map do |field|
30
+ if field.type.nil?
31
+ nil
32
+ else
33
+ Builders.get(:c, field.type).max_enc_len_cpp_sym
34
+ end
35
+ end.compact
36
+
37
+ formatter << ["#define #{max_enc_len_cpp_sym}",
38
+ "(#{enum_builder.max_enc_len_cpp_sym} +",
39
+ Maxer.max_str(*field_lens)].join(" ") + ")"
40
+ end
41
+
42
+ def packer_defn(formatter)
43
+ formatter << "CAUTERIZE_STATUS_T err;"
44
+
45
+ # pack the tag
46
+ formatter << "if (CA_OK != (err = #{enum_builder.packer_sym}(dst, &src->tag))) { return err; }"
47
+
48
+ # pack the fields
49
+ formatter << "switch (src->tag)"
50
+ formatter.braces do
51
+ having_data.each do |field|
52
+ bldr = Builders.get(:c, field.type)
53
+ formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
54
+ formatter << "if (CA_OK != (err = #{bldr.packer_sym}(dst, &src->data.#{field.name}))) { return err; }"
55
+ formatter << "break;"
56
+ end
57
+
58
+ format_no_data_stubs(formatter)
59
+
60
+ formatter.backdent "default:"
61
+ formatter << "return CA_ERR_INVALID_TYPE_TAG;"
62
+ formatter << "break;"
63
+ end
64
+ formatter << "return CA_OK;"
65
+ end
66
+
67
+ def unpacker_defn(formatter)
68
+ enum_builder = Builders.get(:c, @tag_enum)
69
+
70
+ formatter << "CAUTERIZE_STATUS_T err;"
71
+
72
+ # unpack the tag
73
+ formatter << "if (CA_OK != (err = #{enum_builder.unpacker_sym}(src, &dst->tag))) { return err; }"
74
+
75
+ # pack the fields
76
+ formatter << "switch (dst->tag)"
77
+ formatter.braces do
78
+ having_data.each do |field|
79
+ bldr = Builders.get(:c, field.type)
80
+ formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
81
+ formatter << "if (CA_OK != (err = #{bldr.unpacker_sym}(src, &dst->data.#{field.name}))) { return err; }"
82
+ formatter << "break;"
83
+ end
84
+
85
+ format_no_data_stubs(formatter)
86
+
87
+ formatter.backdent "default:"
88
+ formatter << "return CA_ERR_INVALID_TYPE_TAG;"
89
+ formatter << "break;"
90
+ end
91
+ formatter << "return CA_OK;"
92
+ end
93
+
94
+ def struct_proto(formatter)
95
+ formatter << (render + ";")
96
+ end
97
+
98
+ def struct_defn(formatter)
99
+ formatter << render
100
+ formatter.braces do
101
+ Builders.get(:c, @tag_enum).declare(formatter, "tag")
102
+ formatter << "union"
103
+ formatter.braces do
104
+ @blueprint.fields.values.each do |field|
105
+ if field.type
106
+ Builders.get(:c, field.type).declare(formatter, field.name)
107
+ else
108
+ formatter << "/* No data associated with '#{field.name}'. */"
109
+ end
110
+ end
111
+ end
112
+ formatter.append(" data;")
113
+ end
114
+ formatter.append(";")
115
+ end
116
+
117
+ private
118
+
119
+ def having_data
120
+ @blueprint.fields.values.reject {|v| v.type.nil?}
121
+ end
122
+
123
+ def no_data
124
+ @blueprint.fields.values.reject {|v| not v.type.nil?}
125
+ end
126
+
127
+ def format_no_data_stubs(formatter)
128
+ if 0 < no_data.length
129
+ formatter.backdent "/* No data associated with the remaining tags. */"
130
+ no_data.each do |field|
131
+ formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
132
+ end
133
+ formatter << "break;"
134
+ end
135
+ end
136
+
137
+ def enum_builder; Builders.get(:c, @tag_enum) end
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ Cauterize::Builders.register(:c, Cauterize::Group, Cauterize::Builders::C::Group)