From 41c1402d15efafd0addc9aa2d4f80058e5bf535d Mon Sep 17 00:00:00 2001 From: Luca Marturana Date: Wed, 2 Apr 2014 12:59:24 +0200 Subject: [PATCH] Const modifier to table get and some array operations --- include/array.h | 27 +++++++++++++++++++++++---- include/table.h | 2 +- src/array.cpp | 25 ++++++++++++++++++++----- src/table.cpp | 2 +- 4 files changed, 45 insertions(+), 11 deletions(-) diff --git a/include/array.h b/include/array.h index befd1d6..98f78c5 100644 --- a/include/array.h +++ b/include/array.h @@ -1,6 +1,6 @@ /** * AMQP field array - * + * * @copyright 2014 Copernica BV */ @@ -20,7 +20,7 @@ private: * @typedef */ typedef std::vector> FieldArray; - + /** * The actual fields * @var FieldArray @@ -30,7 +30,7 @@ private: public: /** * Constructor to construct an array from a received frame - * + * * @param frame received frame */ Array(ReceivedFrame &frame); @@ -99,6 +99,25 @@ public: */ const Field &get(uint8_t index); + /** + * Get number of elements on this array + * + * @return array size + */ + uint32_t count() const; + + /** + * Remove last element from array + */ + void pop_back(); + + /** + * Add field to end of array + * + * @param value + */ + void push_back(const Field &value); + /** * Get a field * @@ -111,7 +130,7 @@ public: } /** - * Write encoded payload to the given buffer. + * Write encoded payload to the given buffer. * @param buffer */ virtual void fill(OutBuffer& buffer) const override; diff --git a/include/table.h b/include/table.h index c26fb6d..5343769 100644 --- a/include/table.h +++ b/include/table.h @@ -109,7 +109,7 @@ public: * @param name field name * @return the field value */ - const Field &get(const std::string &name); + const Field &get(const std::string &name) const; /** * Get a field diff --git a/src/array.cpp b/src/array.cpp index 3b5831b..706572d 100644 --- a/src/array.cpp +++ b/src/array.cpp @@ -1,8 +1,8 @@ /** * Array.cpp - * + * * Implementation of an array - * + * */ #include "includes.h" @@ -23,7 +23,7 @@ Array::Array(ReceivedFrame &frame) { // one byte less for the field type charsToRead -= 1; - + // read the field type and construct the field Field *field = Field::decode(frame); if (!field) continue; @@ -62,7 +62,7 @@ const Field &Array::get(uint8_t index) { // used if index does not exist static ShortString empty; - + // check whether we have that many elements if (index >= _fields.size()) return empty; @@ -70,6 +70,21 @@ const Field &Array::get(uint8_t index) return *_fields[index]; } +uint32_t Array::count() const +{ + return _fields.size(); +} + +void Array::pop_back() +{ + _fields.pop_back(); +} + +void Array::push_back(const Field& value) +{ + _fields.push_back(std::shared_ptr(value.clone())); +} + /** * Get the size this field will take when * encoded in the AMQP wire-frame format @@ -92,7 +107,7 @@ size_t Array::size() const } /** - * Write encoded payload to the given buffer. + * Write encoded payload to the given buffer. */ void Array::fill(OutBuffer& buffer) const { diff --git a/src/table.cpp b/src/table.cpp index ba41983..4be9b11 100644 --- a/src/table.cpp +++ b/src/table.cpp @@ -97,7 +97,7 @@ Table &Table::operator=(Table &&table) * @param name field name * @return the field value */ -const Field &Table::get(const std::string &name) +const Field &Table::get(const std::string &name) const { // we need an empty string static ShortString empty;