From 84baf0acb5254857e424e22a6c1cedbd1ffb8ec2 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Tue, 16 Oct 2007 19:07:54 +0000 Subject: * Summary: generalized Invoker visitor to all *Operations and *Handler classes, client and broker. Single template free function invoke(Invocable, const AMQBody&); works for all invocable handlers. * rubygen/templates/OperationsInvoker.rb: Generates invoker visitors for all Operations classes, client and server. * src/qpid/framing/Invoker.h: Invoker base class and template invoke() function. * rubygen/templates/structs.rb: add generic invoke method template to invoke an arbitrary object with the correct memeber function. * src/qpid/framing/AMQMethodBody.cpp, .h: Removed invoke(), replaced by qpid::framing::invoke() * src/qpid/broker/SemanticHandler.cpp, ConnectionHandler.cpp: Replace AMQMethodBody::invoke with invoke() free function. * src/qpid/framing/StructHelper.h: Avoid un-necessary alloc and copy in encode/decode. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk@585223 13f79535-47bb-0310-9956-ffa450edef68 --- qpid/cpp/rubygen/cppgen.rb | 1 - qpid/cpp/rubygen/templates/InvocationVisitor.rb | 101 ------------------------ qpid/cpp/rubygen/templates/Operations.rb | 14 ++-- qpid/cpp/rubygen/templates/OperationsInvoker.rb | 92 +++++++++++++++++++++ qpid/cpp/rubygen/templates/structs.rb | 16 ++-- 5 files changed, 108 insertions(+), 116 deletions(-) delete mode 100644 qpid/cpp/rubygen/templates/InvocationVisitor.rb create mode 100755 qpid/cpp/rubygen/templates/OperationsInvoker.rb (limited to 'qpid/cpp/rubygen') diff --git a/qpid/cpp/rubygen/cppgen.rb b/qpid/cpp/rubygen/cppgen.rb index 509cd39295..ceda5e039c 100755 --- a/qpid/cpp/rubygen/cppgen.rb +++ b/qpid/cpp/rubygen/cppgen.rb @@ -202,7 +202,6 @@ class CppGen < Generator end def struct_class(type, name, bases, &block) - genl gen "#{type} #{name}" if (!bases.empty?) genl ":" diff --git a/qpid/cpp/rubygen/templates/InvocationVisitor.rb b/qpid/cpp/rubygen/templates/InvocationVisitor.rb deleted file mode 100644 index 67a0479bb6..0000000000 --- a/qpid/cpp/rubygen/templates/InvocationVisitor.rb +++ /dev/null @@ -1,101 +0,0 @@ -#!/usr/bin/env ruby -$: << ".." # Include .. in load path -require 'cppgen' - -class InvocationVisitor < CppGen - - def initialize(outdir, amqp) - super(outdir, amqp) - @namespace="qpid::framing" - @classname="InvocationVisitor" - @filename="qpid/framing/InvocationVisitor" - end - - def invocation_args(m) - m.param_names.collect {|p| "body.get" + p.caps + "()" }.join(",\n") - end - - def null_visit(m) - genl "void InvocationVisitor::visit(const #{m.body_name}&){}" - end - - def define_visit(m) - if (m.fields.size > 0) - body = "body" - else - body = "/*body*/" - end - gen <(invocable); - } else { - ptr = ops->get#{m.parent.cppname}Handler(); - } - - if (ptr) { -EOS - if (m.result) - indent(2) { genl "encode<#{m.result.struct.cpptype.name}>(ptr->#{m.cppname}(#{invocation_args(m)}), result);" } - else - indent(2) { genl "ptr->#{m.cppname}(#{invocation_args(m)});" } - end - - gen <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/qpid/cpp/rubygen/templates/structs.rb b/qpid/cpp/rubygen/templates/structs.rb index 2f5a3d8365..6a58dfd763 100644 --- a/qpid/cpp/rubygen/templates/structs.rb +++ b/qpid/cpp/rubygen/templates/structs.rb @@ -191,14 +191,20 @@ class StructGen < CppGen def methodbody_extra_defs(s) gen < ResultType invoke(T& invocable) const { + return invocable.#{s.cppname}(#{s.param_names.join ", "}); + } + using AMQMethodBody::accept; void accept(MethodBodyConstVisitor& v) const { v.visit(*this); } - inline ClassId amqpClassId() const { return CLASS_ID; } - inline MethodId amqpMethodId() const { return METHOD_ID; } - inline bool isContentBearing() const { return #{s.content ? "true" : "false" }; } - inline bool resultExpected() const { return #{s.result ? "true" : "false"}; } - inline bool responseExpected() const { return #{s.responses().empty? ? "false" : "true"}; } + 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 -- cgit v1.2.1