summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/framing/AMQFrame.h
diff options
context:
space:
mode:
authorAlan Conway <aconway@apache.org>2007-08-16 20:12:33 +0000
committerAlan Conway <aconway@apache.org>2007-08-16 20:12:33 +0000
commit49c7a491c98c26fe7d4f017a7ba655dfc029278c (patch)
tree304d51ba039a5391b4ebde08caab3da978b465fb /cpp/src/qpid/framing/AMQFrame.h
parentdc13ca80ff893f74ab57fee6543de6543aa366bc (diff)
downloadqpid-python-49c7a491c98c26fe7d4f017a7ba655dfc029278c.tar.gz
AMQBodies are no longer allocated on the heap and passed with shared_ptr.
AMQFrame contains a boost::variant of AMQHeaderBody,AMQContentBody, AMQHeatbeatBody, and MethodHolder. A variant is basically a type-safe union, it can allocate any of the types in-place. MethodHolder contains a Blob, a less sophisticated kind of variant, which can contain any of the concrete method body types. Using variants for all the method types causes outrageous compile times and bloated library symbol names. Blob lacks some of the finer features of variant and needs help from generated code. For now both are hidden to the rest of the code base behind AMQFrame and MethodBody classes so if/when we decide to settle on just one "variant" type solution we can do so. This commit touches nearly 100 files, mostly converting method signatures with shared_ptr<FooBody> to FooBody* or FooBody&, and converting stored shared_ptr<AMQBody> to AMQFrame and share_ptr<AMQMethodBody> to MethodHolder. There is one outstanding client memory leak, which I will fix in my next commit. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@566822 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid/framing/AMQFrame.h')
-rw-r--r--cpp/src/qpid/framing/AMQFrame.h87
1 files changed, 54 insertions, 33 deletions
diff --git a/cpp/src/qpid/framing/AMQFrame.h b/cpp/src/qpid/framing/AMQFrame.h
index 16c1427802..9e825a9936 100644
--- a/cpp/src/qpid/framing/AMQFrame.h
+++ b/cpp/src/qpid/framing/AMQFrame.h
@@ -21,55 +21,76 @@
* under the License.
*
*/
-#include <boost/cast.hpp>
-
-#include "amqp_types.h"
-#include "AMQBody.h"
#include "AMQDataBlock.h"
-#include "AMQMethodBody.h"
#include "AMQHeaderBody.h"
#include "AMQContentBody.h"
#include "AMQHeartbeatBody.h"
-#include "qpid/framing/AMQP_MethodVersionMap.h"
-#include "qpid/framing/AMQP_HighestVersion.h"
-#include "qpid/framing/Buffer.h"
-#include "qpid/shared_ptr.h"
+#include "MethodHolder.h"
+#include "ProtocolVersion.h"
+
+#include <boost/cast.hpp>
+#include <boost/variant.hpp>
namespace qpid {
namespace framing {
-
class AMQFrame : public AMQDataBlock
{
public:
- AMQFrame(ProtocolVersion _version = highestProtocolVersion);
- AMQFrame(ProtocolVersion _version, uint16_t channel, AMQBody* body);
- AMQFrame(ProtocolVersion _version, uint16_t channel, const AMQBody::shared_ptr& body);
- virtual ~AMQFrame();
- virtual void encode(Buffer& buffer);
- virtual bool decode(Buffer& buffer);
- virtual uint32_t size() const;
- uint16_t getChannel() const { return channel; }
-
- shared_ptr<AMQBody> getBody() { return body; }
- void setBody(const shared_ptr<AMQBody>& b) { body = b; }
-
- /** Convenience template to cast the body to an expected type */
- template <class T> boost::shared_ptr<T> castBody() {
- assert(dynamic_cast<T*>(getBody().get()));
- boost::static_pointer_cast<T>(getBody());
+ AMQFrame(ProtocolVersion=ProtocolVersion()) {}
+
+ /** Construct a frame with a copy of b */
+ AMQFrame(ProtocolVersion, ChannelId c, const AMQBody* b) : channel(c) {
+ setBody(*b);
+ }
+
+ AMQFrame(ProtocolVersion, ChannelId c, const AMQBody& b) : channel(c) {
+ setBody(b);
}
+
+ ChannelId getChannel() const { return channel; }
+ void setChannel(ChannelId c) { channel = c; }
- uint32_t decodeHead(Buffer& buffer);
- void decodeBody(Buffer& buffer, uint32_t size);
+ AMQBody* getBody();
+ const AMQBody* getBody() const;
- uint16_t channel;
- uint8_t type;
- AMQBody::shared_ptr body;
- ProtocolVersion version;
+ /** Copy a body instance to the frame */
+ void setBody(const AMQBody& b) { CopyVisitor cv(*this); b.accept(cv); }
+
+ /** Convenience template to cast the body to an expected type. */
+ template <class T> T* castBody() {
+ boost::polymorphic_downcast<T*>(getBody());
+ }
+
+ bool empty() { return boost::get<boost::blank>(&body); }
+
+ void encode(Buffer& buffer);
+ bool decode(Buffer& buffer);
+ uint32_t size() const;
private:
- static AMQP_MethodVersionMap versionMap;
+ struct CopyVisitor : public AMQBodyConstVisitor {
+ AMQFrame& frame;
+ CopyVisitor(AMQFrame& f) : frame(f) {}
+ void visit(const AMQHeaderBody& x) { frame.body=x; }
+ void visit(const AMQContentBody& x) { frame.body=x; }
+ void visit(const AMQHeartbeatBody& x) { frame.body=x; }
+ void visit(const AMQMethodBody& x) { frame.body=MethodHolder(x); }
+ };
+ friend struct CopyVisitor;
+
+ typedef boost::variant<boost::blank,
+ AMQHeaderBody,
+ AMQContentBody,
+ AMQHeartbeatBody,
+ MethodHolder> Variant;
+
+ void visit(AMQHeaderBody& x) { body=x; }
+
+ void decodeBody(Buffer& buffer, uint32_t size, uint8_t type);
+
+ uint16_t channel;
+ Variant body;
};
std::ostream& operator<<(std::ostream&, const AMQFrame&);