cauterize 0.0.1.pre1 → 0.0.1.pre5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +1 -1
- data/Gemfile +1 -1
- data/README.md +0 -2
- data/Rakefile +19 -3
- data/bin/cauterize +14 -6
- data/example/Cauterize +22 -10
- data/example/build.sh +15 -2
- data/lib/cauterize/base_type.rb +2 -5
- data/lib/cauterize/builders.rb +1 -3
- data/lib/cauterize/builders/c/buildable.rb +3 -2
- data/lib/cauterize/builders/c/builtin.rb +46 -0
- data/lib/cauterize/builders/c/enumeration.rb +23 -2
- data/lib/cauterize/builders/c/fixed_array.rb +16 -7
- data/lib/cauterize/builders/c/group.rb +32 -3
- data/lib/cauterize/builders/c/scalar.rb +5 -0
- data/lib/cauterize/builders/c/variable_array.rb +1 -1
- data/lib/cauterize/builders/cs/buildable.rb +59 -0
- data/lib/cauterize/builders/cs/builtin.rb +23 -0
- data/lib/cauterize/builders/cs/composite.rb +21 -0
- data/lib/cauterize/builders/cs/csarray.rb +32 -0
- data/lib/cauterize/builders/cs/enumeration.rb +21 -0
- data/lib/cauterize/builders/cs/fixed_array.rb +25 -0
- data/lib/cauterize/builders/cs/group.rb +33 -0
- data/lib/cauterize/builders/cs/scalar.rb +10 -0
- data/lib/cauterize/builders/cs/variable_array.rb +34 -0
- data/lib/cauterize/builtin.rb +52 -0
- data/lib/cauterize/c_builder.rb +15 -1
- data/lib/cauterize/cauterize.rb +44 -13
- data/lib/cauterize/composite.rb +3 -3
- data/lib/cauterize/cs_builder.rb +53 -0
- data/lib/cauterize/enumeration.rb +20 -3
- data/lib/cauterize/fixed_array.rb +3 -3
- data/lib/cauterize/formatter.rb +7 -3
- data/lib/cauterize/group.rb +5 -8
- data/lib/cauterize/scalar.rb +16 -7
- data/lib/cauterize/variable_array.rb +6 -6
- data/lib/cauterize/version.rb +1 -1
- data/spec/base_type_spec.rb +133 -125
- data/spec/builders/c/buildable_spec.rb +18 -18
- data/spec/builders/c/builtin_spec.rb +22 -0
- data/spec/builders/c/composite_spec.rb +37 -33
- data/spec/builders/c/enumeration_spec.rb +84 -21
- data/spec/builders/c/fixed_array_spec.rb +6 -6
- data/spec/builders/c/group_spec.rb +97 -90
- data/spec/builders/c/scalar_spec.rb +24 -6
- data/spec/builders/c/variable_array_spec.rb +37 -37
- data/spec/builders/cs/buildable_spec.rb +8 -0
- data/spec/builders/cs/composite_spec.rb +32 -0
- data/spec/builders/cs/enumeration_spec.rb +33 -0
- data/spec/builders/cs/fixed_array_spec.rb +40 -0
- data/spec/builders/cs/group_spec.rb +56 -0
- data/spec/builders/cs/scalar_spec.rb +7 -0
- data/spec/builders/cs/variable_array_spec.rb +46 -0
- data/spec/builders_spec.rb +38 -38
- data/spec/builtin_spec.rb +46 -0
- data/spec/c_builder_spec.rb +116 -102
- data/spec/cauterize_spec.rb +8 -1
- data/spec/composite_spec.rb +52 -48
- data/spec/cs_builder_spec.rb +113 -0
- data/spec/enumeration_spec.rb +55 -16
- data/spec/fixed_array_spec.rb +7 -9
- data/spec/group_spec.rb +81 -76
- data/spec/scalar_spec.rb +20 -10
- data/spec/spec_helper.rb +103 -94
- data/spec/support/shared_examples_for_c_buildables.rb +68 -64
- data/spec/variable_array_spec.rb +12 -17
- data/{c → support/c}/src/cauterize.c +8 -7
- data/support/c/src/cauterize.h +59 -0
- data/{c → support/c}/src/cauterize_debug.h +0 -0
- data/support/c/src/cauterize_util.h +49 -0
- data/{c → support/c}/test/greatest.h +0 -0
- data/{c → support/c}/test/test.c +0 -0
- data/support/cs/src/CauterizeCompositeFormatter.cs +34 -0
- data/support/cs/src/CauterizeContainerFormatter.cs +18 -0
- data/support/cs/src/CauterizeEnumFormatter.cs +67 -0
- data/support/cs/src/CauterizeException.cs +15 -0
- data/support/cs/src/CauterizeFixedArrayFormatter.cs +39 -0
- data/support/cs/src/CauterizeFormatter.cs +40 -0
- data/support/cs/src/CauterizeGroupFormatter.cs +46 -0
- data/support/cs/src/CauterizePrimitiveFormatter.cs +33 -0
- data/support/cs/src/CauterizeTypeFormatterFactory.cs +39 -0
- data/support/cs/src/CauterizeTypes.cs +107 -0
- data/support/cs/src/CauterizeVariableArrayFormatter.cs +49 -0
- data/support/cs/src/ICauterizeTypeFormatter.cs +12 -0
- data/support/cs/src/OrderAttribute.cs +50 -0
- data/support/cs/src/PrimitiveSupport.cs +134 -0
- data/support/cs/src/SerializedRepresentationAttribute.cs +24 -0
- data/support/cs/test/CauterizeCompositeFormatterTest.cs +59 -0
- data/support/cs/test/CauterizeEnumFormatterTest.cs +110 -0
- data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +91 -0
- data/support/cs/test/CauterizeFormatterTest.cs +40 -0
- data/support/cs/test/CauterizeGroupFormatterTest.cs +147 -0
- data/support/cs/test/CauterizeIntegrationTest.cs +129 -0
- data/support/cs/test/CauterizePrimitiveFormatterTest.cs +98 -0
- data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +73 -0
- data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +130 -0
- data/support/cs/test/OrderAttributeTest.cs +39 -0
- data/support/cs/test/SerializedRepresentationAttributeTest.cs +39 -0
- metadata +68 -10
- data/c/src/cauterize.h +0 -46
- data/c/src/cauterize_util.h +0 -7
data/.gitignore
CHANGED
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
-r spec_helper
|
1
|
+
-r spec_helper
|
data/Gemfile
CHANGED
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'tmpdir'
|
2
|
+
require 'fileutils'
|
2
3
|
require 'bundler/gem_tasks'
|
3
4
|
require 'rspec/core/rake_task'
|
4
5
|
|
@@ -13,20 +14,35 @@ task :greatest do
|
|
13
14
|
test_suite_path = File.join(d, "test_suite.c")
|
14
15
|
mk_test_suite_file(test_suite_path)
|
15
16
|
|
16
|
-
args = "-Wall -Wextra -Werror -
|
17
|
-
srcs = "c/src/cauterize.c c/test/test.c"
|
17
|
+
args = "-Wall -Wextra -Werror -Isupport/c/test -Isupport/c/src -I#{d}"
|
18
|
+
srcs = "support/c/src/cauterize.c support/c/test/test.c"
|
18
19
|
bin = File.join(d, "test.bin")
|
19
20
|
sh "gcc #{args} #{srcs} -o #{bin}"
|
20
21
|
sh bin
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
25
|
+
desc "Run C# tests"
|
26
|
+
task :nunit do
|
27
|
+
cd "support/cs" do
|
28
|
+
FileUtils.mkdir_p "lib"
|
29
|
+
sh "dmcs -target:library -out:lib/Cauterize.dll src/*.cs"
|
30
|
+
references = "-r:lib/nunit.framework.dll -r:lib/Moq.dll -r:lib/Cauterize.dll"
|
31
|
+
sh "dmcs -target:library #{references} -out:lib/Cauterize.Test.dll test/*.cs"
|
32
|
+
sh "#{nunit} lib/*.dll"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def nunit
|
37
|
+
"nunit-console4"
|
38
|
+
end
|
39
|
+
|
24
40
|
# Support Methods
|
25
41
|
|
26
42
|
SUITE_ENTRY_TEMPLATE = " RUN_TEST(%s);"
|
27
43
|
|
28
44
|
def mk_test_suite_file(path)
|
29
|
-
test_files = Dir["c/test/*.c"]
|
45
|
+
test_files = Dir["support/c/test/*.c"]
|
30
46
|
suite_text = test_files.map do |test_file|
|
31
47
|
File.read(test_file).lines.map do |l|
|
32
48
|
m = l.match(/^TEST (?<sym>[^\(]+)\(\)/)
|
data/bin/cauterize
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
LIB_PATH = File.dirname(__FILE__) + "/../lib"
|
4
|
-
C_PATH = File.dirname(__FILE__) + "/../c/src"
|
5
4
|
|
6
5
|
$LOAD_PATH.unshift LIB_PATH
|
7
6
|
|
@@ -20,8 +19,6 @@ class CauterizeCmdline < Thor
|
|
20
19
|
:desc => "Bravely overwrite files in non-empty directories",
|
21
20
|
:default => false
|
22
21
|
def generate(language, target_dir, desc_file = nil)
|
23
|
-
raise CmdLineException.new("Language '#{language}' is not supported.") unless language == 'c'
|
24
|
-
|
25
22
|
if not options[:force] and Dir.exist?(target_dir) and Dir[target_dir + "/*"].size > 0
|
26
23
|
raise CmdLineException.new("Directory '#{target_dir}' isn't empty. Cowardly quitting.")
|
27
24
|
end
|
@@ -32,16 +29,27 @@ class CauterizeCmdline < Thor
|
|
32
29
|
raise CmdLineException.new("The description file #{desc_file} does not exist.")
|
33
30
|
end
|
34
31
|
|
35
|
-
|
36
|
-
|
32
|
+
language = language.downcase
|
33
|
+
if %w(c cs).include? language
|
34
|
+
Cauterize.generate language, target_dir, desc_file
|
35
|
+
else
|
36
|
+
raise CmdLineException.new("Language '#{language}' is not supported.")
|
37
|
+
end
|
37
38
|
|
38
39
|
# Copy the cauterize support files.
|
39
40
|
unless options[:omit_support]
|
40
|
-
|
41
|
+
support_path = File.join(File.dirname(__FILE__),"../support/#{language}/src")
|
42
|
+
Dir["#{support_path}/*"].each do |path|
|
41
43
|
FileUtils.cp(path, target_dir)
|
42
44
|
end
|
43
45
|
end
|
44
46
|
end
|
47
|
+
|
48
|
+
desc "version",
|
49
|
+
"Prints the cauterize version to the screen."
|
50
|
+
def version
|
51
|
+
puts "cauterize-" + Cauterize::VERSION
|
52
|
+
end
|
45
53
|
end
|
46
54
|
|
47
55
|
begin
|
data/example/Cauterize
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
set_name("example_project")
|
2
|
+
set_version("1.2.3")
|
2
3
|
|
3
|
-
scalar(:
|
4
|
-
scalar(:
|
5
|
-
scalar(:int32_t)
|
6
|
-
scalar(:int64_t)
|
7
|
-
scalar(:uint8_t)
|
8
|
-
scalar(:uint16_t)
|
9
|
-
scalar(:uint32_t)
|
10
|
-
scalar(:uint64_t)
|
4
|
+
scalar(:bigint) { |t| t.type_name(:int32) }
|
5
|
+
scalar(:usmallint) { |t| t.type_name(:uint8) }
|
11
6
|
|
12
7
|
enumeration(:color) do |e|
|
13
8
|
e.value :red
|
@@ -15,14 +10,29 @@ enumeration(:color) do |e|
|
|
15
10
|
e.value :green
|
16
11
|
end
|
17
12
|
|
13
|
+
enumeration(:large_value) do |e|
|
14
|
+
e.value :negative, -500
|
15
|
+
e.value :positive, 500
|
16
|
+
end
|
17
|
+
|
18
|
+
composite(:weirdness) do |c|
|
19
|
+
c.field :val, :large_value
|
20
|
+
c.field :num, :int8
|
21
|
+
end
|
22
|
+
|
18
23
|
fixed_array(:color_list) do |a|
|
19
24
|
a.array_type :color
|
20
25
|
a.array_size 4
|
21
26
|
end
|
22
27
|
|
28
|
+
fixed_array(:color_list_list) do |a|
|
29
|
+
a.array_type :color_list
|
30
|
+
a.array_size 4
|
31
|
+
end
|
32
|
+
|
23
33
|
variable_array(:numbers) do |a|
|
24
|
-
a.size_type :
|
25
|
-
a.array_type :
|
34
|
+
a.size_type :usmallint
|
35
|
+
a.array_type :bigint
|
26
36
|
a.array_size 128
|
27
37
|
end
|
28
38
|
|
@@ -41,4 +51,6 @@ end
|
|
41
51
|
group(:insanity) do |g|
|
42
52
|
g.field :nonsensical, :nonsensical
|
43
53
|
g.field :crazy, :crazy
|
54
|
+
g.field :any_empty_entry
|
55
|
+
g.field :another_empty
|
44
56
|
end
|
data/example/build.sh
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
#!/bin/sh
|
2
2
|
|
3
|
-
rm -rf
|
4
|
-
../bin/cauterize generate c
|
3
|
+
rm -rf c_cauterize_output
|
4
|
+
../bin/cauterize generate c c_cauterize_output
|
5
|
+
|
6
|
+
CFLAGS="-Wall -Werror -Wextra"
|
7
|
+
INCLUDES="-Ic_cauterize_output"
|
8
|
+
|
9
|
+
|
10
|
+
gcc $CFLAGS $INCLUDES c_cauterize_output/cauterize.c -c -o c_cauterize_output/cauterize.o
|
11
|
+
gcc $CFLAGS $INCLUDES c_cauterize_output/example_project.c -c -o c_cauterize_output/example_project.o
|
12
|
+
|
13
|
+
rm -rf cs_cauterize_output
|
14
|
+
../bin/cauterize generate cs cs_cauterize_output
|
15
|
+
|
16
|
+
dmcs cs_cauterize_output/*.cs -target:library -out:cs_cauterize_output/example_project.dll
|
17
|
+
|
data/lib/cauterize/base_type.rb
CHANGED
@@ -5,13 +5,10 @@ module Cauterize
|
|
5
5
|
attr_reader :name, :id
|
6
6
|
@@next_id = {}
|
7
7
|
@@instances = {}
|
8
|
-
@@used_names = Set.new
|
9
8
|
|
10
9
|
def initialize(name)
|
11
|
-
if @@
|
12
|
-
raise Exception.new("A type with the name #{name} already exists.")
|
13
|
-
else
|
14
|
-
@@used_names << name
|
10
|
+
if @@instances.keys.include?(name)
|
11
|
+
raise Exception.new("A type with the name #{name} already exists. [#{@@instances[name].inspect}]")
|
15
12
|
end
|
16
13
|
|
17
14
|
@name = name
|
data/lib/cauterize/builders.rb
CHANGED
@@ -17,10 +17,8 @@ module Cauterize
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def get(language, description_instance)
|
20
|
-
if @builders and @builders[language]
|
20
|
+
if @builders and @builders[language] and @builders[language][description_instance.class]
|
21
21
|
@builders[language][description_instance.class].new(description_instance)
|
22
|
-
else
|
23
|
-
raise UnregisteredException.new("The builder for #{description_instance.class} in #{language} is unregistered.")
|
24
22
|
end
|
25
23
|
end
|
26
24
|
end
|
@@ -38,18 +38,19 @@ module Cauterize
|
|
38
38
|
|
39
39
|
# Methods that are pretty much the same for everyone.
|
40
40
|
def packer_sym; "Pack_#{@blueprint.name}" end
|
41
|
-
def packer_sig; "CAUTERIZE_STATUS_T #{packer_sym}(struct Cauterize * dst, #{render} * src)" end
|
41
|
+
def packer_sig; "CALLCONV CAUTERIZE_STATUS_T DLLDECL #{packer_sym}(struct Cauterize * dst, #{render} * src)" end
|
42
42
|
def packer_proto(formatter)
|
43
43
|
formatter << packer_sig + ";"
|
44
44
|
end
|
45
45
|
|
46
46
|
def unpacker_sym; "Unpack_#{@blueprint.name}" end
|
47
|
-
def unpacker_sig; "CAUTERIZE_STATUS_T #{unpacker_sym}(struct Cauterize * src, #{render} * dst)" end
|
47
|
+
def unpacker_sig; "CALLCONV CAUTERIZE_STATUS_T DLLDECL #{unpacker_sym}(struct Cauterize * src, #{render} * dst)" end
|
48
48
|
def unpacker_proto(formatter)
|
49
49
|
formatter << unpacker_sig + ";"
|
50
50
|
end
|
51
51
|
|
52
52
|
# These are only different in a few type varieties.
|
53
|
+
def typedef_decl(formatter); nil end
|
53
54
|
def struct_proto(formatter); nil end
|
54
55
|
def struct_defn(formatter); nil end
|
55
56
|
def enum_defn(formatter); nil end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Cauterize
|
2
|
+
module Builders
|
3
|
+
module C
|
4
|
+
class BuiltIn < Buildable
|
5
|
+
@@C_TYPE_MAPPING = {
|
6
|
+
1 => {signed: :int8_t, unsigned: :uint8_t},
|
7
|
+
2 => {signed: :int16_t, unsigned: :uint16_t},
|
8
|
+
4 => {signed: :int32_t, unsigned: :uint32_t},
|
9
|
+
8 => {signed: :int64_t, unsigned: :uint64_t},
|
10
|
+
}
|
11
|
+
|
12
|
+
def render
|
13
|
+
render_ctype
|
14
|
+
end
|
15
|
+
|
16
|
+
def declare(formatter, sym)
|
17
|
+
formatter << "#{render} #{sym};"
|
18
|
+
end
|
19
|
+
|
20
|
+
# These are identical to the Scalar definitions. For now.
|
21
|
+
def packer_defn(formatter)
|
22
|
+
formatter << packer_sig
|
23
|
+
formatter.braces do
|
24
|
+
formatter << "return CauterizeAppend(dst, (uint8_t*)src, sizeof(*src));"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def unpacker_defn(formatter)
|
29
|
+
formatter << unpacker_sig
|
30
|
+
formatter.braces do
|
31
|
+
formatter << "return CauterizeRead(src, (uint8_t*)dst, sizeof(*dst));"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def render_ctype
|
38
|
+
s_key = @blueprint.is_signed ? :signed : :unsigned
|
39
|
+
@@C_TYPE_MAPPING[@blueprint.byte_length][s_key]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Cauterize::Builders.register(:c, Cauterize::BuiltIn, Cauterize::Builders::C::BuiltIn)
|
@@ -11,16 +11,37 @@ module Cauterize
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def packer_defn(formatter)
|
14
|
+
rep_builder = Builders.get(:c, @blueprint.representation)
|
15
|
+
|
14
16
|
formatter << packer_sig
|
15
17
|
formatter.braces do
|
16
|
-
|
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});"
|
17
23
|
end
|
18
24
|
end
|
19
25
|
|
20
26
|
def unpacker_defn(formatter)
|
27
|
+
rep_builder = Builders.get(:c, @blueprint.representation)
|
28
|
+
|
21
29
|
formatter << unpacker_sig
|
22
30
|
formatter.braces do
|
23
|
-
|
31
|
+
rep = "enum_representation"
|
32
|
+
rep_builder.declare(formatter, rep)
|
33
|
+
|
34
|
+
formatter << "CAUTERIZE_STATUS_T s = #{rep_builder.unpacker_sym}(src, &#{rep});"
|
35
|
+
formatter << "if (CA_OK != s)"
|
36
|
+
formatter.braces do
|
37
|
+
formatter << "return s;"
|
38
|
+
end
|
39
|
+
|
40
|
+
formatter << "else"
|
41
|
+
formatter.braces do
|
42
|
+
formatter << "*dst = (#{render})#{rep};"
|
43
|
+
formatter << "return CA_OK;"
|
44
|
+
end
|
24
45
|
end
|
25
46
|
end
|
26
47
|
|
@@ -3,14 +3,11 @@ module Cauterize
|
|
3
3
|
module C
|
4
4
|
class FixedArray < Buildable
|
5
5
|
def render
|
6
|
-
#
|
7
|
-
# Keep in mind that there's no sane way to "render" an array in C
|
8
|
-
# that doesn't involve an identifier.
|
9
|
-
ty_bldr.render
|
6
|
+
"struct #{@blueprint.name}"
|
10
7
|
end
|
11
8
|
|
12
9
|
def declare(formatter, sym)
|
13
|
-
formatter << "#{
|
10
|
+
formatter << "#{render} #{sym};"
|
14
11
|
end
|
15
12
|
|
16
13
|
def packer_defn(formatter)
|
@@ -23,7 +20,7 @@ module Cauterize
|
|
23
20
|
# store each used item in the array
|
24
21
|
formatter << "for (i = 0; i < #{@blueprint.array_size}; i++)"
|
25
22
|
formatter.braces do
|
26
|
-
formatter << "if (CA_OK != (err = #{ty_bldr.packer_sym}(dst, &src[i]))) { return err; }"
|
23
|
+
formatter << "if (CA_OK != (err = #{ty_bldr.packer_sym}(dst, &src->data[i]))) { return err; }"
|
27
24
|
end
|
28
25
|
formatter.blank_line
|
29
26
|
|
@@ -41,7 +38,7 @@ module Cauterize
|
|
41
38
|
# store each used item in the array
|
42
39
|
formatter << "for (i = 0; i < #{@blueprint.array_size}; i++)"
|
43
40
|
formatter.braces do
|
44
|
-
formatter << "if (CA_OK != (err = #{ty_bldr.unpacker_sym}(src, &dst[i]))) { return err; }"
|
41
|
+
formatter << "if (CA_OK != (err = #{ty_bldr.unpacker_sym}(src, &dst->data[i]))) { return err; }"
|
45
42
|
end
|
46
43
|
formatter.blank_line
|
47
44
|
|
@@ -49,6 +46,18 @@ module Cauterize
|
|
49
46
|
end
|
50
47
|
end
|
51
48
|
|
49
|
+
def struct_proto(formatter)
|
50
|
+
formatter << (render + ";")
|
51
|
+
end
|
52
|
+
|
53
|
+
def struct_defn(formatter)
|
54
|
+
formatter << render
|
55
|
+
formatter.braces do
|
56
|
+
formatter << "#{ty_bldr.render} data[#{@blueprint.array_size}];"
|
57
|
+
end
|
58
|
+
formatter.append(";")
|
59
|
+
end
|
60
|
+
|
52
61
|
private
|
53
62
|
|
54
63
|
def ty_bldr
|
@@ -27,13 +27,15 @@ module Cauterize
|
|
27
27
|
# pack the fields
|
28
28
|
formatter << "switch (src->tag)"
|
29
29
|
formatter.braces do
|
30
|
-
|
30
|
+
having_data.each do |field|
|
31
31
|
bldr = Builders.get(:c, field.type)
|
32
32
|
formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
|
33
33
|
formatter << "if (CA_OK != (err = #{bldr.packer_sym}(dst, &src->data.#{field.name}))) { return err; }"
|
34
34
|
formatter << "break;"
|
35
35
|
end
|
36
36
|
|
37
|
+
format_no_data_stubs(formatter)
|
38
|
+
|
37
39
|
formatter.backdent "default:"
|
38
40
|
formatter << "return CA_ERR_INVALUD_TYPE_TAG;"
|
39
41
|
formatter << "break;"
|
@@ -54,13 +56,15 @@ module Cauterize
|
|
54
56
|
# pack the fields
|
55
57
|
formatter << "switch (dst->tag)"
|
56
58
|
formatter.braces do
|
57
|
-
|
59
|
+
having_data.each do |field|
|
58
60
|
bldr = Builders.get(:c, field.type)
|
59
61
|
formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
|
60
62
|
formatter << "if (CA_OK != (err = #{bldr.unpacker_sym}(src, &dst->data.#{field.name}))) { return err; }"
|
61
63
|
formatter << "break;"
|
62
64
|
end
|
63
65
|
|
66
|
+
format_no_data_stubs(formatter)
|
67
|
+
|
64
68
|
formatter.backdent "default:"
|
65
69
|
formatter << "return CA_ERR_INVALUD_TYPE_TAG;"
|
66
70
|
formatter << "break;"
|
@@ -80,13 +84,38 @@ module Cauterize
|
|
80
84
|
formatter << "union"
|
81
85
|
formatter.braces do
|
82
86
|
@blueprint.fields.values.each do |field|
|
83
|
-
Builders.get(:c, field.type)
|
87
|
+
b = Builders.get(:c, field.type)
|
88
|
+
if b
|
89
|
+
b.declare(formatter, field.name)
|
90
|
+
else
|
91
|
+
formatter << "/* No data associated with '#{field.name}'. */"
|
92
|
+
end
|
84
93
|
end
|
85
94
|
end
|
86
95
|
formatter.append(" data;")
|
87
96
|
end
|
88
97
|
formatter.append(";")
|
89
98
|
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def having_data
|
103
|
+
@blueprint.fields.values.reject {|v| v.type.nil?}
|
104
|
+
end
|
105
|
+
|
106
|
+
def no_data
|
107
|
+
@blueprint.fields.values.reject {|v| not v.type.nil?}
|
108
|
+
end
|
109
|
+
|
110
|
+
def format_no_data_stubs(formatter)
|
111
|
+
if 0 < no_data.length
|
112
|
+
formatter.backdent "/* No data associated with the remaining tags. */"
|
113
|
+
no_data.each do |field|
|
114
|
+
formatter.backdent "case #{@blueprint.enum_sym(field.name)}:"
|
115
|
+
end
|
116
|
+
formatter << "break;"
|
117
|
+
end
|
118
|
+
end
|
90
119
|
end
|
91
120
|
end
|
92
121
|
end
|