blob: 95c60f5727269d43e09ece61900da0c07de17bd9 (
plain)
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
|
#!/usr/bin/env ruby
$: << ".." # Include .. in load path
require 'cppgen'
class MethodHolderGen < CppGen
def initialize(outdir, amqp)
super(outdir, amqp)
@namespace="qpid::framing"
@classname="MethodHolder"
@filename="qpid/framing/MethodHolder"
end
def gen_max_size()
# Generate program to generate MaxSize.h
cpp_file("generate_#{@classname}MaxSize_h") {
@amqp.methods_.each { |m| include "qpid/framing/#{m.body_name}" }
genl
include "<algorithm>"
include "<fstream>"
genl
genl "using namespace std;"
genl "using namespace qpid::framing;"
genl
scope("int main(int, char** argv) {") {
genl "size_t maxSize=0;"
@amqp.methods_.each { |m|
genl "maxSize=max(maxSize, sizeof(#{m.body_name}));" }
gen <<EOS
ofstream out("#{@filename}MaxSize.h");
out << "// GENERATED CODE: generated by " << argv[0] << endl;
out << "namespace qpid{ namespace framing { " << endl;
out << "const size_t MAX_METHODBODY_SIZE=" << maxSize << ";" << endl;
out << "}}" << endl;
EOS
}
}
end
def gen_construct
cpp_file(@filename+"_construct") {
include @filename
include "qpid/framing/MethodBodyConstVisitor.h"
@amqp.methods_.each { |m| include "qpid/framing/#{m.body_name}" }
genl
include "qpid/Exception.h"
genl
namespace(@namespace) {
# construct function
scope("void #{@classname}::construct(ClassId c, MethodId m) {") {
scope("switch (c) {") {
@amqp.classes.each { |c|
scope("case #{c.index}: switch(m) {") {
c.methods_.each { |m|
genl "case #{m.index}: blob.construct(in_place<#{m.body_name}>()); break;"
}
genl "default: throw Exception(QPID_MSG(\"Invalid method id \" << m << \" for class #{c.name} \"));"
}
genl "break;"
}
genl "default: throw Exception(QPID_MSG(\"Invalid class id \" << c));"
}
}
# CopyVisitor
struct("#{@classname}::CopyVisitor", "public MethodBodyConstVisitor") { genl "MethodHolder& holder;"
genl "CopyVisitor(MethodHolder& h) : holder(h) {}"
@amqp.methods_.each { |m|
genl "void visit(const #{m.body_name}& x) { holder.blob=x; }"
}
}
genl
# operator=
scope("#{@classname}& MethodHolder::operator=(const AMQMethodBody& m) {") {
genl "CopyVisitor cv(*this); m.accept(cv); return *this;"
}
}}
end
def generate
gen_max_size
gen_construct
end
end
MethodHolderGen.new(Outdir, Amqp).generate();
|