diff options
| author | Alan Conway <aconway@apache.org> | 2008-02-27 16:37:48 +0000 |
|---|---|---|
| committer | Alan Conway <aconway@apache.org> | 2008-02-27 16:37:48 +0000 |
| commit | 0731e05211ff5e47e6a8b2c006bd6012da5cc161 (patch) | |
| tree | c30b793e7db0471caf9cc9b2226fdf20a353c8f9 /cpp/rubygen/99-0 | |
| parent | 178e1ad48948e5818ee3922cb73e515f581557d6 (diff) | |
| download | qpid-python-0731e05211ff5e47e6a8b2c006bd6012da5cc161.tar.gz | |
Generate code for both 0-99 preview and 0-10 final specs .
0-10 final: extended code generation and non-generated support classes.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@631638 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/rubygen/99-0')
| -rwxr-xr-x | cpp/rubygen/99-0/MethodBodyConstVisitor.rb | 27 | ||||
| -rwxr-xr-x | cpp/rubygen/99-0/MethodBodyDefaultVisitor.rb | 35 | ||||
| -rwxr-xr-x | cpp/rubygen/99-0/MethodHolder.rb | 100 | ||||
| -rwxr-xr-x | cpp/rubygen/99-0/Operations.rb | 96 | ||||
| -rwxr-xr-x | cpp/rubygen/99-0/OperationsInvoker.rb | 92 | ||||
| -rwxr-xr-x | cpp/rubygen/99-0/Proxy.rb | 82 | ||||
| -rw-r--r-- | cpp/rubygen/99-0/Session.rb | 195 | ||||
| -rwxr-xr-x | cpp/rubygen/99-0/all_method_bodies.rb | 21 | ||||
| -rwxr-xr-x | cpp/rubygen/99-0/constants.rb | 82 | ||||
| -rw-r--r-- | cpp/rubygen/99-0/frame_body_lists.rb | 31 | ||||
| -rw-r--r-- | cpp/rubygen/99-0/structs.rb | 538 |
11 files changed, 1299 insertions, 0 deletions
diff --git a/cpp/rubygen/99-0/MethodBodyConstVisitor.rb b/cpp/rubygen/99-0/MethodBodyConstVisitor.rb new file mode 100755 index 0000000000..f9ef95f5a0 --- /dev/null +++ b/cpp/rubygen/99-0/MethodBodyConstVisitor.rb @@ -0,0 +1,27 @@ +#!/usr/bin/env ruby +$: << ".." # Include .. in load path +require 'cppgen' + +class MethodBodyConstVisitorGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + @namespace="qpid::framing" + @classname="MethodBodyConstVisitor" + @filename="qpid/framing/MethodBodyConstVisitor" + end + + def generate() + h_file("#{@filename}") { + namespace(@namespace) { + @amqp.methods_.each { |m| genl "class #{m.body_name};" } + cpp_class("MethodBodyConstVisitor") { + genl "public:" + genl "virtual ~MethodBodyConstVisitor() {}" + @amqp.methods_.each { |m| genl "virtual void visit(const #{m.body_name}&) = 0;" } + }}} + end +end + +MethodBodyConstVisitorGen.new($outdir, $amqp).generate(); + diff --git a/cpp/rubygen/99-0/MethodBodyDefaultVisitor.rb b/cpp/rubygen/99-0/MethodBodyDefaultVisitor.rb new file mode 100755 index 0000000000..a74b0c06d6 --- /dev/null +++ b/cpp/rubygen/99-0/MethodBodyDefaultVisitor.rb @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby +$: << ".." # Include .. in load path +require 'cppgen' + +class MethodBodyDefaultVisitorGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + @namespace, @classname, @filename = parse_classname("qpid::framing::MethodBodyDefaultVisitor") + end + + def generate() + h_file(@filename) { + include "qpid/framing/MethodBodyConstVisitor" + namespace(@namespace) { + genl "class AMQMethodBody;" + cpp_class(@classname, "public MethodBodyConstVisitor") { + genl "public:" + genl "virtual void defaultVisit(const AMQMethodBody&) = 0;" + @amqp.methods_.each { |m| + genl "virtual void visit(const #{m.body_name}&);" } + }}} + + cpp_file(@filename) { + include(@filename) + include("all_method_bodies.h") + namespace(@namespace) { + @amqp.methods_.each { |m| + genl "void #{@classname}::visit(const #{m.body_name}& b) { defaultVisit(b); }" + }}} + end +end + +MethodBodyDefaultVisitorGen.new($outdir, $amqp).generate(); + diff --git a/cpp/rubygen/99-0/MethodHolder.rb b/cpp/rubygen/99-0/MethodHolder.rb new file mode 100755 index 0000000000..a708db6676 --- /dev/null +++ b/cpp/rubygen/99-0/MethodHolder.rb @@ -0,0 +1,100 @@ +#!/usr/bin/env ruby +$: << ".." # Include .. in load path +require 'cppgen' + +class MethodHolderGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + @namespace="qpid::framing" + @classname="BodyHolder" + @filename="qpid/framing/BodyHolder" + end + + def gen_max_size() + # Generate program to generate MaxSize.h + cpp_file("generate_MaxMethodBodySize_h") { + include "qpid/framing/AMQHeaderBody" + include "qpid/framing/AMQContentBody" + include "qpid/framing/AMQHeartbeatBody" + @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;" + genl "maxSize=max(maxSize, sizeof(AMQHeaderBody));" + genl "maxSize=max(maxSize, sizeof(AMQContentBody));" + genl "maxSize=max(maxSize, sizeof(AMQHeartbeatBody));" + @amqp.methods_.each { |m| + genl "maxSize=max(maxSize, sizeof(#{m.body_name}));" } + gen <<EOS +ofstream out("qpid/framing/MaxMethodBodySize.h"); +out << "// GENERATED CODE: generated by " << argv[0] << endl; +out << "namespace qpid{ namespace framing { " << endl; +out << "const size_t MAX_METHOD_BODY_SIZE=" << maxSize << ";" << endl; +out << "}}" << endl; +EOS + } + } + end + + def gen_construct + cpp_file(@filename+"_gen") { + include @filename + include "qpid/framing/AMQHeaderBody" + include "qpid/framing/AMQContentBody" + include "qpid/framing/AMQHeartbeatBody" + @amqp.methods_.each { |m| include "qpid/framing/#{m.body_name}" } + include "qpid/framing/FrameDefaultVisitor.h" + include "qpid/Exception.h" + genl + namespace(@namespace) { + scope("void #{@classname}::setMethod(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 = in_place<#{m.body_name}>(); break;" + } + genl "default: throw Exception(QPID_MSG(\"Invalid method id \" << int(m) << \" for class #{c.name} \"));" + } + genl "break;" + } + genl "default: throw Exception(QPID_MSG(\"Invalid class id \" << int(c)));" + } + } + + struct("CopyVisitor", "public FrameDefaultVisitor") { + genl "using FrameDefaultVisitor::visit;" + genl "using FrameDefaultVisitor::defaultVisit;" + genl "BodyHolder& holder;" + genl "CopyVisitor(BodyHolder& h) : holder(h) {}" + ["Header", "Content", "Heartbeat"].each { |type| + genl "void visit(const AMQ#{type}Body& x) { holder=x; }" + } + @amqp.methods_.each { |m| + genl "void visit(const #{m.body_name}& x) { holder=x; }" + } + genl "void defaultVisit(const AMQBody&) { assert(0); }" + } + genl + + scope("void BodyHolder::setBody(const AMQBody& b) {") { + genl "CopyVisitor cv(*this); b.accept(cv);" + } + }} + end + + def generate + gen_max_size + gen_construct + end +end + +MethodHolderGen.new($outdir, $amqp).generate(); + diff --git a/cpp/rubygen/99-0/Operations.rb b/cpp/rubygen/99-0/Operations.rb new file mode 100755 index 0000000000..c985bb6105 --- /dev/null +++ b/cpp/rubygen/99-0/Operations.rb @@ -0,0 +1,96 @@ +#!/usr/bin/env ruby +# Usage: output_directory xml_spec_file [xml_spec_file...] +# +$: << '..' +require 'cppgen' +require 'fileutils' +require 'etc' +require 'pathname' + +class OperationsGen < CppGen + + def initialize(chassis, outdir, amqp) + super(outdir, amqp) + @chassis=chassis + @classname="AMQP_#{@chassis.caps}Operations" + end + + def handler_method (m) + return_type = m.result ? m.result.cpptype.ret : "void" + gen "\nvirtual #{return_type} #{m.cppname}(" + gen m.signature.join(",\n") + gen ") = 0;\n" + end + + def handler_classname(c) c.name.caps+"Handler"; end + + def handler_class(c) + if (!c.methods_on(@chassis).empty?) + handlerclass=handler_classname c + gen <<EOS +// ==================== class #{handlerclass} ==================== +class #{handlerclass} { + // Constructors and destructors + public: + class Invoker; // Declared in #{@chassis.caps}Invoker + + #{handlerclass}(){}; + virtual ~#{handlerclass}() {} + // Protocol methods +EOS + c.methods_on(@chassis).each { |m| handler_method(m) if !m.content() } + gen <<EOS +}; // class #{handlerclass} + + +EOS + end + end + + def handler_get(c) + if (!c.methods_on(@chassis).empty?) + handlerclass=handler_classname c + gen "virtual #{handlerclass}* get#{handlerclass}() = 0;\n" + end + end + + def generate() + h_file("qpid/framing/#{@classname}.h") { + gen <<EOS +#include <sstream> +#include "qpid/framing/ProtocolVersion.h" +#include "qpid/framing/amqp_structs.h" + +namespace qpid { +namespace framing { + +class AMQMethodBody; + +class #{@classname} { + public: + class Invoker; // Declared in #{@chassis.caps}Invoker + + virtual ~#{@classname}() {} + + virtual ProtocolVersion getVersion() const = 0; + + // Inner classes +EOS + indent { @amqp.classes.each { |c| handler_class(c) } } + gen <<EOS + + // Method handler get methods + +EOS + indent { @amqp.classes.each { |c| handler_get(c) } } + gen <<EOS +}; /* class #{@classname} */ +}} +EOS +} + end +end + +OperationsGen.new("client",ARGV[0], $amqp).generate() +OperationsGen.new("server",ARGV[0], $amqp).generate() + diff --git a/cpp/rubygen/99-0/OperationsInvoker.rb b/cpp/rubygen/99-0/OperationsInvoker.rb new file mode 100755 index 0000000000..642f98ce8e --- /dev/null +++ b/cpp/rubygen/99-0/OperationsInvoker.rb @@ -0,0 +1,92 @@ +#!/usr/bin/env ruby +# Usage: output_directory xml_spec_file [xml_spec_file...] +# +$: << '..' +require 'cppgen' + +class OperationsInvokerGen < CppGen + def initialize(chassis, outdir, amqp) + super(outdir, amqp) + @chassis=chassis + @ops="AMQP_#{@chassis.caps}Operations" + @classname="#{@ops}::Invoker" + @filename="qpid/framing/#{@chassis.caps}Invoker" + end + + def handler(c) "#{@ops}::#{c.cppname}Handler"; end + def getter(c) "get#{c.cppname}Handler"; end + def invoker(c) "#{handler(c)}::Invoker"; end + def visit_methods(c) c.methods_on(@chassis).select { |m| !m.content } end + + def handler_visits_cpp(c) + visit_methods(c).each { |m| + scope("void #{invoker(c)}::visit(const #{m.body_name}& body) {") { + if (m.result) + genl "this->encode(body.invoke(target), result.result);" + else + genl "body.invoke(target);" + end + genl "result.handled=true;" + } + } + end + + def ops_visits_cpp() + @amqp.classes.each { |c| + visit_methods(c).each { |m| + scope("void #{@classname}::visit(const #{m.body_name}& body) {") { + genl "#{handler(c)}::Invoker invoker(*target.#{getter(c)}());" + genl "body.accept(invoker);" + genl "result=invoker.getResult();" + } + } + } + end + + def invoker_h(invoker, target, methods) + return if methods.empty? + genl + cpp_class(invoker, "public qpid::framing::Invoker") { + genl "#{target}& target;" + public + genl("Invoker(#{target}& target_) : target(target_) {}") + genl "using MethodBodyDefaultVisitor::visit;" + methods.each { |m| genl "void visit(const #{m.body_name}& body);" } + } + end + + def generate() + h_file(@filename) { + include "qpid/framing/#{@ops}" + include "qpid/framing/Invoker.h" + namespace("qpid::framing") { + # AMQP_*Operations invoker. + methods=@amqp.classes.map { |c| visit_methods(c).to_a }.flatten + invoker_h(@classname, @ops, methods) + + # AMQP_*Operations::*Handler invokers. + @amqp.classes.each { |c| + invoker_h(invoker(c), handler(c), visit_methods(c)) + } + } + } + + cpp_file(@filename) { + include @filename + @amqp.classes.each { |c| + visit_methods(c).each { |m| + include "qpid/framing/#{m.body_name}" + }} + namespace("qpid::framing") { + ops_visits_cpp + @amqp.classes.each { |c| + next if visit_methods(c).empty? + handler_visits_cpp(c) + } + } + } + end +end + +OperationsInvokerGen.new("client",ARGV[0], $amqp).generate() +OperationsInvokerGen.new("server",ARGV[0], $amqp).generate() diff --git a/cpp/rubygen/99-0/Proxy.rb b/cpp/rubygen/99-0/Proxy.rb new file mode 100755 index 0000000000..2829884673 --- /dev/null +++ b/cpp/rubygen/99-0/Proxy.rb @@ -0,0 +1,82 @@ +#!/usr/bin/env ruby +$: << ".." # Include .. in load path +require 'cppgen' + +class ProxyGen < CppGen + + def initialize(chassis, outdir, amqp) + super(outdir, amqp) + @chassis=chassis + @classname="AMQP_#{@chassis.caps}Proxy" + @filename="qpid/framing/#{@classname}" + end + + def proxy_member(c) c.name.lcaps+"Proxy"; end + + def inner_class_decl(c) + cname=c.name.caps + cpp_class(cname, "Proxy") { + gen <<EOS +public: +#{cname}(FrameHandler& f) : Proxy(f) {} +static #{cname}& get(#{@classname}& proxy) { return proxy.get#{cname}(); } +EOS + c.methods_on(@chassis).each { |m| + genl "virtual void #{m.cppname}(#{m.signature.join(",\n ")});" + genl + }} + end + + def inner_class_defn(c) + cname=c.cppname + c.methods_on(@chassis).each { |m| + genl "void #{@classname}::#{cname}::#{m.cppname}(#{m.signature.join(", ")})" + scope { + params=(["getVersion()"]+m.param_names).join(", ") + genl "send(#{m.body_name}(#{params}));" + }} + end + + def generate + # .h file + h_file(@filename) { + include "qpid/framing/Proxy.h" + include "qpid/framing/amqp_types.h" + namespace("qpid::framing") { + cpp_class(@classname, "public Proxy") { + public + genl "#{@classname}(FrameHandler& out);" + genl + @amqp.classes.each { |c| + inner_class_decl(c) + genl + genl "#{c.cppname}& get#{c.cppname}() { return #{proxy_member(c)}; }" + genl + } + private + @amqp.classes.each{ |c| gen c.cppname+" "+proxy_member(c)+";\n" } + }}} + + # .cpp file + cpp_file(@filename) { + include "<sstream>" + include "#{@classname}.h" + include "qpid/framing/amqp_types_full.h" + @amqp.methods_on(@chassis).each { + |m| include "qpid/framing/"+m.body_name + } + genl + namespace("qpid::framing") { + genl "#{@classname}::#{@classname}(FrameHandler& f) :" + gen " Proxy(f)" + @amqp.classes.each { |c| gen ",\n "+proxy_member(c)+"(f)" } + genl "{}\n" + @amqp.classes.each { |c| inner_class_defn(c) } + }} + end +end + + +ProxyGen.new("client", $outdir, $amqp).generate; +ProxyGen.new("server", $outdir, $amqp).generate; + diff --git a/cpp/rubygen/99-0/Session.rb b/cpp/rubygen/99-0/Session.rb new file mode 100644 index 0000000000..e01a28a62d --- /dev/null +++ b/cpp/rubygen/99-0/Session.rb @@ -0,0 +1,195 @@ +#!/usr/bin/env ruby +# Usage: output_directory xml_spec_file [xml_spec_file...] +# +$: << '..' +require 'cppgen' + +class CppGen + def session_methods + excludes = ["channel", "connection", "session", "execution"] + gen_methods=@amqp.methods_on(@chassis).reject { |m| + excludes.include? m.parent.name + } + end + + def doxygen(m) + doxygen_comment { + genl m.doc + genl + m.fields_c.each { |f| + genl "@param #{f.cppname}" + genl f.doc if f.doc + genl + } + } + end +end + +class ContentField # For extra content parameters + def cppname() "content" end + def signature() "const MethodContent& content" end + def sig_default() signature+"="+"DefaultContent(std::string())" end + def unpack() "p[arg::content|DefaultContent(std::string())]"; end + def doc() "Message content"; end +end + +class AmqpField + def unpack() "p[arg::#{cppname}|#{cpptype.default_value}]"; end + def sig_default() signature+"="+cpptype.default_value; end +end + +class AmqpMethod + def fields_c() content ? fields+[ContentField.new] : fields end + def param_names_c() fields_c.map { |f| f.cppname} end + def signature_c() fields_c.map { |f| f.signature }; end + def sig_c_default() fields_c.map { |f| f.sig_default }; end + def argpack_name() "#{parent.cppname}#{name.caps}Parameters"; end + def argpack_type() + "boost::parameter::parameters<" + + fields_c.map { |f| "arg::keyword_tags::"+f.cppname }.join(',') + + ">" + end + def return_type() + return "TypedResult<qpid::framing::#{result.cpptype.ret}>" if (result) + return "Response" if (not responses().empty?) + return "Completion" + end + def session_function() "#{parent.name.lcaps}#{name.caps}"; end +end + +class SessionNoKeywordGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + @chassis="server" + @namespace,@classname,@file= + parse_classname "qpid::client::no_keyword::Session_#{@amqp.version.bars}" + end + + def generate() + h_file(@file) { + include "qpid/client/SessionBase.h" + + namespace("qpid::client") { + genl "using std::string;" + genl "using framing::Content;" + genl "using framing::FieldTable;" + genl "using framing::MethodContent;" + genl "using framing::SequenceNumberSet;" + genl "using framing::Uuid;" + genl + namespace("no_keyword") { + doxygen_comment { + genl "AMQP #{@amqp.version} session API." + genl @amqp.class_("session").doc + } + cpp_class(@classname, "public SessionBase") { + public + genl "Session_#{@amqp.version.bars}() {}" + genl "Session_#{@amqp.version.bars}(shared_ptr<SessionCore> core) : SessionBase(core) {}" + session_methods.each { |m| + genl + doxygen(m) + args=m.sig_c_default.join(", ") + genl "#{m.return_type} #{m.session_function}(#{args});" + } + }}}} + + cpp_file(@file) { + include @classname + include "qpid/framing/all_method_bodies.h" + namespace(@namespace) { + genl "using namespace framing;" + session_methods.each { |m| + genl + sig=m.signature_c.join(", ") + func="#{@classname}::#{m.session_function}" + scope("#{m.return_type} #{func}(#{sig}) {") { + args=(["ProtocolVersion()"]+m.param_names).join(", ") + body="#{m.body_name}(#{args})" + sendargs=body + sendargs << ", content" if m.content + genl "return #{m.return_type}(impl->send(#{sendargs}), impl);" + }}}} + end +end + +class SessionGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + @chassis="server" + session="Session_#{@amqp.version.bars}" + @base="no_keyword::#{session}" + @fqclass=FqClass.new "qpid::client::#{session}" + @classname=@fqclass.name + @fqbase=FqClass.new("qpid::client::#{@base}") + end + + def gen_keyword_decl(m, prefix) + return if m.fields_c.empty? # Inherited function will do. + scope("BOOST_PARAMETER_MEMFUN(#{m.return_type}, #{m.session_function}, 0, #{m.fields_c.size}, #{m.argpack_name}) {") { + scope("return #{prefix}#{m.session_function}(",");") { + gen m.fields_c.map { |f| f.unpack() }.join(",\n") + } + } + genl + end + + def generate() + keyword_methods=session_methods.reject { |m| m.fields_c.empty? } + max_arity = keyword_methods.map{ |m| m.fields_c.size }.max + + h_file(@fqclass.file) { + include @fqbase.file + genl + genl "#define BOOST_PARAMETER_MAX_ARITY #{max_arity}" + include "<boost/parameter.hpp>" + genl + namespace("qpid::client") { + # Generate keyword tag declarations. + namespace("arg") { + keyword_methods.map{ |m| m.param_names_c }.flatten.uniq.each { |k| + genl "BOOST_PARAMETER_KEYWORD(keyword_tags, #{k})" + }} + genl + # Doxygen comment. + doxygen_comment { + genl "AMQP #{@amqp.version} session API with keyword arguments." + genl <<EOS +This class provides the same set of functions as #{@base}, but also +allows parameters be passed using keywords. The keyword is the +parameter name in the namespace "arg". + +For example given the normal function "foo(int x=0, int y=0, int z=0)" +you could call it in either of the following ways: + +@code +session.foo(1,2,3); // Normal no keywords +session.foo(arg::z=3, arg::x=1); // Keywords and a default +@endcode + +The keyword functions are easy to use but their declarations are hard +to read. You may find it easier to read the documentation for #{@base} +which provides the same set of functions using normal non-keyword +declarations. + +\\ingroup clientapi +EOS + } + # Session class. + cpp_class(@classname,"public #{@base}") { + private + genl "#{@classname}(shared_ptr<SessionCore> core) : #{ @base}(core) {}" + keyword_methods.each { |m| typedef m.argpack_type, m.argpack_name } + genl "friend class Connection;" + public + genl "#{@classname}() {}" + keyword_methods.each { |m| gen_keyword_decl(m,@base+"::") } + }}} + end +end + +SessionNoKeywordGen.new(ARGV[0], $amqp).generate() +SessionGen.new(ARGV[0], $amqp).generate() + diff --git a/cpp/rubygen/99-0/all_method_bodies.rb b/cpp/rubygen/99-0/all_method_bodies.rb new file mode 100755 index 0000000000..5971d49189 --- /dev/null +++ b/cpp/rubygen/99-0/all_method_bodies.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby +$: << ".." # Include .. in load path +require 'cppgen' + +class AllMethodBodiesGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + @namespace="qpid::framing" + @filename="qpid/framing/all_method_bodies" + end + + def generate() + h_file(@filename) { + @amqp.methods_.each { |m| include "qpid/framing/"+m.body_name } + } + end +end + +AllMethodBodiesGen.new($outdir, $amqp).generate(); + diff --git a/cpp/rubygen/99-0/constants.rb b/cpp/rubygen/99-0/constants.rb new file mode 100755 index 0000000000..b5f559d504 --- /dev/null +++ b/cpp/rubygen/99-0/constants.rb @@ -0,0 +1,82 @@ +#!/usr/bin/env ruby +$: << ".." # Include .. in load path +require 'cppgen' + +class ConstantsGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + @namespace="qpid::framing" + @dir="qpid/framing" + end + + def constants_h() + h_file("#{@dir}/constants") { + namespace(@namespace) { + scope("enum AmqpConstant {","};") { + l=[] + l.concat @amqp.constants.map { |c| "#{c.name.shout}=#{c.value}" } + @amqp.classes.each { |c| + l << "#{c.name.shout}_CLASS_ID=#{c.index}" + l.concat c.methods_.map { |m| + "#{c.name.shout}_#{m.name.shout}_METHOD_ID=#{m.index}" } + } + genl l.join(",\n") + }}} + end + + def exbase(c) + case c.class_ + when "soft-error" then "ChannelException" + when "hard-error" then "ConnectionException" + end + end + + def reply_exceptions_h() + h_file("#{@dir}/reply_exceptions") { + include "qpid/Exception" + namespace(@namespace) { + @amqp.constants.each { |c| + base = exbase c + if base + genl + struct(c.name.caps+"Exception", base) { + genl "#{c.name.caps}Exception(const std::string& msg=std::string()) : #{base}(#{c.value}, \"#{c.name}: \"+msg) {}" + } + end + } + genl + genl "void throwReplyException(int code, const std::string& text);" + } + } + end + + def reply_exceptions_cpp() + cpp_file("#{@dir}/reply_exceptions") { + include "#{@dir}/reply_exceptions" + include "<sstream>" + namespace("qpid::framing") { + scope("void throwReplyException(int code, const std::string& text) {"){ + scope("switch (code) {") { + genl "case 200: break; // No exception" + @amqp.constants.each { |c| + if exbase c + genl "case #{c.value}: throw #{c.name.caps}Exception(text);" + end + } + scope("default:","") { + genl "std::ostringstream msg;" + genl 'msg << "Invalid reply code " << code << ": " << text;' + genl 'throw InvalidArgumentException(msg.str());' + }}}}} + end + + def generate() + constants_h + reply_exceptions_h + reply_exceptions_cpp + end +end + +ConstantsGen.new($outdir, $amqp).generate(); + diff --git a/cpp/rubygen/99-0/frame_body_lists.rb b/cpp/rubygen/99-0/frame_body_lists.rb new file mode 100644 index 0000000000..b20e4550f3 --- /dev/null +++ b/cpp/rubygen/99-0/frame_body_lists.rb @@ -0,0 +1,31 @@ +$: << ".." # Include .. in load path +require 'cppgen' + +class FrameBodyListsGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp); + end + + def generate + h_file("qpid/framing/frame_body_lists.h") { + gen <<EOS +/**@file + * Macro lists of frame body classes, used to generate Visitors + */ +EOS + gen "#define METHOD_BODIES() " + @amqp.methods_.each { |m| gen "\\\n (#{m.body_name}) " } + gen <<EOS + + +#define OTHER_BODIES() (AMQContentBody)(AMQHeaderBody)(AMQHeartbeatBody)) + +EOS + } + end +end + +FrameBodyListsGen.new(ARGV[0], $amqp).generate; + + diff --git a/cpp/rubygen/99-0/structs.rb b/cpp/rubygen/99-0/structs.rb new file mode 100644 index 0000000000..336591be00 --- /dev/null +++ b/cpp/rubygen/99-0/structs.rb @@ -0,0 +1,538 @@ +#!/usr/bin/env ruby +# Usage: output_directory xml_spec_file [xml_spec_file...] +# +$: << '..' +require 'cppgen' + +class StructGen < CppGen + + def initialize(outdir, amqp) + super(outdir, amqp) + end + + EncodingMap={ + "octet"=>"Octet", + "short"=>"Short", + "long"=>"Long", + "longlong"=>"LongLong", + "longstr"=>"LongString", + "shortstr"=>"ShortString", + "timestamp"=>"LongLong", + "table"=>"FieldTable", + "content"=>"Content", + "long-struct"=>"LongString" + } + SizeMap={ + "octet"=>1, + "short"=>2, + "long"=>4, + "longlong"=>8, + "timestamp"=>8 + } + + ValueTypes=["octet", "short", "long", "longlong", "timestamp"] + + def is_packed(s) + s.kind_of? AmqpStruct + end + + def execution_header?(s) + false and s.kind_of? AmqpMethod and s.parent.l4? + end + + def has_bitfields_only(s) + s.fields.select {|f| f.domain.type_ != "bit"}.empty? + end + + def default_initialisation(s) + params = s.fields.select {|f| ValueTypes.include?(f.domain.type_) || (!is_packed(s) && f.domain.type_ == "bit")} + strings = params.collect {|f| "#{f.cppname}(0)"} + strings << "flags(0)" if (is_packed(s)) + if strings.empty? + return "" + else + return " : " + strings.join(", ") + end + end + + def printable_form(f) + if (f.cpptype.name == "uint8_t") + return "(int) " + f.cppname + elsif (f.domain.type_ == "bit") + return "get#{f.name.caps}()" + else + return f.cppname + end + end + + def flag_mask(s, i) + pos = SizeMap[s.pack]*8 - 8 - (i/8)*8 + (i % 8) + return "(1 << #{pos})" + end + + def encode_packed_struct(s) + genl s.cpp_pack_type.encode('flags', 'buffer') + process_packed_fields(s) { |f, i| encode_packed_field(s, f, i) unless f.domain.type_ == "bit" } + end + + def decode_packed_struct(s) + genl "#{s.cpp_pack_type.decode('flags', 'buffer')}" + process_packed_fields(s) { |f, i| decode_packed_field(s, f, i) unless f.domain.type_ == "bit" } + end + + def size_packed_struct(s) + genl "total += #{SizeMap[s.pack]};" + process_packed_fields(s) { |f, i| size_packed_field(s, f, i) unless f.domain.type_ == "bit" } + end + + def print_packed_struct(s) + process_packed_fields(s) { |f, i| print_packed_field(s, f, i) } + end + + def encode_packed_field(s, f, i) + genl "if (flags & #{flag_mask(s, i)})" + indent { genl f.domain.cpptype.encode(f.cppname,"buffer") } + end + + def decode_packed_field(s, f, i) + genl "if (flags & #{flag_mask(s, i)})" + indent { genl f.domain.cpptype.decode(f.cppname,"buffer") } + end + + def size_packed_field(s, f, i) + genl "if (flags & #{flag_mask(s, i)})" + indent { generate_size(f, []) } + end + + def print_packed_field(s, f, i) + genl "if (flags & #{flag_mask(s, i)})" + indent { + genl "out << \"#{f.name}=\" << #{printable_form(f)} << \"; \";" + } + end + + def generate_encode(f, combined) + if (f.domain.type_ == "bit") + genl "uint8_t #{f.cppname}_bits = #{f.cppname};" + count = 0 + combined.each { |c| genl "#{f.cppname}_bits |= #{c.cppname} << #{count += 1};" } + genl "buffer.putOctet(#{f.cppname}_bits);" + else + genl f.domain.cpptype.encode(f.cppname,"buffer") + end + end + + def generate_decode(f, combined) + if (f.domain.type_ == "bit") + genl "uint8_t #{f.cppname}_bits = buffer.getOctet();" + genl "#{f.cppname} = 1 & #{f.cppname}_bits;" + count = 0 + combined.each { |c| genl "#{c.cppname} = (1 << #{count += 1}) & #{f.cppname}_bits;" } + else + genl f.domain.cpptype.decode(f.cppname,"buffer") + end + end + + def generate_size(f, combined) + if (f.domain.type_ == "bit") + names = ([f] + combined).collect {|g| g.cppname} + genl "total += 1;//#{names.join(", ")}" + else + size = SizeMap[f.domain.type_] + if (size) + genl "total += #{size};//#{f.cppname}" + elsif (f.cpptype.name == "SequenceNumberSet") + genl "total += #{f.cppname}.encodedSize();" + else + encoded = EncodingMap[f.domain.type_] + gen "total += (" + gen "4 + " if encoded == "LongString" + gen "1 + " if encoded == "ShortString" + genl "#{f.cppname}.size());" + end + end + end + + def process_packed_fields(s) + s.fields.each { |f| yield f, s.fields.index(f) } + end + + def process_fields(s) + last = nil + count = 0 + bits = [] + s.fields.each { + |f| if (last and last.bit? and f.bit? and count < 7) + count += 1 + bits << f + else + if (last and last.bit?) + yield last, bits + count = 0 + bits = [] + end + if (not f.bit?) + yield f + end + last = f + end + } + if (last and last.bit?) + yield last, bits + end + end + + def methodbody_extra_defs(s) + gen <<EOS + typedef #{s.result ? s.result.struct.cpptype.name : 'void'} ResultType; + + template <class T> ResultType invoke(T& invocable) const { + return invocable.#{s.cppname}(#{s.param_names.join ", "}); + } + + using AMQMethodBody::accept; + void accept(MethodBodyConstVisitor& v) const { v.visit(*this); } + + ClassId amqpClassId() const { return CLASS_ID; } + MethodId amqpMethodId() const { return METHOD_ID; } + bool isContentBearing() const { return #{s.content ? "true" : "false" }; } + bool resultExpected() const { return #{s.result ? "true" : "false"}; } + bool responseExpected() const { return #{s.responses().empty? ? "false" : "true"}; } +EOS + end + + def define_constructor(name, s) + if (s.fields.size > 0) + genl "#{name}(" + if (s.kind_of? AmqpMethod) + indent {gen "ProtocolVersion, "} + end + indent { gen s.fields.collect { |f| "#{f.cpptype.param} _#{f.cppname}" }.join(",\n") } + genl ") : " + if (is_packed(s)) + initialisers = s.fields.select { |f| f.domain.type_ != "bit"}.collect { |f| "#{f.cppname}(_#{f.cppname})"} + + initialisers << "flags(0)" + indent { gen initialisers.join(",\n") } + genl "{" + indent { + process_packed_fields(s) { |f, i| genl "set#{f.name.caps}(_#{f.cppname});" if f.domain.type_ == "bit"} + process_packed_fields(s) { |f, i| genl "flags |= #{flag_mask(s, i)};" unless f.domain.type_ == "bit"} + } + genl "}" + else + indent { gen s.fields.collect { |f| " #{f.cppname}(_#{f.cppname})" }.join(",\n") } + genl "{}" + end + end + #default constructors: + if (s.kind_of? AmqpMethod) + genl "#{name}(ProtocolVersion=ProtocolVersion()) {}" + end + if (s.kind_of? AmqpStruct) + genl "#{name}() #{default_initialisation(s)} {}" + end + end + + def define_packed_field_accessors(s, f, i) + if (f.domain.type_ == "bit") + genl "void #{s.cppname}::set#{f.name.caps}(#{f.cpptype.param} _#{f.cppname}) {" + indent { + genl "if (_#{f.cppname}) flags |= #{flag_mask(s, i)};" + genl "else flags &= ~#{flag_mask(s, i)};" + } + genl "}" + genl "#{f.cpptype.ret} #{s.cppname}::get#{f.name.caps}() const { return flags & #{flag_mask(s, i)}; }" + else + genl "void #{s.cppname}::set#{f.name.caps}(#{f.cpptype.param} _#{f.cppname}) {" + indent { + genl "#{f.cppname} = _#{f.cppname};" + genl "flags |= #{flag_mask(s, i)};" + } + genl "}" + genl "#{f.cpptype.ret} #{s.cppname}::get#{f.name.caps}() const { return #{f.cppname}; }" + if (f.cpptype.name == "FieldTable") + genl "#{f.cpptype.name}& #{s.cppname}::get#{f.name.caps}() {" + indent { + genl "flags |= #{flag_mask(s, i)};"#treat the field table as having been 'set' + genl "return #{f.cppname};" + } + genl "}" + end + genl "bool #{s.cppname}::has#{f.name.caps}() const { return flags & #{flag_mask(s, i)}; }" + genl "void #{s.cppname}::clear#{f.name.caps}Flag() { flags &= ~#{flag_mask(s, i)}; }" + end + genl "" + end + + def define_packed_accessors(s) + process_packed_fields(s) { |f, i| define_packed_field_accessors(s, f, i) } + end + + def declare_packed_accessors(f) + genl "void set#{f.name.caps}(#{f.cpptype.param} _#{f.cppname});"; + genl "#{f.cpptype.ret} get#{f.name.caps}() const;" + if (f.cpptype.name == "FieldTable") + genl "#{f.cpptype.name}& get#{f.name.caps}();" + end + if (f.domain.type_ != "bit") + #extra 'accessors' for packed fields: + genl "bool has#{f.name.caps}() const;" + genl "void clear#{f.name.caps}Flag();" + end + end + + def define_accessors(f) + genl "void set#{f.name.caps}(#{f.cpptype.param} _#{f.cppname}) { #{f.cppname} = _#{f.cppname}; }" + genl "#{f.cpptype.ret} get#{f.name.caps}() const { return #{f.cppname}; }" + if (f.cpptype.name == "FieldTable") + genl "#{f.cpptype.name}& get#{f.name.caps}() { return #{f.cppname}; }" + end + end + + def define_struct(s) + classname = s.cppname + inheritance = "" + if (s.kind_of? AmqpMethod) + classname = s.body_name + if (execution_header?(s)) + inheritance = ": public ModelMethod" + else + inheritance = ": public AMQMethodBody" + end + end + + h_file("qpid/framing/#{classname}.h") { + if (s.kind_of? AmqpMethod) + gen <<EOS +#include "qpid/framing/AMQMethodBody.h" +#include "qpid/framing/AMQP_ServerOperations.h" +#include "qpid/framing/MethodBodyConstVisitor.h" +EOS + end + include "qpid/framing/ModelMethod.h" if (execution_header?(s)) + + #need to include any nested struct definitions + s.fields.each { |f| include f.cpptype.name if f.domain.struct } + + gen <<EOS + +#include <ostream> +#include "qpid/framing/amqp_types_full.h" + +namespace qpid { +namespace framing { + +class #{classname} #{inheritance} { +EOS + if (is_packed(s)) + indent { s.fields.each { |f| genl "#{f.cpptype.name} #{f.cppname};" unless f.domain.type_ == "bit"} } + indent { + genl "#{s.cpp_pack_type.name} flags;" + } + else + indent { s.fields.each { |f| genl "#{f.cpptype.name} #{f.cppname};" } } + end + genl "public:" + if (s.kind_of? AmqpMethod) + indent { genl "static const ClassId CLASS_ID = #{s.parent.index};" } + indent { genl "static const MethodId METHOD_ID = #{s.index};" } + end + + if (s.kind_of? AmqpStruct) + if (s.type_) + if (s.result?) + #as result structs have types that are only unique to the + #class, they have a class dependent qualifier added to them + #(this is inline with current python code but a formal + #solution is expected from the WG) + indent { genl "static const uint16_t TYPE = #{s.type_} + #{s.parent.parent.parent.index} * 256;" } + else + indent { genl "static const uint16_t TYPE = #{s.type_};" } + end + end + end + + indent { + define_constructor(classname, s) + genl "" + if (is_packed(s)) + s.fields.each { |f| declare_packed_accessors(f) } + else + s.fields.each { |f| define_accessors(f) } + end + } + if (s.kind_of? AmqpMethod) + methodbody_extra_defs(s) + end + if (s.kind_of? AmqpStruct) + indent {genl "friend std::ostream& operator<<(std::ostream&, const #{classname}&);" } + end + + gen <<EOS + void encode(Buffer&) const; + void decode(Buffer&, uint32_t=0); + void encodeStructBody(Buffer&) const; + void decodeStructBody(Buffer&, uint32_t=0); + uint32_t size() const; + uint32_t bodySize() const; + void print(std::ostream& out) const; +}; /* class #{classname} */ + +}} +EOS + } + cpp_file("qpid/framing/#{classname}.cpp") { + if (s.fields.size > 0 || execution_header?(s)) + buffer = "buffer" + else + buffer = "/*buffer*/" + end + gen <<EOS +#include "#{classname}.h" + +using namespace qpid::framing; + +EOS + + if (is_packed(s)) + define_packed_accessors(s) + end + gen <<EOS +void #{classname}::encodeStructBody(Buffer& #{buffer}) const +{ +EOS + if (execution_header?(s)) + genl "ModelMethod::encode(buffer);" + end + + if (is_packed(s)) + indent {encode_packed_struct(s)} + else + indent { process_fields(s) { |f, combined| generate_encode(f, combined) } } + end + gen <<EOS +} + +void #{classname}::encode(Buffer& buffer) const +{ +EOS + indent { + if (s.kind_of? AmqpStruct) + if (s.type_) + genl "buffer.put#{s.size.caps}(bodySize() + 2/*typecode*/);" if s.size + genl "buffer.putShort(TYPE);" + else + genl "buffer.put#{s.size.caps}(size());" if s.size + end + end + genl "encodeStructBody(buffer);" + } + gen <<EOS +} + +void #{classname}::decodeStructBody(Buffer& #{buffer}, uint32_t /*size*/) +{ +EOS + if (execution_header?(s)) + genl "ModelMethod::decode(buffer);" + end + + if (is_packed(s)) + indent {decode_packed_struct(s)} + else + indent { process_fields(s) { |f, combined| generate_decode(f, combined) } } + end + gen <<EOS +} + +void #{classname}::decode(Buffer& buffer, uint32_t /*size*/) +{ +EOS + indent { + if (s.kind_of? AmqpStruct) + genl "buffer.get#{s.size.caps}();" if s.size + genl "if (TYPE != buffer.getShort()) throw InternalErrorException(\"Bad type code for struct\");" if s.type_ + end + genl "decodeStructBody(buffer);" + } + gen <<EOS +} + +uint32_t #{classname}::bodySize() const +{ + uint32_t total = 0; +EOS + if (execution_header?(s)) + genl "total += ModelMethod::size();" + end + + if (is_packed(s)) + indent {size_packed_struct(s)} + else + indent { process_fields(s) { |f, combined| generate_size(f, combined) } } + end + gen <<EOS + return total; +} + +uint32_t #{classname}::size() const +{ + uint32_t total = bodySize(); +EOS + if (s.kind_of? AmqpStruct) + genl "total += #{SizeMap[s.size]}/*size field*/;" if s.size + genl "total += 2/*typecode*/;" if s.type_ + end + gen <<EOS + return total; +} + +void #{classname}::print(std::ostream& out) const +{ + out << "{#{classname}: "; +EOS + if (is_packed(s)) + indent {print_packed_struct(s)} + else + copy = Array.new(s.fields) + f = copy.shift + + indent { + genl "out << \"#{f.name}=\" << #{printable_form(f)};" if f + copy.each { |f| genl "out << \"; #{f.name}=\" << #{printable_form(f)};" } + } + end + gen <<EOS + out << "}"; +} +EOS + + if (s.kind_of? AmqpStruct) + gen <<EOS +namespace qpid{ +namespace framing{ + + std::ostream& operator<<(std::ostream& out, const #{classname}& s) + { + s.print(out); + return out; + } + +} +} +EOS + end +} + end + + def generate() + @amqp.structs.each { |s| define_struct(s) } + @amqp.methods_.each { |m| define_struct(m) } + #generate a single include file containing the list of structs for convenience + h_file("qpid/framing/amqp_structs.h") { @amqp.structs.each { |s| genl "#include \"#{s.cppname}.h\"" } } + end +end + +StructGen.new(ARGV[0], $amqp).generate() + |
