Replace shared_ptr by unique_ptr for Fields (#402)

There is no real purpose to using shared pointers it seems.
This commit is contained in:
Raoul Wols 2021-07-20 10:32:10 +02:00 committed by GitHub
parent 29dd838478
commit 819af12055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 49 deletions

View File

@ -32,7 +32,7 @@ private:
* Definition of an array as a vector
* @typedef
*/
typedef std::vector<std::shared_ptr<Field>> FieldArray;
typedef std::vector<std::unique_ptr<Field>> FieldArray;
/**
* The actual fields
@ -74,9 +74,9 @@ public:
* Create a new instance of this object
* @return Field*
*/
virtual std::shared_ptr<Field> clone() const override
virtual std::unique_ptr<Field> clone() const override
{
return std::make_shared<Array>(*this);
return std::unique_ptr<Array>(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

View File

@ -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<Field> clone() const override
virtual std::unique_ptr<Field> clone() const override
{
return std::make_shared<BooleanSet>(*this);
return std::unique_ptr<Field>(new BooleanSet(*this));
}
/**

View File

@ -94,11 +94,11 @@ public:
/**
* Create a new identical instance of this object
* @return Field*
* @return unique_ptr
*/
virtual std::shared_ptr<Field> clone() const override
virtual std::unique_ptr<Field> clone() const override
{
return std::make_shared<DecimalField>(_places, _number);
return std::unique_ptr<Field>(new DecimalField(_places, _number));
}
/**

View File

@ -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<Field>
* @return std::unique_ptr<Field>
*/
static std::shared_ptr<Field> decode(InBuffer &frame);
static std::unique_ptr<Field> 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<Field> clone() const = 0;
virtual std::unique_ptr<Field> clone() const = 0;
/**
* Get the size this field will take when

View File

@ -93,12 +93,12 @@ public:
/**
* Create a new instance of this object
* @return Field*
* @return unique_ptr
*/
virtual std::shared_ptr<Field> clone() const override
virtual std::unique_ptr<Field> clone() const override
{
// create a new copy of ourselves and return it
return std::make_shared<NumericField>(_value);
return std::unique_ptr<Field>(new NumericField(_value));
}
/**

View File

@ -86,12 +86,12 @@ public:
/**
* Create a new instance of this object
* @return Field*
* @return std::unique_ptr<Field>
*/
virtual std::shared_ptr<Field> clone() const override
virtual std::unique_ptr<Field> clone() const override
{
// create a new copy of ourselves and return it
return std::make_shared<StringField>(_data);
return std::unique_ptr<Field>(new StringField(_data));
}
/**

View File

@ -33,7 +33,7 @@ private:
* We define a custom type for storing fields
* @typedef FieldMap
*/
typedef std::map<std::string, std::shared_ptr<Field> > FieldMap;
typedef std::map<std::string, std::unique_ptr<Field> > 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<Field> clone() const override
virtual std::unique_ptr<Field> clone() const override
{
return std::make_shared<Table>(*this);
return std::unique_ptr<Table>(new Table(*this));
}
/**

View File

@ -42,12 +42,12 @@ public:
/**
* Create a new instance of this object
* @return Field*
* @return unique_ptr
*/
virtual std::shared_ptr<Field> clone() const override
virtual std::unique_ptr<Field> clone() const override
{
// create a new copy of ourselves and return it
return std::make_shared<VoidField>();
return std::unique_ptr<Field>(new VoidField);
}
/**

View File

@ -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<uint32_t>(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());

View File

@ -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<Field>
* @return std::unique_ptr<Field>
*/
std::shared_ptr<Field> Field::decode(InBuffer &frame)
std::unique_ptr<Field> Field::decode(InBuffer &frame)
{
// get the type
uint8_t type = frame.nextUint8();
@ -24,24 +24,25 @@ std::shared_ptr<Field> Field::decode(InBuffer &frame)
// create field based on type
switch (type)
{
case 't': return std::make_shared<BooleanSet>(frame);
case 'b': return std::make_shared<Octet>(frame);
case 'B': return std::make_shared<UOctet>(frame);
case 'U': return std::make_shared<Short>(frame);
case 'u': return std::make_shared<UShort>(frame);
case 'I': return std::make_shared<Long>(frame);
case 'i': return std::make_shared<ULong>(frame);
case 'L': return std::make_shared<LongLong>(frame);
case 'l': return std::make_shared<ULongLong>(frame);
case 'f': return std::make_shared<Float>(frame);
case 'd': return std::make_shared<Double>(frame);
case 'D': return std::make_shared<DecimalField>(frame);
case 's': return std::make_shared<ShortString>(frame);
case 'S': return std::make_shared<LongString>(frame);
case 'A': return std::make_shared<Array>(frame);
case 'T': return std::make_shared<Timestamp>(frame);
case 'F': return std::make_shared<Table>(frame);
case 'V': return std::make_shared<VoidField>(frame);
// @todo: use std::make_unique when switching to C++14/17/20
case 't': return std::unique_ptr<Field>(new BooleanSet(frame));
case 'b': return std::unique_ptr<Field>(new Octet(frame));
case 'B': return std::unique_ptr<Field>(new UOctet(frame));
case 'U': return std::unique_ptr<Field>(new Short(frame));
case 'u': return std::unique_ptr<Field>(new UShort(frame));
case 'I': return std::unique_ptr<Field>(new Long(frame));
case 'i': return std::unique_ptr<Field>(new ULong(frame));
case 'L': return std::unique_ptr<Field>(new LongLong(frame));
case 'l': return std::unique_ptr<Field>(new ULongLong(frame));
case 'f': return std::unique_ptr<Field>(new Float(frame));
case 'd': return std::unique_ptr<Field>(new Double(frame));
case 'D': return std::unique_ptr<Field>(new DecimalField(frame));
case 's': return std::unique_ptr<Field>(new ShortString(frame));
case 'S': return std::unique_ptr<Field>(new LongString(frame));
case 'A': return std::unique_ptr<Field>(new Array(frame));
case 'T': return std::unique_ptr<Field>(new Timestamp(frame));
case 'F': return std::unique_ptr<Field>(new Table(frame));
case 'V': return std::unique_ptr<Field>(new VoidField(frame));
default: return nullptr;
}
}