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.
- checksums.yaml +4 -4
- data/.gitignore +33 -33
- data/.rspec +1 -1
- data/.travisci.yml +4 -4
- data/Gemfile +4 -4
- data/LICENSE.txt +21 -21
- data/README.md +177 -177
- data/Rakefile +54 -54
- data/bin/cauterize +61 -61
- data/cauterize.gemspec +27 -27
- data/example/Cauterize +68 -68
- data/example/build.sh +25 -25
- data/example/c_example_support/empty_main.c +1 -1
- data/example/c_example_support/example_project_config.c +11 -11
- data/example/c_example_support/example_project_config.h +15 -15
- data/example/ruby_ex.rb +14 -14
- data/lib/cauterize.rb +117 -117
- data/lib/cauterize/base_type.rb +78 -78
- data/lib/cauterize/builders.rb +41 -41
- data/lib/cauterize/builders/c/buildable.rb +91 -91
- data/lib/cauterize/builders/c/builtin.rb +43 -43
- data/lib/cauterize/builders/c/composite.rb +57 -57
- data/lib/cauterize/builders/c/enumeration.rb +60 -60
- data/lib/cauterize/builders/c/fixed_array.rb +74 -74
- data/lib/cauterize/builders/c/group.rb +143 -143
- data/lib/cauterize/builders/c/scalar.rb +34 -34
- data/lib/cauterize/builders/c/variable_array.rb +98 -98
- data/lib/cauterize/builders/cs/buildable.rb +59 -59
- data/lib/cauterize/builders/cs/builtin.rb +22 -22
- data/lib/cauterize/builders/cs/composite.rb +21 -21
- data/lib/cauterize/builders/cs/csarray.rb +32 -32
- data/lib/cauterize/builders/cs/enumeration.rb +21 -21
- data/lib/cauterize/builders/cs/fixed_array.rb +27 -27
- data/lib/cauterize/builders/cs/group.rb +32 -32
- data/lib/cauterize/builders/cs/scalar.rb +10 -10
- data/lib/cauterize/builders/cs/variable_array.rb +40 -34
- data/lib/cauterize/builders/doc/buildable.rb +30 -30
- data/lib/cauterize/builders/doc/builtin.rb +21 -21
- data/lib/cauterize/builders/doc/composite.rb +23 -23
- data/lib/cauterize/builders/doc/enumeration.rb +24 -24
- data/lib/cauterize/builders/doc/fixed_array.rb +26 -26
- data/lib/cauterize/builders/doc/group.rb +29 -29
- data/lib/cauterize/builders/doc/scalar.rb +21 -21
- data/lib/cauterize/builders/doc/variable_array.rb +26 -26
- data/lib/cauterize/builders/ruby/buildable.rb +11 -11
- data/lib/cauterize/builders/ruby/builtin.rb +25 -25
- data/lib/cauterize/builders/ruby/composite.rb +26 -26
- data/lib/cauterize/builders/ruby/enumeration.rb +27 -27
- data/lib/cauterize/builders/ruby/fixed_array.rb +20 -20
- data/lib/cauterize/builders/ruby/group.rb +35 -35
- data/lib/cauterize/builders/ruby/scalar.rb +19 -19
- data/lib/cauterize/builders/ruby/variable_array.rb +22 -22
- data/lib/cauterize/builtin.rb +62 -62
- data/lib/cauterize/c_builder.rb +103 -103
- data/lib/cauterize/cauterize.rb +76 -76
- data/lib/cauterize/composite.rb +64 -64
- data/lib/cauterize/cs_builder.rb +58 -58
- data/lib/cauterize/doc_builder.rb +34 -34
- data/lib/cauterize/enumeration.rb +98 -98
- data/lib/cauterize/fixed_array.rb +50 -50
- data/lib/cauterize/formatter.rb +67 -67
- data/lib/cauterize/group.rb +81 -81
- data/lib/cauterize/representation.rb +32 -32
- data/lib/cauterize/ruby_builder.rb +44 -44
- data/lib/cauterize/scalar.rb +53 -53
- data/lib/cauterize/snake_case.rb +21 -21
- data/lib/cauterize/variable_array.rb +55 -55
- data/lib/cauterize/version.rb +3 -3
- data/spec/base_type_spec.rb +251 -251
- data/spec/builders/c/buildable_spec.rb +25 -25
- data/spec/builders/c/builtin_spec.rb +22 -22
- data/spec/builders/c/composite_spec.rb +50 -50
- data/spec/builders/c/enumeration_spec.rb +95 -95
- data/spec/builders/c/fixed_array_spec.rb +36 -36
- data/spec/builders/c/group_spec.rb +140 -140
- data/spec/builders/c/scalar_spec.rb +26 -26
- data/spec/builders/c/variable_array_spec.rb +48 -48
- data/spec/builders/cs/buildable_spec.rb +8 -8
- data/spec/builders/cs/composite_spec.rb +32 -32
- data/spec/builders/cs/enumeration_spec.rb +33 -33
- data/spec/builders/cs/fixed_array_spec.rb +41 -41
- data/spec/builders/cs/group_spec.rb +56 -56
- data/spec/builders/cs/scalar_spec.rb +7 -7
- data/spec/builders/cs/variable_array_spec.rb +49 -45
- data/spec/builders/doc/buildable_spec.rb +25 -25
- data/spec/builders_spec.rb +57 -57
- data/spec/builtin_spec.rb +43 -43
- data/spec/c_builder_spec.rb +176 -176
- data/spec/cauterize_spec.rb +15 -15
- data/spec/composite_spec.rb +77 -77
- data/spec/cs_builder_spec.rb +115 -115
- data/spec/doc_builder_spec.rb +260 -260
- data/spec/enumeration_spec.rb +145 -145
- data/spec/fixed_array_spec.rb +61 -61
- data/spec/group_spec.rb +111 -111
- data/spec/ruby_builder_spec.rb +83 -83
- data/spec/ruby_generated_spec.rb +735 -735
- data/spec/scalar_spec.rb +44 -44
- data/spec/spec_helper.rb +122 -122
- data/spec/support/shared_examples_for_array_buildables.rb +22 -22
- data/spec/support/shared_examples_for_c_buildables.rb +103 -103
- data/spec/support/shared_examples_for_sane_c_buildables.rb +22 -22
- data/spec/support/shared_examples_for_stubbed_functions.rb +18 -18
- data/spec/support/spec_sample_model.rb +74 -74
- data/spec/test_main.c +13 -13
- data/spec/variable_array_spec.rb +73 -73
- data/support/c/src/cauterize.c +75 -75
- data/support/c/src/cauterize.h +60 -60
- data/support/c/src/cauterize_debug.h +29 -29
- data/support/c/src/cauterize_util.h +8 -8
- data/support/c/test/greatest.h +536 -536
- data/support/c/test/test.c +166 -166
- data/support/cs/src/CauterizeCompositeFormatter.cs +0 -0
- data/support/cs/src/CauterizeContainerFormatter.cs +0 -0
- data/support/cs/src/CauterizeEnumFormatter.cs +0 -0
- data/support/cs/src/CauterizeException.cs +15 -15
- data/support/cs/src/CauterizeFixedArrayFormatter.cs +2 -2
- data/support/cs/src/CauterizeFormatter.cs +0 -0
- data/support/cs/src/CauterizeGroupFormatter.cs +0 -0
- data/support/cs/src/CauterizeTypeFormatterFactory.cs +0 -0
- data/support/cs/src/CauterizeTypes.cs +46 -12
- data/support/cs/src/CauterizeVariableArrayFormatter.cs +1 -1
- data/support/cs/src/ICauterizeTypeFormatter.cs +0 -0
- data/support/cs/src/OrderAttribute.cs +0 -0
- data/support/cs/src/PrimitiveSupport.cs +12 -0
- data/support/cs/test/CauterizeCompositeFormatterTest.cs +0 -0
- data/support/cs/test/CauterizeEnumFormatterTest.cs +0 -0
- data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +0 -0
- data/support/cs/test/CauterizeFormatterTest.cs +0 -0
- data/support/cs/test/CauterizeGroupFormatterTest.cs +0 -0
- data/support/cs/test/CauterizeIntegrationTest.cs +0 -0
- data/support/cs/test/CauterizePrimitiveFormatterTest.cs +0 -0
- data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +0 -0
- data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +0 -0
- data/support/cs/test/OrderAttributeTest.cs +0 -0
- data/support/ruby/src/cauterize_ruby_baseclasses.rb +367 -367
- data/support/ruby/src/cauterize_ruby_builtins.rb +148 -148
- metadata +20 -20
@@ -1,98 +1,98 @@
|
|
1
|
-
# enumeration.rb
|
2
|
-
#
|
3
|
-
# Enumerations correspond exactly to C enumerations.
|
4
|
-
|
5
|
-
require 'set'
|
6
|
-
|
7
|
-
module Cauterize
|
8
|
-
module_function
|
9
|
-
def enumeration(name, desc=nil)
|
10
|
-
e = Cauterize.enumerations[name] || Cauterize.enumerations[name] = Enumeration.new(name, desc)
|
11
|
-
yield e if block_given?
|
12
|
-
return e
|
13
|
-
end
|
14
|
-
|
15
|
-
def enumeration!(name, desc=nil, &blk)
|
16
|
-
if Cauterize.enumerations[name]
|
17
|
-
raise Exception.new("Enumeration with name #{name} already exists.")
|
18
|
-
else
|
19
|
-
Cauterize.enumeration(name, desc, &blk)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def enumerations
|
24
|
-
@enumerations ||= {}
|
25
|
-
end
|
26
|
-
|
27
|
-
class EnumerationValue
|
28
|
-
attr_reader :name, :value
|
29
|
-
|
30
|
-
def initialize(name, value)
|
31
|
-
@name = name
|
32
|
-
@value = value
|
33
|
-
end
|
34
|
-
|
35
|
-
def value_hash(digest)
|
36
|
-
digest.update(@name.to_s)
|
37
|
-
digest.update(@value.to_s)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class Enumeration < BaseType
|
42
|
-
attr_reader :values
|
43
|
-
|
44
|
-
def initialize(name, desc=nil)
|
45
|
-
super
|
46
|
-
@values = {}
|
47
|
-
@value_id = 0
|
48
|
-
@used_ids = Set.new
|
49
|
-
end
|
50
|
-
|
51
|
-
def value(name, id=nil)
|
52
|
-
if @values[name]
|
53
|
-
raise Exception.new("Cannot duplicate name #{name}.")
|
54
|
-
end
|
55
|
-
|
56
|
-
next_id = value_id(id)
|
57
|
-
@used_ids << next_id
|
58
|
-
@values[name] = EnumerationValue.new(name, next_id)
|
59
|
-
end
|
60
|
-
|
61
|
-
def representation
|
62
|
-
Representation.enumRepresentation(
|
63
|
-
min: @values.values.map(&:value).min,
|
64
|
-
max: @values.values.map(&:value).max
|
65
|
-
)
|
66
|
-
end
|
67
|
-
|
68
|
-
protected
|
69
|
-
|
70
|
-
def local_hash(digest)
|
71
|
-
representation.type_hash(digest)
|
72
|
-
values.keys.sort.inject(digest) {|d, k|
|
73
|
-
values[k].value_hash(digest)
|
74
|
-
}
|
75
|
-
end
|
76
|
-
|
77
|
-
private
|
78
|
-
|
79
|
-
def value_id(next_id=nil)
|
80
|
-
if next_id
|
81
|
-
@value_id = next_id.to_i
|
82
|
-
end
|
83
|
-
|
84
|
-
v = @value_id
|
85
|
-
|
86
|
-
if @used_ids.include? v
|
87
|
-
raise Exception.new("Cannot duplicate constant #{v}.")
|
88
|
-
end
|
89
|
-
|
90
|
-
loop do
|
91
|
-
@value_id += 1
|
92
|
-
break unless @used_ids.include? @value_id
|
93
|
-
end
|
94
|
-
|
95
|
-
return v
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
1
|
+
# enumeration.rb
|
2
|
+
#
|
3
|
+
# Enumerations correspond exactly to C enumerations.
|
4
|
+
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
module Cauterize
|
8
|
+
module_function
|
9
|
+
def enumeration(name, desc=nil)
|
10
|
+
e = Cauterize.enumerations[name] || Cauterize.enumerations[name] = Enumeration.new(name, desc)
|
11
|
+
yield e if block_given?
|
12
|
+
return e
|
13
|
+
end
|
14
|
+
|
15
|
+
def enumeration!(name, desc=nil, &blk)
|
16
|
+
if Cauterize.enumerations[name]
|
17
|
+
raise Exception.new("Enumeration with name #{name} already exists.")
|
18
|
+
else
|
19
|
+
Cauterize.enumeration(name, desc, &blk)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def enumerations
|
24
|
+
@enumerations ||= {}
|
25
|
+
end
|
26
|
+
|
27
|
+
class EnumerationValue
|
28
|
+
attr_reader :name, :value
|
29
|
+
|
30
|
+
def initialize(name, value)
|
31
|
+
@name = name
|
32
|
+
@value = value
|
33
|
+
end
|
34
|
+
|
35
|
+
def value_hash(digest)
|
36
|
+
digest.update(@name.to_s)
|
37
|
+
digest.update(@value.to_s)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Enumeration < BaseType
|
42
|
+
attr_reader :values
|
43
|
+
|
44
|
+
def initialize(name, desc=nil)
|
45
|
+
super
|
46
|
+
@values = {}
|
47
|
+
@value_id = 0
|
48
|
+
@used_ids = Set.new
|
49
|
+
end
|
50
|
+
|
51
|
+
def value(name, id=nil)
|
52
|
+
if @values[name]
|
53
|
+
raise Exception.new("Cannot duplicate name #{name}.")
|
54
|
+
end
|
55
|
+
|
56
|
+
next_id = value_id(id)
|
57
|
+
@used_ids << next_id
|
58
|
+
@values[name] = EnumerationValue.new(name, next_id)
|
59
|
+
end
|
60
|
+
|
61
|
+
def representation
|
62
|
+
Representation.enumRepresentation(
|
63
|
+
min: @values.values.map(&:value).min,
|
64
|
+
max: @values.values.map(&:value).max
|
65
|
+
)
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
def local_hash(digest)
|
71
|
+
representation.type_hash(digest)
|
72
|
+
values.keys.sort.inject(digest) {|d, k|
|
73
|
+
values[k].value_hash(digest)
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def value_id(next_id=nil)
|
80
|
+
if next_id
|
81
|
+
@value_id = next_id.to_i
|
82
|
+
end
|
83
|
+
|
84
|
+
v = @value_id
|
85
|
+
|
86
|
+
if @used_ids.include? v
|
87
|
+
raise Exception.new("Cannot duplicate constant #{v}.")
|
88
|
+
end
|
89
|
+
|
90
|
+
loop do
|
91
|
+
@value_id += 1
|
92
|
+
break unless @used_ids.include? @value_id
|
93
|
+
end
|
94
|
+
|
95
|
+
return v
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
@@ -1,50 +1,50 @@
|
|
1
|
-
module Cauterize
|
2
|
-
module_function
|
3
|
-
|
4
|
-
def fixed_array(name, desc=nil)
|
5
|
-
a = Cauterize.fixed_arrays[name] || Cauterize.fixed_arrays[name] = FixedArray.new(name, desc)
|
6
|
-
yield a if block_given?
|
7
|
-
return a
|
8
|
-
end
|
9
|
-
|
10
|
-
def fixed_array!(name, &blk)
|
11
|
-
if Cauterize.fixed_arrays[name]
|
12
|
-
raise Exception.new("FixedArray with name #{name} already exists.")
|
13
|
-
else
|
14
|
-
Cauterize.fixed_array(name, &blk)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def fixed_arrays
|
19
|
-
@fixed_ararys ||= {}
|
20
|
-
end
|
21
|
-
|
22
|
-
class FixedArray < BaseType
|
23
|
-
def initialize(name, desc=nil)
|
24
|
-
super
|
25
|
-
end
|
26
|
-
|
27
|
-
def array_type(t = nil)
|
28
|
-
if t
|
29
|
-
@array_type = BaseType.find_type!(t)
|
30
|
-
else
|
31
|
-
@array_type
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def array_size(s = nil)
|
36
|
-
if s
|
37
|
-
@array_size = s
|
38
|
-
else
|
39
|
-
@array_size
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
protected
|
44
|
-
|
45
|
-
def local_hash(digest)
|
46
|
-
digest.update(@array_size.to_s)
|
47
|
-
@array_type.type_hash(digest)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
1
|
+
module Cauterize
|
2
|
+
module_function
|
3
|
+
|
4
|
+
def fixed_array(name, desc=nil)
|
5
|
+
a = Cauterize.fixed_arrays[name] || Cauterize.fixed_arrays[name] = FixedArray.new(name, desc)
|
6
|
+
yield a if block_given?
|
7
|
+
return a
|
8
|
+
end
|
9
|
+
|
10
|
+
def fixed_array!(name, &blk)
|
11
|
+
if Cauterize.fixed_arrays[name]
|
12
|
+
raise Exception.new("FixedArray with name #{name} already exists.")
|
13
|
+
else
|
14
|
+
Cauterize.fixed_array(name, &blk)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def fixed_arrays
|
19
|
+
@fixed_ararys ||= {}
|
20
|
+
end
|
21
|
+
|
22
|
+
class FixedArray < BaseType
|
23
|
+
def initialize(name, desc=nil)
|
24
|
+
super
|
25
|
+
end
|
26
|
+
|
27
|
+
def array_type(t = nil)
|
28
|
+
if t
|
29
|
+
@array_type = BaseType.find_type!(t)
|
30
|
+
else
|
31
|
+
@array_type
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def array_size(s = nil)
|
36
|
+
if s
|
37
|
+
@array_size = s
|
38
|
+
else
|
39
|
+
@array_size
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def local_hash(digest)
|
46
|
+
digest.update(@array_size.to_s)
|
47
|
+
@array_type.type_hash(digest)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/cauterize/formatter.rb
CHANGED
@@ -1,67 +1,67 @@
|
|
1
|
-
def default_formatter
|
2
|
-
Formatter.new(" ")
|
3
|
-
end
|
4
|
-
|
5
|
-
def four_space_formatter
|
6
|
-
Formatter.new(" ")
|
7
|
-
end
|
8
|
-
|
9
|
-
class Formatter
|
10
|
-
def initialize(indent_str)
|
11
|
-
@indent_level = 0
|
12
|
-
@indent_str = indent_str
|
13
|
-
@lines = []
|
14
|
-
end
|
15
|
-
|
16
|
-
def indent(line)
|
17
|
-
(@indent_str * @indent_level) + line
|
18
|
-
end
|
19
|
-
|
20
|
-
def <<(line)
|
21
|
-
@lines << indent(line)
|
22
|
-
end
|
23
|
-
|
24
|
-
# indent back one level
|
25
|
-
def backdent(line)
|
26
|
-
if 0 == @indent_level
|
27
|
-
@lines << line
|
28
|
-
else
|
29
|
-
@indent_level -= 1
|
30
|
-
@lines << indent(line)
|
31
|
-
@indent_level += 1
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def append(text)
|
36
|
-
if @lines.length == 0
|
37
|
-
@lines << ""
|
38
|
-
end
|
39
|
-
|
40
|
-
@lines[-1] += text
|
41
|
-
end
|
42
|
-
|
43
|
-
def braces
|
44
|
-
self << "{"
|
45
|
-
indented { yield self } if block_given?
|
46
|
-
self << "}"
|
47
|
-
end
|
48
|
-
|
49
|
-
def blank_line
|
50
|
-
@lines << ""
|
51
|
-
end
|
52
|
-
|
53
|
-
def to_s(extra_indent = 0)
|
54
|
-
@indent_level += extra_indent
|
55
|
-
s = @lines.map {|l| indent(l) }.join("\n")
|
56
|
-
@indent_level -= extra_indent
|
57
|
-
return s
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
61
|
-
|
62
|
-
def indented
|
63
|
-
@indent_level += 1
|
64
|
-
yield self
|
65
|
-
@indent_level -= 1
|
66
|
-
end
|
67
|
-
end
|
1
|
+
def default_formatter
|
2
|
+
Formatter.new(" ")
|
3
|
+
end
|
4
|
+
|
5
|
+
def four_space_formatter
|
6
|
+
Formatter.new(" ")
|
7
|
+
end
|
8
|
+
|
9
|
+
class Formatter
|
10
|
+
def initialize(indent_str)
|
11
|
+
@indent_level = 0
|
12
|
+
@indent_str = indent_str
|
13
|
+
@lines = []
|
14
|
+
end
|
15
|
+
|
16
|
+
def indent(line)
|
17
|
+
(@indent_str * @indent_level) + line
|
18
|
+
end
|
19
|
+
|
20
|
+
def <<(line)
|
21
|
+
@lines << indent(line)
|
22
|
+
end
|
23
|
+
|
24
|
+
# indent back one level
|
25
|
+
def backdent(line)
|
26
|
+
if 0 == @indent_level
|
27
|
+
@lines << line
|
28
|
+
else
|
29
|
+
@indent_level -= 1
|
30
|
+
@lines << indent(line)
|
31
|
+
@indent_level += 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def append(text)
|
36
|
+
if @lines.length == 0
|
37
|
+
@lines << ""
|
38
|
+
end
|
39
|
+
|
40
|
+
@lines[-1] += text
|
41
|
+
end
|
42
|
+
|
43
|
+
def braces
|
44
|
+
self << "{"
|
45
|
+
indented { yield self } if block_given?
|
46
|
+
self << "}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def blank_line
|
50
|
+
@lines << ""
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_s(extra_indent = 0)
|
54
|
+
@indent_level += extra_indent
|
55
|
+
s = @lines.map {|l| indent(l) }.join("\n")
|
56
|
+
@indent_level -= extra_indent
|
57
|
+
return s
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def indented
|
63
|
+
@indent_level += 1
|
64
|
+
yield self
|
65
|
+
@indent_level -= 1
|
66
|
+
end
|
67
|
+
end
|
data/lib/cauterize/group.rb
CHANGED
@@ -1,81 +1,81 @@
|
|
1
|
-
module Cauterize
|
2
|
-
module_function
|
3
|
-
|
4
|
-
def group(name, desc=nil)
|
5
|
-
a = Cauterize.groups[name] || Cauterize.groups[name] = Group.new(name, desc)
|
6
|
-
yield a if block_given?
|
7
|
-
return a
|
8
|
-
end
|
9
|
-
|
10
|
-
def group!(name, &blk)
|
11
|
-
if Cauterize.groups[name]
|
12
|
-
raise Exception.new("Group with name #{name} already exists.")
|
13
|
-
else
|
14
|
-
Cauterize.group(name, &blk)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def groups
|
19
|
-
@groups ||= {}
|
20
|
-
end
|
21
|
-
|
22
|
-
class GroupField
|
23
|
-
attr_reader :name, :type, :description
|
24
|
-
|
25
|
-
def initialize(name, type, desc=nil)
|
26
|
-
@name = name
|
27
|
-
@type = BaseType.find_type!(type) if type
|
28
|
-
@description = desc
|
29
|
-
end
|
30
|
-
|
31
|
-
def field_hash(digest)
|
32
|
-
digest.update(@name.to_s)
|
33
|
-
if @type
|
34
|
-
@type.type_hash(digest)
|
35
|
-
else
|
36
|
-
digest
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
class Group < BaseType
|
42
|
-
attr_reader :fields, :tag_enum
|
43
|
-
|
44
|
-
def initialize(name, desc=nil)
|
45
|
-
# This technically should be defined before the group itself is defined
|
46
|
-
# because the group depends on the enumeration. Technically, this snippet
|
47
|
-
# of code doesn't care, but others may depend on the ordering.
|
48
|
-
@tag_enum = Cauterize.enumeration!("group_#{name}_type".to_sym)
|
49
|
-
|
50
|
-
# Make sure that this is called *AFTER* the enumeration is created.
|
51
|
-
super
|
52
|
-
@fields = {}
|
53
|
-
end
|
54
|
-
|
55
|
-
def field(name, type, desc=nil)
|
56
|
-
if @fields[name]
|
57
|
-
raise Exception.new("Field name #{name} already used.")
|
58
|
-
else
|
59
|
-
@fields[name] = GroupField.new(name, type, desc)
|
60
|
-
@tag_enum.value(enum_sym(name))
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def dataless(name, desc=nil)
|
65
|
-
field(name, nil, desc)
|
66
|
-
end
|
67
|
-
|
68
|
-
def enum_sym(fname)
|
69
|
-
"group_#{@name}_type_#{fname}".up_snake.to_sym
|
70
|
-
end
|
71
|
-
|
72
|
-
protected
|
73
|
-
|
74
|
-
def local_hash(digest)
|
75
|
-
@tag_enum.type_hash(digest)
|
76
|
-
@fields.keys.inject(digest) {|d, k|
|
77
|
-
@fields[k].field_hash(digest)
|
78
|
-
}
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
1
|
+
module Cauterize
|
2
|
+
module_function
|
3
|
+
|
4
|
+
def group(name, desc=nil)
|
5
|
+
a = Cauterize.groups[name] || Cauterize.groups[name] = Group.new(name, desc)
|
6
|
+
yield a if block_given?
|
7
|
+
return a
|
8
|
+
end
|
9
|
+
|
10
|
+
def group!(name, &blk)
|
11
|
+
if Cauterize.groups[name]
|
12
|
+
raise Exception.new("Group with name #{name} already exists.")
|
13
|
+
else
|
14
|
+
Cauterize.group(name, &blk)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def groups
|
19
|
+
@groups ||= {}
|
20
|
+
end
|
21
|
+
|
22
|
+
class GroupField
|
23
|
+
attr_reader :name, :type, :description
|
24
|
+
|
25
|
+
def initialize(name, type, desc=nil)
|
26
|
+
@name = name
|
27
|
+
@type = BaseType.find_type!(type) if type
|
28
|
+
@description = desc
|
29
|
+
end
|
30
|
+
|
31
|
+
def field_hash(digest)
|
32
|
+
digest.update(@name.to_s)
|
33
|
+
if @type
|
34
|
+
@type.type_hash(digest)
|
35
|
+
else
|
36
|
+
digest
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Group < BaseType
|
42
|
+
attr_reader :fields, :tag_enum
|
43
|
+
|
44
|
+
def initialize(name, desc=nil)
|
45
|
+
# This technically should be defined before the group itself is defined
|
46
|
+
# because the group depends on the enumeration. Technically, this snippet
|
47
|
+
# of code doesn't care, but others may depend on the ordering.
|
48
|
+
@tag_enum = Cauterize.enumeration!("group_#{name}_type".to_sym)
|
49
|
+
|
50
|
+
# Make sure that this is called *AFTER* the enumeration is created.
|
51
|
+
super
|
52
|
+
@fields = {}
|
53
|
+
end
|
54
|
+
|
55
|
+
def field(name, type, desc=nil)
|
56
|
+
if @fields[name]
|
57
|
+
raise Exception.new("Field name #{name} already used.")
|
58
|
+
else
|
59
|
+
@fields[name] = GroupField.new(name, type, desc)
|
60
|
+
@tag_enum.value(enum_sym(name))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def dataless(name, desc=nil)
|
65
|
+
field(name, nil, desc)
|
66
|
+
end
|
67
|
+
|
68
|
+
def enum_sym(fname)
|
69
|
+
"group_#{@name}_type_#{fname}".up_snake.to_sym
|
70
|
+
end
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
def local_hash(digest)
|
75
|
+
@tag_enum.type_hash(digest)
|
76
|
+
@fields.keys.inject(digest) {|d, k|
|
77
|
+
@fields[k].field_hash(digest)
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|