summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2007-10-16 19:07:54 +0000
committerAlan Conway <aconway@apache.org>2007-10-16 19:07:54 +0000
commit428de9b6fe6f81f2bfc3f47d5db013b4b00da6a2 (patch)
treee3fe772cbe0e08ea84f4b2f54b969dc7471bb8e1 /cpp
parent3bd61c1fa1a748789707b502ada85200f6180f5b (diff)
downloadqpid-python-428de9b6fe6f81f2bfc3f47d5db013b4b00da6a2.tar.gz
* 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/qpid@585223 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp')
-rwxr-xr-xcpp/rubygen/cppgen.rb1
-rw-r--r--cpp/rubygen/templates/InvocationVisitor.rb101
-rwxr-xr-xcpp/rubygen/templates/Operations.rb14
-rwxr-xr-xcpp/rubygen/templates/OperationsInvoker.rb92
-rw-r--r--cpp/rubygen/templates/structs.rb16
-rw-r--r--cpp/src/Makefile.am1
-rw-r--r--cpp/src/qpid/broker/ConnectionHandler.cpp3
-rw-r--r--cpp/src/qpid/broker/SemanticHandler.cpp14
-rw-r--r--cpp/src/qpid/broker/SessionHandler.cpp7
-rw-r--r--cpp/src/qpid/broker/SessionHandler.h2
-rw-r--r--cpp/src/qpid/client/ExecutionHandler.cpp10
-rw-r--r--cpp/src/qpid/client/ExecutionHandler.h6
-rw-r--r--cpp/src/qpid/framing/AMQMethodBody.cpp15
-rw-r--r--cpp/src/qpid/framing/AMQMethodBody.h4
-rw-r--r--cpp/src/qpid/framing/FrameDefaultVisitor.h3
-rw-r--r--cpp/src/qpid/framing/Invoker.h86
-rw-r--r--cpp/src/qpid/framing/StructHelper.h13
17 files changed, 218 insertions, 170 deletions
diff --git a/cpp/rubygen/cppgen.rb b/cpp/rubygen/cppgen.rb
index 509cd39295..ceda5e039c 100755
--- a/cpp/rubygen/cppgen.rb
+++ b/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/cpp/rubygen/templates/InvocationVisitor.rb b/cpp/rubygen/templates/InvocationVisitor.rb
deleted file mode 100644
index 67a0479bb6..0000000000
--- a/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 <<EOS
-void InvocationVisitor::visit(const #{m.body_name}& #{body})
-{
- AMQP_ServerOperations::#{m.parent.cppname}Handler* ptr(0);
- if (invocable) {
- ptr = dynamic_cast<AMQP_ServerOperations::#{m.parent.cppname}Handler*>(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 <<EOS
- succeeded = true;
- } else {
- succeeded = false;
- }
-}
-EOS
- end
-
- def generate()
- h_file("#{@filename}") {
- include "AMQP_ServerOperations.h"
- include "MethodBodyConstVisitor.h"
- include "qpid/framing/StructHelper.h"
- namespace(@namespace) {
- cpp_class("InvocationVisitor", ["public MethodBodyConstVisitor", "private StructHelper"]) {
- indent {
- genl "AMQP_ServerOperations* const ops;"
- genl "Invocable* const invocable;"
- genl "std::string result;"
- genl "bool succeeded;"
- }
- genl "public:"
- indent {
- genl "InvocationVisitor(AMQP_ServerOperations* _ops) : ops(_ops), invocable(0), succeeded(false) {}"
- genl "InvocationVisitor(Invocable* _invocable) : ops(0), invocable(_invocable), succeeded(false) {}"
- genl "const std::string& getResult() const { return result; }"
- genl "const bool hasResult() const { return !result.empty(); }"
- genl "bool wasHandled() const { return succeeded; }"
- genl "void clear();"
- genl "virtual ~InvocationVisitor() {}"
- }
- @amqp.methods_.each { |m| genl "void visit(const #{m.body_name}&);" }
- }
- }
- }
-
- cpp_file("#{@filename}") {
- include "InvocationVisitor.h"
- @amqp.methods_.each { |m| include m.body_name }
- namespace(@namespace) {
- genl "void InvocationVisitor::clear() {"
- indent {
- genl "succeeded = false;"
- genl "result.clear();"
- }
- genl "}"
- genl
- @amqp.methods_.each { |m| m.on_server? && !m.content() ? define_visit(m) : null_visit(m) }
- }
- }
- end
-end
-
-InvocationVisitor.new(Outdir, Amqp).generate();
-
diff --git a/cpp/rubygen/templates/Operations.rb b/cpp/rubygen/templates/Operations.rb
index 1002bf07a4..91007ef3e1 100755
--- a/cpp/rubygen/templates/Operations.rb
+++ b/cpp/rubygen/templates/Operations.rb
@@ -29,9 +29,11 @@ class OperationsGen < CppGen
handlerclass=handler_classname c
gen <<EOS
// ==================== class #{handlerclass} ====================
-class #{handlerclass} : public virtual Invocable {
+class #{handlerclass} {
// Constructors and destructors
public:
+ class Invoker; // Declared in #{@chassis.caps}Invoker
+
#{handlerclass}(){};
virtual ~#{handlerclass}() {}
// Protocol methods
@@ -64,16 +66,10 @@ namespace framing {
class AMQMethodBody;
-class Invocable
-{
-protected:
- Invocable() {}
- virtual ~Invocable() {}
-};
-
class #{@classname} {
-
public:
+ class Invoker; // Declared in #{@chassis.caps}Invoker
+
virtual ~#{@classname}() {}
virtual ProtocolVersion getVersion() const = 0;
diff --git a/cpp/rubygen/templates/OperationsInvoker.rb b/cpp/rubygen/templates/OperationsInvoker.rb
new file mode 100755
index 0000000000..747dd06189
--- /dev/null
+++ b/cpp/rubygen/templates/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/templates/structs.rb b/cpp/rubygen/templates/structs.rb
index 2f5a3d8365..6a58dfd763 100644
--- a/cpp/rubygen/templates/structs.rb
+++ b/cpp/rubygen/templates/structs.rb
@@ -191,14 +191,20 @@ class StructGen < CppGen
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); }
- 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
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am
index 7a7a0b879a..7f644a3c8f 100644
--- a/cpp/src/Makefile.am
+++ b/cpp/src/Makefile.am
@@ -341,6 +341,7 @@ nobase_include_HEADERS = \
qpid/framing/HandlerUpdater.h \
qpid/framing/HeaderProperties.h \
qpid/framing/InitiationHandler.h \
+ qpid/framing/Invoker.h \
qpid/framing/InputHandler.h \
qpid/framing/MethodContent.h \
qpid/framing/MethodHolder.h \
diff --git a/cpp/src/qpid/broker/ConnectionHandler.cpp b/cpp/src/qpid/broker/ConnectionHandler.cpp
index a769d05470..f697986194 100644
--- a/cpp/src/qpid/broker/ConnectionHandler.cpp
+++ b/cpp/src/qpid/broker/ConnectionHandler.cpp
@@ -23,6 +23,7 @@
#include "ConnectionHandler.h"
#include "Connection.h"
#include "qpid/framing/ConnectionStartBody.h"
+#include "qpid/framing/ServerInvoker.h"
using namespace qpid;
using namespace qpid::broker;
@@ -44,7 +45,7 @@ void ConnectionHandler::handle(framing::AMQFrame& frame)
{
AMQMethodBody* method=frame.getBody()->getMethod();
try{
- if (!method->invoke(handler.get()))
+ if (!invoke(*handler.get(), *method))
throw ConnectionException(503, "Class can't be accessed over channel 0");
}catch(ConnectionException& e){
handler->client.close(e.code, e.toString(), method->amqpClassId(), method->amqpMethodId());
diff --git a/cpp/src/qpid/broker/SemanticHandler.cpp b/cpp/src/qpid/broker/SemanticHandler.cpp
index dc5407be99..8535dc6a60 100644
--- a/cpp/src/qpid/broker/SemanticHandler.cpp
+++ b/cpp/src/qpid/broker/SemanticHandler.cpp
@@ -28,7 +28,7 @@
#include "Connection.h"
#include "qpid/framing/ExecutionCompleteBody.h"
#include "qpid/framing/ExecutionResultBody.h"
-#include "qpid/framing/InvocationVisitor.h"
+#include "qpid/framing/ServerInvoker.h"
#include <boost/format.hpp>
#include <boost/bind.hpp>
@@ -121,14 +121,13 @@ void SemanticHandler::handleCommand(framing::AMQMethodBody* method)
{
SequenceNumber id = incoming.next();
BrokerAdapter adapter(state);
- InvocationVisitor v(&adapter);
- method->accept(v);
+ Invoker::Result invoker = invoke(adapter, *method);
incoming.complete(id);
- if (!v.wasHandled()) {
+ if (!invoker.wasHandled()) {
throw ConnectionException(540, "Not implemented");
- } else if (v.hasResult()) {
- session.getProxy().getExecution().result(id.getValue(), v.getResult());
+ } else if (invoker.hasResult()) {
+ session.getProxy().getExecution().result(id.getValue(), invoker.getResult());
}
if (method->isSync()) {
incoming.sync(id);
@@ -139,9 +138,8 @@ void SemanticHandler::handleCommand(framing::AMQMethodBody* method)
void SemanticHandler::handleL3(framing::AMQMethodBody* method)
{
- if (!method->invoke(this)) {
+ if (!invoke(*this, *method))
throw ConnectionException(540, "Not implemented");
- }
}
void SemanticHandler::handleContent(AMQFrame& frame)
diff --git a/cpp/src/qpid/broker/SessionHandler.cpp b/cpp/src/qpid/broker/SessionHandler.cpp
index d7308572f9..ed092d6a05 100644
--- a/cpp/src/qpid/broker/SessionHandler.cpp
+++ b/cpp/src/qpid/broker/SessionHandler.cpp
@@ -23,6 +23,7 @@
#include "Connection.h"
#include "qpid/framing/reply_exceptions.h"
#include "qpid/framing/constants.h"
+#include "qpid/framing/ServerInvoker.h"
#include "qpid/log/Statement.h"
namespace qpid {
@@ -48,10 +49,10 @@ void SessionHandler::handleIn(AMQFrame& f) {
// state. This is a temporary state after we have sent a channel
// exception, where extra frames might arrive that should be
// ignored.
- //
- AMQMethodBody* m=f.getMethod();
+ //
+ AMQMethodBody* m = f.getBody()->getMethod();
try {
- if (m && m->invoke(this))
+ if (m && invoke(*this, *m))
return;
else if (session.get())
session->in(f);
diff --git a/cpp/src/qpid/broker/SessionHandler.h b/cpp/src/qpid/broker/SessionHandler.h
index aec3731dc0..51a65e3092 100644
--- a/cpp/src/qpid/broker/SessionHandler.h
+++ b/cpp/src/qpid/broker/SessionHandler.h
@@ -41,7 +41,7 @@ class SessionState;
* association between the channel and a session.
*/
class SessionHandler : public framing::FrameHandler::InOutHandler,
- private framing::AMQP_ServerOperations::SessionHandler,
+ public framing::AMQP_ServerOperations::SessionHandler,
private boost::noncopyable
{
public:
diff --git a/cpp/src/qpid/client/ExecutionHandler.cpp b/cpp/src/qpid/client/ExecutionHandler.cpp
index 25813dd623..e4edece414 100644
--- a/cpp/src/qpid/client/ExecutionHandler.cpp
+++ b/cpp/src/qpid/client/ExecutionHandler.cpp
@@ -25,6 +25,7 @@
#include "qpid/framing/MessageTransferBody.h"
#include "qpid/framing/AMQP_HighestVersion.h"
#include "qpid/framing/all_method_bodies.h"
+#include "qpid/framing/ServerInvoker.h"
using namespace qpid::client;
using namespace qpid::framing;
@@ -49,20 +50,13 @@ bool isContentFrame(AMQFrame& frame)
return type == HEADER_BODY || type == CONTENT_BODY || isMessageMethod(body);
}
-bool invoke(AMQBody* body, Invocable* target)
-{
- AMQMethodBody* method=body->getMethod();
- return method && method->invoke(target);
-}
-
ExecutionHandler::ExecutionHandler(uint64_t _maxFrameSize) :
version(framing::highestProtocolVersion), maxFrameSize(_maxFrameSize) {}
//incoming:
void ExecutionHandler::handle(AMQFrame& frame)
{
- AMQBody* body = frame.getBody();
- if (!invoke(body, this)) {
+ if (!invoke(*this, *frame.getBody())) {
if (!arriving) {
arriving = FrameSet::shared_ptr(new FrameSet(++incomingCounter));
}
diff --git a/cpp/src/qpid/client/ExecutionHandler.h b/cpp/src/qpid/client/ExecutionHandler.h
index 7205983cf7..0999540909 100644
--- a/cpp/src/qpid/client/ExecutionHandler.h
+++ b/cpp/src/qpid/client/ExecutionHandler.h
@@ -38,9 +38,9 @@ namespace qpid {
namespace client {
class ExecutionHandler :
- private framing::AMQP_ServerOperations::ExecutionHandler,
- public framing::FrameHandler,
- public Execution
+ public framing::AMQP_ServerOperations::ExecutionHandler,
+ public framing::FrameHandler,
+ public Execution
{
framing::SequenceNumber incomingCounter;
framing::AccumulatedAck incomingCompletionStatus;
diff --git a/cpp/src/qpid/framing/AMQMethodBody.cpp b/cpp/src/qpid/framing/AMQMethodBody.cpp
index 48b50763fc..924d906d43 100644
--- a/cpp/src/qpid/framing/AMQMethodBody.cpp
+++ b/cpp/src/qpid/framing/AMQMethodBody.cpp
@@ -19,25 +19,10 @@
*
*/
#include "AMQMethodBody.h"
-#include "qpid/framing/InvocationVisitor.h"
namespace qpid {
namespace framing {
AMQMethodBody::~AMQMethodBody() {}
-void AMQMethodBody::invoke(AMQP_ServerOperations& ops)
-{
- InvocationVisitor v(&ops);
- accept(v);
- assert(v.wasHandled());
-}
-
-bool AMQMethodBody::invoke(Invocable* invocable)
-{
- InvocationVisitor v(invocable);
- accept(v);
- return v.wasHandled();
-}
-
}} // namespace qpid::framing
diff --git a/cpp/src/qpid/framing/AMQMethodBody.h b/cpp/src/qpid/framing/AMQMethodBody.h
index 09a5ea4f00..9f64fd1690 100644
--- a/cpp/src/qpid/framing/AMQMethodBody.h
+++ b/cpp/src/qpid/framing/AMQMethodBody.h
@@ -35,7 +35,6 @@ namespace framing {
class Buffer;
class AMQP_ServerOperations;
-class Invocable;
class MethodBodyConstVisitor;
class AMQMethodBody : public AMQBody {
@@ -53,9 +52,6 @@ class AMQMethodBody : public AMQBody {
virtual bool resultExpected() const = 0;
virtual bool responseExpected() const = 0;
- void invoke(AMQP_ServerOperations&);
- bool invoke(Invocable*);
-
template <class T> bool isA() const {
return amqpClassId()==T::CLASS_ID && amqpMethodId()==T::METHOD_ID;
}
diff --git a/cpp/src/qpid/framing/FrameDefaultVisitor.h b/cpp/src/qpid/framing/FrameDefaultVisitor.h
index a826d69b3d..93a5204308 100644
--- a/cpp/src/qpid/framing/FrameDefaultVisitor.h
+++ b/cpp/src/qpid/framing/FrameDefaultVisitor.h
@@ -43,7 +43,8 @@ class AMQHeartbeatBody;
* for any non-overridden visit functions.
*
*/
-struct FrameDefaultVisitor : public AMQBodyConstVisitor, public MethodBodyDefaultVisitor
+struct FrameDefaultVisitor : public AMQBodyConstVisitor,
+ protected MethodBodyDefaultVisitor
{
virtual void defaultVisit(const AMQBody&) = 0;
diff --git a/cpp/src/qpid/framing/Invoker.h b/cpp/src/qpid/framing/Invoker.h
new file mode 100644
index 0000000000..e6467ab3c4
--- /dev/null
+++ b/cpp/src/qpid/framing/Invoker.h
@@ -0,0 +1,86 @@
+#ifndef QPID_FRAMING_INVOKER_H
+#define QPID_FRAMING_INVOKER_H
+
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+#include "qpid/framing/AMQMethodBody.h"
+#include "qpid/framing/MethodBodyDefaultVisitor.h"
+#include "qpid/framing/StructHelper.h"
+
+#include <boost/optional.hpp>
+
+namespace qpid {
+namespace framing {
+
+class AMQMethodBody;
+
+/**
+ * Base class for invoker visitors.
+ */
+class Invoker: public MethodBodyDefaultVisitor, protected StructHelper
+{
+ public:
+ struct Result {
+ public:
+ Result() : handled(false) {}
+ const std::string& getResult() const { return result; }
+ const bool hasResult() const { return !result.empty(); }
+ bool wasHandled() const { return handled; }
+ operator bool() const { return handled; }
+
+ std::string result;
+ bool handled;
+ };
+
+ void defaultVisit(const AMQMethodBody&) {}
+ Result getResult() const { return result; }
+
+ protected:
+ Result result;
+};
+
+/**
+ * Invoke on an invocable object.
+ * Invocable classes must provide a nested type Invoker.
+ */
+template <class Invocable>
+Invoker::Result invoke(Invocable& target, const AMQMethodBody& body) {
+ typename Invocable::Invoker invoker(target);
+ body.accept(invoker);
+ return invoker.getResult();
+}
+
+/**
+ * Invoke on an invocable object.
+ * Invocable classes must provide a nested type Invoker.
+ */
+template <class Invocable>
+Invoker::Result invoke(Invocable& target, const AMQBody& body) {
+ typename Invocable::Invoker invoker(target);
+ const AMQMethodBody* method = body.getMethod();
+ if (method)
+ method->accept(invoker);
+ return invoker.getResult();
+}
+
+}} // namespace qpid::framing
+
+#endif /*!QPID_FRAMING_INVOKER_H*/
diff --git a/cpp/src/qpid/framing/StructHelper.h b/cpp/src/qpid/framing/StructHelper.h
index 6b111e1f9e..7fc1d2e22b 100644
--- a/cpp/src/qpid/framing/StructHelper.h
+++ b/cpp/src/qpid/framing/StructHelper.h
@@ -35,21 +35,14 @@ public:
template <class T> void encode(const T t, std::string& data) {
uint32_t size = t.size() + 2/*type*/;
- char* bytes = static_cast<char*>(::alloca(size));
- Buffer wbuffer(bytes, size);
+ data.resize(size);
+ Buffer wbuffer(const_cast<char*>(data.data()), size);
wbuffer.putShort(T::TYPE);
t.encode(wbuffer);
-
- Buffer rbuffer(bytes, size);
- rbuffer.getRawData(data, size);
}
template <class T> void decode(T& t, const std::string& data) {
- char* bytes = static_cast<char*>(::alloca(data.length()));
- Buffer wbuffer(bytes, data.length());
- wbuffer.putRawData(data);
-
- Buffer rbuffer(bytes, data.length());
+ Buffer rbuffer(const_cast<char*>(data.data()), data.length());
uint16_t type = rbuffer.getShort();
if (type == T::TYPE) {
t.decode(rbuffer);