Add checkNull and documentation

This commit is contained in:
fbraem 2015-10-26 15:20:49 +01:00
parent 4dc1d2f62d
commit a46ef4144e
2 changed files with 32 additions and 8 deletions

View File

@ -29,42 +29,62 @@ namespace Poco {
namespace Redis { namespace Redis {
class Redis_API Array class Redis_API Array
/// Represents a Redis Array. /// Represents a Redis Array. An Array can contain Integers, Strings,
/// Bulk Strings, Errors and other arrays. It can also contain a Null
/// value.
{ {
public: public:
Array(); Array();
/// Default constructor. As long as there are no elements added,
/// the array will contain a Null value.
Array(const Array& copy); Array(const Array& copy);
/// Copy constructor.
virtual ~Array(); virtual ~Array();
/// Destructor.
void add(Poco::Int64 value); void add(Poco::Int64 value);
/// Adds an integer element.
void add(const std::string& value); void add(const std::string& value);
/// Adds a simple string element (can't contain a newline!).
void add(const BulkString& value); void add(const BulkString& value);
/// Adds a bulk string element.
void add(); void add();
/// Adds a Null bulk string element.
void add(RedisType::Ptr value); void add(RedisType::Ptr value);
/// Adds a Redis element.
std::vector<RedisType::Ptr>::const_iterator begin() const; std::vector<RedisType::Ptr>::const_iterator begin() const;
/// Returns an iterator to the start of the array. Note:
/// this can throw a NullValueException when this is a Null array.
void clear(); void clear();
std::vector<RedisType::Ptr>::const_iterator end() const; std::vector<RedisType::Ptr>::const_iterator end() const;
/// Returns an iterator to the end of the array. Note:
/// this can throw a NullValueException when this is a Null array.
bool isNull() const; bool isNull() const;
/// Returns true when this is a Null array.
std::string toString() const; std::string toString() const;
/// Returns the String representation as specified in the
/// Redis Protocol specification.
size_t size() const; size_t size() const;
/// Returns the size of the array. Note:
/// this can throw a NullValueException when this is a Null array.
private: private:
Nullable<std::vector<RedisType::Ptr> > _elements; Nullable<std::vector<RedisType::Ptr> > _elements;
static std::vector<RedisType::Ptr> _empty; void checkNull();
}; };
inline std::vector<RedisType::Ptr>::const_iterator Array::begin() const inline std::vector<RedisType::Ptr>::const_iterator Array::begin() const
@ -72,6 +92,12 @@ inline std::vector<RedisType::Ptr>::const_iterator Array::begin() const
return _elements.value().begin(); return _elements.value().begin();
} }
inline void Array::checkNull()
{
std::vector<RedisType::Ptr> v;
if ( _elements.isNull() ) _elements.assign(v);
}
inline void Array::clear() inline void Array::clear()
{ {
if ( !_elements.isNull() ) if ( !_elements.isNull() )

View File

@ -20,8 +20,6 @@
namespace Poco { namespace Poco {
namespace Redis { namespace Redis {
std::vector<RedisType::Ptr> Array::_empty;
Array::Array() Array::Array()
{ {
@ -39,14 +37,14 @@ Array::~Array()
void Array::add(Int64 value) void Array::add(Int64 value)
{ {
if ( _elements.isNull() ) _elements.assign(_empty); checkNull();
_elements.value().push_back(new Type<Int64>(value)); _elements.value().push_back(new Type<Int64>(value));
} }
void Array::add(const std::string& value) void Array::add(const std::string& value)
{ {
if ( _elements.isNull() ) _elements.assign(_empty); checkNull();
BulkString rs(value); BulkString rs(value);
_elements.value().push_back(new Type<BulkString>(rs)); _elements.value().push_back(new Type<BulkString>(rs));
@ -54,14 +52,14 @@ void Array::add(const std::string& value)
void Array::add(const BulkString& value) void Array::add(const BulkString& value)
{ {
if ( _elements.isNull() ) _elements.assign(_empty); checkNull();
_elements.value().push_back(new Type<BulkString>(value)); _elements.value().push_back(new Type<BulkString>(value));
} }
void Array::add() void Array::add()
{ {
if ( _elements.isNull() ) _elements.assign(_empty); checkNull();
BulkString value; BulkString value;
_elements.value().push_back(new Type<BulkString>(value)); _elements.value().push_back(new Type<BulkString>(value));