Merged recent commits

This commit is contained in:
Martijn Otto 2014-04-14 14:23:37 +02:00
commit 05412e0d2b
11 changed files with 203 additions and 116 deletions

View File

@ -83,8 +83,20 @@ public:
*/ */
Array set(uint8_t index, const Field &value) Array set(uint8_t index, const Field &value)
{ {
// copy to a new pointer and store it // construct a shared pointer
_fields[index] = std::shared_ptr<Field>(value.clone()); 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 // allow chaining
return *this; return *this;
@ -108,14 +120,14 @@ public:
uint32_t count() const; uint32_t count() const;
/** /**
* Remove last element from array * Remove last element from array
*/ */
void pop_back(); 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); void push_back(const Field &value);
@ -145,6 +157,35 @@ public:
{ {
return 'A'; 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); 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 * Get one of the booleans
* @param index from 0 to 7, where 0 is rightmost bit * @param index from 0 to 7, where 0 is rightmost bit

View File

@ -25,8 +25,8 @@ public:
* Set a function to be executed after a given timeout. * Set a function to be executed after a given timeout.
* *
* This function is not strictly necessary to implement. If you * This function is not strictly necessary to implement. If you
* do not implement it, certain channel methods that fail * do not implement it, certain methods that fail immediately
* immediately will not be reported. * will not be reported.
* *
* @param timeout number of seconds to wait * @param timeout number of seconds to wait
* @param callback function to execute once time runs out * @param callback function to execute once time runs out

View File

@ -88,6 +88,16 @@ public:
return new DecimalField(_places, _number); 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 * Assign a new value
* *

View File

@ -59,9 +59,25 @@ public:
*/ */
virtual char typeID() const = 0; 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 * end namespace
*/ */

View File

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

View File

@ -157,6 +157,16 @@ public:
{ {
return F; 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; 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'; 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

@ -70,16 +70,27 @@ const Field &Array::get(uint8_t index)
return *_fields[index]; return *_fields[index];
} }
/**
* Number of entries in the array
* @return uint32_t
*/
uint32_t Array::count() const uint32_t Array::count() const
{ {
return _fields.size(); return _fields.size();
} }
/**
* Remove a field from the array
*/
void Array::pop_back() void Array::pop_back()
{ {
_fields.pop_back(); _fields.pop_back();
} }
/**
* Add a field to the array
* @param value
*/
void Array::push_back(const Field& value) void Array::push_back(const Field& value)
{ {
_fields.push_back(std::shared_ptr<Field>(value.clone())); _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 * Get the size this field will take when
* encoded in the AMQP wire-frame format * encoded in the AMQP wire-frame format
* @return size_t
*/ */
size_t Array::size() const size_t Array::size() const
{ {
@ -95,11 +107,11 @@ size_t Array::size() const
size_t size = 4; size_t size = 4;
// iterate over all elements // iterate over all elements
for (auto iter(_fields.begin()); iter != _fields.end(); ++iter) for (auto item : _fields)
{ {
// add the size of the field type and size of element // add the size of the field type and size of element
size += sizeof((*iter)->typeID()); size += sizeof(item->typeID());
size += (*iter)->size(); size += item->size();
} }
// return the result // return the result
@ -108,15 +120,19 @@ size_t Array::size() const
/** /**
* Write encoded payload to the given buffer. * Write encoded payload to the given buffer.
* @param buffer
*/ */
void Array::fill(OutBuffer& buffer) const void Array::fill(OutBuffer& buffer) const
{ {
// store total size for all elements
buffer.add(static_cast<uint32_t>(size()-4));
// iterate over all elements // iterate over all elements
for (auto iter(_fields.begin()); iter != _fields.end(); ++iter) for (auto item : _fields)
{ {
// encode the element type and element // encode the element type and element
buffer.add((*iter)->typeID()); buffer.add((uint8_t)item->typeID());
(*iter)->fill(buffer); item->fill(buffer);
} }
} }

View File

@ -145,7 +145,7 @@ size_t Table::size() const
void Table::fill(OutBuffer& buffer) const void Table::fill(OutBuffer& buffer) const
{ {
// add size // add size
buffer.add((uint32_t) size()-4); buffer.add(static_cast<uint32_t>(size()-4));
// loop through the fields // loop through the fields
for (auto iter(_fields.begin()); iter != _fields.end(); ++iter) for (auto iter(_fields.begin()); iter != _fields.end(); ++iter)