From 819af12055e4fb78d0bcc8875e1293e8561ae0c3 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Tue, 20 Jul 2021 10:32:10 +0200 Subject: [PATCH] Replace shared_ptr by unique_ptr for Fields (#402) There is no real purpose to using shared pointers it seems. --- include/amqpcpp/array.h | 12 +++++----- include/amqpcpp/booleanset.h | 6 ++--- include/amqpcpp/decimalfield.h | 6 ++--- include/amqpcpp/field.h | 6 ++--- include/amqpcpp/numericfield.h | 6 ++--- include/amqpcpp/stringfield.h | 6 ++--- include/amqpcpp/table.h | 6 ++--- include/amqpcpp/voidfield.h | 6 ++--- src/array.cpp | 4 ++-- src/field.cpp | 41 +++++++++++++++++----------------- 10 files changed, 50 insertions(+), 49 deletions(-) diff --git a/include/amqpcpp/array.h b/include/amqpcpp/array.h index 40b0e7d..3f63a8e 100644 --- a/include/amqpcpp/array.h +++ b/include/amqpcpp/array.h @@ -32,7 +32,7 @@ private: * Definition of an array as a vector * @typedef */ - typedef std::vector> FieldArray; + typedef std::vector> FieldArray; /** * The actual fields @@ -74,9 +74,9 @@ public: * Create a new instance of this object * @return Field* */ - virtual std::shared_ptr clone() const override + virtual std::unique_ptr clone() const override { - return std::make_shared(*this); + return std::unique_ptr(new Array(*this)); } /** @@ -95,19 +95,19 @@ public: */ Array set(uint8_t index, const Field &value) { - // construct a shared pointer + // make a copy auto ptr = value.clone(); // should we overwrite an existing record? if (index >= _fields.size()) { // append index - _fields.push_back(ptr); + _fields.push_back(std::move(ptr)); } else { // overwrite pointer - _fields[index] = ptr; + _fields[index] = std::move(ptr); } // allow chaining diff --git a/include/amqpcpp/booleanset.h b/include/amqpcpp/booleanset.h index d381101..1b7b55c 100644 --- a/include/amqpcpp/booleanset.h +++ b/include/amqpcpp/booleanset.h @@ -88,11 +88,11 @@ public: /** * Extending from field forces us to implement a clone function. - * @return shared_ptr + * @return unique_ptr */ - virtual std::shared_ptr clone() const override + virtual std::unique_ptr clone() const override { - return std::make_shared(*this); + return std::unique_ptr(new BooleanSet(*this)); } /** diff --git a/include/amqpcpp/decimalfield.h b/include/amqpcpp/decimalfield.h index 9d7383e..8053ff3 100644 --- a/include/amqpcpp/decimalfield.h +++ b/include/amqpcpp/decimalfield.h @@ -94,11 +94,11 @@ public: /** * Create a new identical instance of this object - * @return Field* + * @return unique_ptr */ - virtual std::shared_ptr clone() const override + virtual std::unique_ptr clone() const override { - return std::make_shared(_places, _number); + return std::unique_ptr(new DecimalField(_places, _number)); } /** diff --git a/include/amqpcpp/field.h b/include/amqpcpp/field.h index 5dd4475..6c04cab 100644 --- a/include/amqpcpp/field.h +++ b/include/amqpcpp/field.h @@ -40,9 +40,9 @@ protected: * Decode a field by fetching a type and full field from a frame * The returned field is allocated on the heap! * @param frame - * @return std::shared_ptr + * @return std::unique_ptr */ - static std::shared_ptr decode(InBuffer &frame); + static std::unique_ptr decode(InBuffer &frame); public: /** @@ -54,7 +54,7 @@ public: * Create a new instance on the heap of this object, identical to the object passed * @return Field* */ - virtual std::shared_ptr clone() const = 0; + virtual std::unique_ptr clone() const = 0; /** * Get the size this field will take when diff --git a/include/amqpcpp/numericfield.h b/include/amqpcpp/numericfield.h index 1ee6518..6038f98 100644 --- a/include/amqpcpp/numericfield.h +++ b/include/amqpcpp/numericfield.h @@ -93,12 +93,12 @@ public: /** * Create a new instance of this object - * @return Field* + * @return unique_ptr */ - virtual std::shared_ptr clone() const override + virtual std::unique_ptr clone() const override { // create a new copy of ourselves and return it - return std::make_shared(_value); + return std::unique_ptr(new NumericField(_value)); } /** diff --git a/include/amqpcpp/stringfield.h b/include/amqpcpp/stringfield.h index 27a7533..0565d3d 100644 --- a/include/amqpcpp/stringfield.h +++ b/include/amqpcpp/stringfield.h @@ -86,12 +86,12 @@ public: /** * Create a new instance of this object - * @return Field* + * @return std::unique_ptr */ - virtual std::shared_ptr clone() const override + virtual std::unique_ptr clone() const override { // create a new copy of ourselves and return it - return std::make_shared(_data); + return std::unique_ptr(new StringField(_data)); } /** diff --git a/include/amqpcpp/table.h b/include/amqpcpp/table.h index 8be349f..db6768a 100644 --- a/include/amqpcpp/table.h +++ b/include/amqpcpp/table.h @@ -33,7 +33,7 @@ private: * We define a custom type for storing fields * @typedef FieldMap */ - typedef std::map > FieldMap; + typedef std::map > FieldMap; /** * Store the fields @@ -96,9 +96,9 @@ public: * Create a new instance on the heap of this object, identical to the object passed * @return Field* */ - virtual std::shared_ptr clone() const override + virtual std::unique_ptr clone() const override { - return std::make_shared(*this); + return std::unique_ptr
(new Table(*this)); } /** diff --git a/include/amqpcpp/voidfield.h b/include/amqpcpp/voidfield.h index 7f597da..aa7780f 100644 --- a/include/amqpcpp/voidfield.h +++ b/include/amqpcpp/voidfield.h @@ -42,12 +42,12 @@ public: /** * Create a new instance of this object - * @return Field* + * @return unique_ptr */ - virtual std::shared_ptr clone() const override + virtual std::unique_ptr clone() const override { // create a new copy of ourselves and return it - return std::make_shared(); + return std::unique_ptr(new VoidField); } /** diff --git a/src/array.cpp b/src/array.cpp index 215ef32..0f29e8f 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -107,7 +107,7 @@ size_t Array::size() const size_t size = 4; // iterate over all elements - for (auto item : _fields) + for (const auto &item : _fields) { // add the size of the field type and size of element size += sizeof(item->typeID()); @@ -128,7 +128,7 @@ void Array::fill(OutBuffer& buffer) const buffer.add(static_cast(size()-4)); // iterate over all elements - for (auto item : _fields) + for (const auto &item : _fields) { // encode the element type and element buffer.add((uint8_t)item->typeID()); diff --git a/src/field.cpp b/src/field.cpp index 0c78874..2d413ce 100644 --- a/src/field.cpp +++ b/src/field.cpp @@ -14,9 +14,9 @@ namespace AMQP { * Decode a field by fetching a type and full field from a frame * The returned field is allocated on the heap! * @param frame - * @return std::shared_ptr + * @return std::unique_ptr */ -std::shared_ptr Field::decode(InBuffer &frame) +std::unique_ptr Field::decode(InBuffer &frame) { // get the type uint8_t type = frame.nextUint8(); @@ -24,24 +24,25 @@ std::shared_ptr Field::decode(InBuffer &frame) // create field based on type switch (type) { - case 't': return std::make_shared(frame); - case 'b': return std::make_shared(frame); - case 'B': return std::make_shared(frame); - case 'U': return std::make_shared(frame); - case 'u': return std::make_shared(frame); - case 'I': return std::make_shared(frame); - case 'i': return std::make_shared(frame); - case 'L': return std::make_shared(frame); - case 'l': return std::make_shared(frame); - case 'f': return std::make_shared(frame); - case 'd': return std::make_shared(frame); - case 'D': return std::make_shared(frame); - case 's': return std::make_shared(frame); - case 'S': return std::make_shared(frame); - case 'A': return std::make_shared(frame); - case 'T': return std::make_shared(frame); - case 'F': return std::make_shared
(frame); - case 'V': return std::make_shared(frame); + // @todo: use std::make_unique when switching to C++14/17/20 + case 't': return std::unique_ptr(new BooleanSet(frame)); + case 'b': return std::unique_ptr(new Octet(frame)); + case 'B': return std::unique_ptr(new UOctet(frame)); + case 'U': return std::unique_ptr(new Short(frame)); + case 'u': return std::unique_ptr(new UShort(frame)); + case 'I': return std::unique_ptr(new Long(frame)); + case 'i': return std::unique_ptr(new ULong(frame)); + case 'L': return std::unique_ptr(new LongLong(frame)); + case 'l': return std::unique_ptr(new ULongLong(frame)); + case 'f': return std::unique_ptr(new Float(frame)); + case 'd': return std::unique_ptr(new Double(frame)); + case 'D': return std::unique_ptr(new DecimalField(frame)); + case 's': return std::unique_ptr(new ShortString(frame)); + case 'S': return std::unique_ptr(new LongString(frame)); + case 'A': return std::unique_ptr(new Array(frame)); + case 'T': return std::unique_ptr(new Timestamp(frame)); + case 'F': return std::unique_ptr(new Table(frame)); + case 'V': return std::unique_ptr(new VoidField(frame)); default: return nullptr; } }