2014-04-02 21:40:35 +08:00
|
|
|
#pragma once
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* AMQP field array
|
2014-04-02 18:59:24 +08:00
|
|
|
*
|
2014-01-04 19:45:04 +08:00
|
|
|
* @copyright 2014 Copernica BV
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set up namespace
|
|
|
|
|
*/
|
|
|
|
|
namespace AMQP {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AMQP field array
|
|
|
|
|
*/
|
|
|
|
|
class Array : public Field
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* Definition of an array as a vector
|
|
|
|
|
* @typedef
|
|
|
|
|
*/
|
|
|
|
|
typedef std::vector<std::shared_ptr<Field>> FieldArray;
|
2014-04-02 18:59:24 +08:00
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* The actual fields
|
|
|
|
|
* @var FieldArray
|
|
|
|
|
*/
|
|
|
|
|
FieldArray _fields;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Constructor to construct an array from a received frame
|
2014-04-02 18:59:24 +08:00
|
|
|
*
|
2014-01-04 19:45:04 +08:00
|
|
|
* @param frame received frame
|
|
|
|
|
*/
|
|
|
|
|
Array(ReceivedFrame &frame);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Copy constructor
|
|
|
|
|
* @param array
|
|
|
|
|
*/
|
|
|
|
|
Array(const Array &array);
|
|
|
|
|
|
2014-01-05 20:08:35 +08:00
|
|
|
/**
|
|
|
|
|
* Move constructor
|
|
|
|
|
* @param array
|
|
|
|
|
*/
|
|
|
|
|
Array(Array &&array) : _fields(std::move(array._fields)) {}
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Constructor for an empty Array
|
|
|
|
|
*/
|
|
|
|
|
Array() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor
|
|
|
|
|
*/
|
|
|
|
|
virtual ~Array() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new instance of this object
|
|
|
|
|
* @return Field*
|
|
|
|
|
*/
|
|
|
|
|
virtual Field *clone() const override
|
|
|
|
|
{
|
|
|
|
|
return new Array(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the size this field will take when
|
|
|
|
|
* encoded in the AMQP wire-frame format
|
|
|
|
|
* @return size_t
|
|
|
|
|
*/
|
|
|
|
|
virtual size_t size() const override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a field
|
|
|
|
|
*
|
|
|
|
|
* @param index field index
|
|
|
|
|
* @param value field value
|
|
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
Array set(uint8_t index, const Field &value)
|
|
|
|
|
{
|
2014-04-14 19:34:46 +08:00
|
|
|
// construct a shared pointer
|
|
|
|
|
auto ptr = std::shared_ptr<Field>(value.clone());
|
2014-04-14 20:18:51 +08:00
|
|
|
|
2014-04-14 19:34:46 +08:00
|
|
|
// should we overwrite an existing record?
|
2014-04-14 20:18:51 +08:00
|
|
|
if (index >= _fields.size())
|
2014-04-14 19:34:46 +08:00
|
|
|
{
|
|
|
|
|
// append index
|
|
|
|
|
_fields.push_back(ptr);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// overwrite pointer
|
|
|
|
|
_fields[index] = ptr;
|
|
|
|
|
}
|
2014-01-04 19:45:04 +08:00
|
|
|
|
|
|
|
|
// allow chaining
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a field
|
|
|
|
|
*
|
|
|
|
|
* If the field does not exist, an empty string is returned
|
|
|
|
|
*
|
|
|
|
|
* @param index field index
|
|
|
|
|
* @return Field
|
|
|
|
|
*/
|
2014-04-14 22:04:49 +08:00
|
|
|
const Field &get(uint8_t index) const;
|
2014-01-04 19:45:04 +08:00
|
|
|
|
2014-04-02 18:59:24 +08:00
|
|
|
/**
|
|
|
|
|
* Get number of elements on this array
|
|
|
|
|
*
|
|
|
|
|
* @return array size
|
|
|
|
|
*/
|
|
|
|
|
uint32_t count() const;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-14 19:34:46 +08:00
|
|
|
* Remove last element from array
|
2014-04-02 18:59:24 +08:00
|
|
|
*/
|
|
|
|
|
void pop_back();
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-14 19:34:46 +08:00
|
|
|
* Add field to end of array
|
2014-04-02 18:59:24 +08:00
|
|
|
*
|
2014-04-14 19:34:46 +08:00
|
|
|
* @param value
|
2014-04-02 18:59:24 +08:00
|
|
|
*/
|
|
|
|
|
void push_back(const Field &value);
|
|
|
|
|
|
2014-01-04 19:45:04 +08:00
|
|
|
/**
|
|
|
|
|
* Get a field
|
|
|
|
|
*
|
|
|
|
|
* @param index field index
|
|
|
|
|
* @return ArrayFieldProxy
|
|
|
|
|
*/
|
|
|
|
|
ArrayFieldProxy operator[](uint8_t index)
|
|
|
|
|
{
|
|
|
|
|
return ArrayFieldProxy(this, index);
|
|
|
|
|
}
|
2014-04-14 22:04:49 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a const field
|
|
|
|
|
* @param index field index
|
|
|
|
|
* @return Field
|
|
|
|
|
*/
|
|
|
|
|
const Field &operator[](uint8_t index) const
|
|
|
|
|
{
|
|
|
|
|
return get(index);
|
|
|
|
|
}
|
2014-01-04 19:45:04 +08:00
|
|
|
|
|
|
|
|
/**
|
2014-04-02 18:59:24 +08:00
|
|
|
* Write encoded payload to the given buffer.
|
2014-01-04 19:45:04 +08:00
|
|
|
* @param buffer
|
|
|
|
|
*/
|
|
|
|
|
virtual void fill(OutBuffer& buffer) const override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the type ID that is used to identify this type of
|
|
|
|
|
* field in a field table
|
|
|
|
|
* @return char
|
|
|
|
|
*/
|
|
|
|
|
virtual char typeID() const override
|
|
|
|
|
{
|
|
|
|
|
return 'A';
|
|
|
|
|
}
|
2014-04-14 19:34:46 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output the object to a stream
|
|
|
|
|
* @param std::ostream
|
|
|
|
|
*/
|
|
|
|
|
virtual void output(std::ostream &stream) const
|
|
|
|
|
{
|
|
|
|
|
// prefix
|
|
|
|
|
stream << "array(";
|
|
|
|
|
|
|
|
|
|
// is this the first iteration
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
|
|
// loop through all members
|
|
|
|
|
for (auto iter : _fields)
|
|
|
|
|
{
|
|
|
|
|
// split with comma
|
|
|
|
|
if (!first) stream << ",";
|
|
|
|
|
|
|
|
|
|
// show output
|
|
|
|
|
stream << *iter;
|
|
|
|
|
|
|
|
|
|
// no longer first iter
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// postfix
|
|
|
|
|
stream << ")";
|
|
|
|
|
}
|
2014-01-04 19:45:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* end namespace
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|