fixed operator[] for arrays and strings, implemented << operator for field and fieldproxy objects to simplify debugging (reported from issue #7)

This commit is contained in:
Emiel Bruijntjes 2014-04-14 13:34:46 +02:00
parent c252c53751
commit cf5def0e89
12 changed files with 197 additions and 108 deletions

View File

@ -17,6 +17,7 @@
#include <cstddef>
#include <cstring>
#include <stdexcept>
#include <iostream>
// base C include files
#include <stdint.h>

View File

@ -83,8 +83,20 @@ public:
*/
Array set(uint8_t index, const Field &value)
{
// copy to a new pointer and store it
_fields[index] = std::shared_ptr<Field>(value.clone());
// construct a shared pointer
auto ptr = std::shared_ptr<Field>(value.clone());
// should we overwrite an existing record?
if (index <= _fields.size())
{
// append index
_fields.push_back(ptr);
}
else
{
// overwrite pointer
_fields[index] = ptr;
}
// allow chaining
return *this;
@ -108,14 +120,14 @@ public:
uint32_t count() const;
/**
* Remove last element from array
* Remove last element from array
*/
void pop_back();
/**
* Add field to end of array
* Add field to end of array
*
* @param value
* @param value
*/
void push_back(const Field &value);
@ -145,6 +157,35 @@ public:
{
return 'A';
}
/**
* 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 << ")";
}
};
/**

View File

@ -83,6 +83,22 @@ public:
return new BooleanSet(*this);
}
/**
* Output the object to a stream
* @param std::ostream
*/
virtual void output(std::ostream &stream) const override
{
// prefix
stream << "booleanset(";
// the members
for (int i=0; i<8; i++) stream << (i == 0 ? "" : ",") << (get(i) ? 1 : 0);
// postfix
stream << ")";
}
/**
* Get one of the booleans
* @param index from 0 to 7, where 0 is rightmost bit

View File

@ -88,6 +88,16 @@ public:
return new DecimalField(_places, _number);
}
/**
* Output the object to a stream
* @param std::ostream
*/
virtual void output(std::ostream &stream) const override
{
// output floating point value
stream << "decimal(" << _number / pow(10, _places) << ")";
}
/**
* Assign a new value
*

View File

@ -59,9 +59,25 @@ public:
*/
virtual char typeID() const = 0;
/**
* Output the object to a stream
* @param std::ostream
*/
virtual void output(std::ostream &stream) const = 0;
};
/**
* Custom output stream operator
* @param stream
* @param field
* @return ostream
*/
inline std::ostream &operator<<(std::ostream &stream, const Field &field)
{
field.output(stream);
return stream;
}
/**
* end namespace
*/

View File

@ -196,21 +196,14 @@ public:
// cast to a string
return operator=(std::string(value));
}
/**
* Get boolean value
* @return BooleanSet
* Get the underlying field
* @return Field
*/
operator BooleanSet ()
const Field &get() const
{
// the value
BooleanSet value;
// retrieve the value
_source->get(_index, value);
// return the result
return value;
return _source->get(_index);
}
/**
@ -219,14 +212,8 @@ public:
*/
operator bool ()
{
// the value
BooleanSet value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -235,14 +222,8 @@ public:
*/
operator int8_t ()
{
// the value
Octet value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -251,14 +232,8 @@ public:
*/
operator uint8_t ()
{
// the value
UOctet value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -267,14 +242,8 @@ public:
*/
operator int16_t ()
{
// the value
Short value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -283,14 +252,8 @@ public:
*/
operator uint16_t ()
{
// the value
UShort value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -299,14 +262,8 @@ public:
*/
operator int32_t ()
{
// the value
Long value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -315,14 +272,8 @@ public:
*/
operator uint32_t ()
{
// the value
ULong value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -331,14 +282,8 @@ public:
*/
operator int64_t ()
{
// the value
Long value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -347,30 +292,8 @@ public:
*/
operator uint64_t ()
{
// the value
ULong value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
}
/**
* Get decimal value
* @return DecimalField
*/
operator DecimalField ()
{
// the value
DecimalField value;
// retrieve the value
_source->get(_index, value);
// return the result
return value.value();
return _source->get(_index);
}
/**
@ -379,16 +302,8 @@ public:
*/
operator std::string ()
{
// it has to be either a short or a long string
ShortString shortValue;
LongString longValue;
// try to retrieve the value
if (_source->get(_index, shortValue)) return shortValue.value();
if (_source->get(_index, longValue)) return longValue.value();
// no valid string found
return std::string("");
// retrieve the value
return _source->get(_index);
}
};
@ -396,6 +311,30 @@ public:
typedef FieldProxy<Table, std::string> AssociativeFieldProxy;
typedef FieldProxy<Array, uint8_t> ArrayFieldProxy;
/**
* Custom output stream operator
* @param stream
* @param field
* @return ostream
*/
inline std::ostream &operator<<(std::ostream &stream, const AssociativeFieldProxy &field)
{
// get underlying field, and output that
return stream << field.get();
}
/**
* Custom output stream operator
* @param stream
* @param field
* @return ostream
*/
inline std::ostream &operator<<(std::ostream &stream, const ArrayFieldProxy &field)
{
// get underlying field, and output that
return stream << field.get();
}
/**
* end namespace
*/

View File

@ -157,6 +157,16 @@ public:
{
return F;
}
/**
* Output the object to a stream
* @param std::ostream
*/
virtual void output(std::ostream &stream) const override
{
// show
stream << "numeric(" << value() << ")";
}
};
/**

View File

@ -145,6 +145,16 @@ public:
{
return F;
}
/**
* Output the object to a stream
* @param std::ostream
*/
virtual void output(std::ostream &stream) const override
{
// show
stream << "string(" << value() << ")";
}
};
/**

View File

@ -136,6 +136,35 @@ public:
{
return 'F';
}
/**
* Output the object to a stream
* @param std::ostream
*/
virtual void output(std::ostream &stream) const
{
// prefix
stream << "table(";
// 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.first << ":" << *iter.second;
// no longer first iter
first = false;
}
// postfix
stream << ")";
}
};
/**

View File

@ -1,6 +1,6 @@
CPP = g++
RM = rm -f
CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++11 -g
CPPFLAGS = -Wall -c -I. -g -flto -std=c++11 -g
LD = g++
LD_FLAGS = -Wall -shared -O2
SHARED_LIB = libamqpcpp.so

View File

@ -70,16 +70,27 @@ const Field &Array::get(uint8_t index)
return *_fields[index];
}
/**
* Number of entries in the array
* @return uint32_t
*/
uint32_t Array::count() const
{
return _fields.size();
}
/**
* Remove a field from the array
*/
void Array::pop_back()
{
_fields.pop_back();
}
/**
* Add a field to the array
* @param value
*/
void Array::push_back(const Field& value)
{
_fields.push_back(std::shared_ptr<Field>(value.clone()));
@ -88,6 +99,7 @@ void Array::push_back(const Field& value)
/**
* Get the size this field will take when
* encoded in the AMQP wire-frame format
* @return size_t
*/
size_t Array::size() const
{
@ -108,9 +120,11 @@ size_t Array::size() const
/**
* Write encoded payload to the given buffer.
* @param buffer
*/
void Array::fill(OutBuffer& buffer) const
{
// store total size for all elements
buffer.add(static_cast<uint32_t>(size()-4));
// iterate over all elements

View File

@ -7,6 +7,9 @@
* @documentation private
*/
// for debug
#include <iostream>
// include the generic amqp functions
#include "../amqpcpp.h"