DynamicStruct keyed by integer

This commit is contained in:
Aleksandar Fabijanic 2008-11-24 12:36:15 +00:00
parent 721062b0d3
commit d43f4d6164
6 changed files with 325 additions and 222 deletions

View File

@ -114,21 +114,21 @@ public:
}
reference operator[](size_type i)
/// element access without range check. If the index is not small than the given size, the behavior is undefined.
/// Element access without range check. If the index is not small than the given size, the behavior is undefined.
{
poco_assert( i < N && "out of range" );
return elems[i];
}
const_reference operator[](size_type i) const
/// element access without range check. If the index is not small than the given size, the behavior is undefined.
/// Element access without range check. If the index is not small than the given size, the behavior is undefined.
{
poco_assert( i < N && "out of range" );
return elems[i];
}
reference at(size_type i)
/// element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
/// Element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
{
if(i>=size())
throw Poco::InvalidArgumentException("Array::at() range check failed: index is over range");
@ -136,14 +136,13 @@ public:
}
const_reference at(size_type i) const
/// element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
/// Element access with range check. Throws Poco::InvalidArgumentException if the index is over range.
{
if(i>=size())
throw Poco::InvalidArgumentException("Array::at() range check failed: index is over range");
return elems[i];
}
// front() and back()
reference front()
{
return elems[0];
@ -186,7 +185,7 @@ public:
}
const T* data() const
/// direct access to data (read-only)
/// Direct access to data (read-only)
{
return elems;
}
@ -197,27 +196,28 @@ public:
}
T* c_array(){
/// use array as C array (direct read/write access to data)
/// Use array as C array (direct read/write access to data)
return elems;
}
template <typename Other>
Array<T,N>& operator= (const Array<Other,N>& rhs)
/// assignment with type conversion
/// Assignment with type conversion
{
std::copy(rhs.begin(),rhs.end(), begin());
return *this;
}
void assign (const T& value)
/// assign one value to all elements
/// Assign one value to all elements
{
std::fill_n(begin(),size(),value);
}
public:
T elems[N]; // fixed-size array of elements of type T, public specifier used to make this class a aggregate.
T elems[N];
/// Fixed-size array of elements of type T, public specifier used to make this class a aggregate.
};

View File

@ -51,180 +51,157 @@ namespace Poco {
namespace Dynamic {
class Foundation_API Struct
template <typename K>
class Struct
/// Struct allows to define a named collection of Var objects.
{
public:
typedef std::map<std::string, Var> Data;
typedef Data::iterator Iterator;
typedef Data::const_iterator ConstIterator;
typedef typename std::map<K, Var> Data;
typedef typename std::set<K> NameSet;
typedef typename Data::iterator Iterator;
typedef typename Data::const_iterator ConstIterator;
typedef typename Struct<K>::Data::value_type ValueType;
typedef typename Struct<K>::Data::size_type SizeType;
typedef typename std::pair<typename Struct<K>::Iterator, bool> InsRetVal;
Struct();
Struct(): _data()
/// Creates an empty Struct
{
}
Struct(const Data &val);
Struct(const Data &val): _data(val)
/// Creates the Struct from the given value.
virtual ~Struct();
{
}
virtual ~Struct()
/// Destroys the Struct.
{
}
Var& operator [] (const std::string& name);
inline Var& operator [] (const K& name)
/// Returns the Var with the given name, creates an entry if not found.
{
return _data[name];
}
const Var& operator [] (const std::string& name) const;
const Var& operator [] (const K& name) const
/// Returns the Var with the given name, throws a
/// NotFoundException if the data member is not found.
{
ConstIterator it = find(name);
if (it == end()) throw NotFoundException(name);
return it->second;
}
bool contains(const std::string& name) const;
/// Returns true if the Struct contains a member with the given
/// name
inline bool contains(const K& name) const
/// Returns true if the Struct contains a member with the given name
{
return find(name) != end();
}
Iterator find(const std::string& name);
inline Iterator find(const K& name)
/// Returns an iterator, pointing to the <name,Var> pair containing
/// the element, or it returns end() if the member was not found
{
return _data.find(name);
}
ConstIterator find(const std::string& name) const;
inline ConstIterator find(const K& name) const
/// Returns a const iterator, pointing to the <name,Var> pair containing
/// the element, or it returns end() if the member was not found
{
return _data.find(name);
}
Iterator end();
inline Iterator end()
/// Returns the end iterator for the Struct
{
return _data.end();
}
ConstIterator end() const;
inline ConstIterator end() const
/// Returns the end const iterator for the Struct
{
return _data.end();
}
Iterator begin();
inline Iterator begin()
/// Returns the begin iterator for the Struct
{
return _data.begin();
}
ConstIterator begin() const;
inline ConstIterator begin() const
/// Returns the begin const iterator for the Struct
{
return _data.begin();
}
std::pair<Struct::Iterator, bool> insert(const std::string& key, const Var& value);
inline InsRetVal insert(const K& key, const Var& value)
/// Inserts a <name, Var> pair into the Struct,
/// returns a pair containing the iterator and a boolean which
/// indicates success or not (is true, when insert succeeded, false,
/// when already another element was present, in this case Iterator
/// points to that other element)
{
return insert(std::make_pair(key, value));
}
std::pair<Struct::Iterator, bool> insert(const Struct::Data::value_type& aPair);
inline InsRetVal insert(const ValueType& aPair)
/// Inserts a <name, Var> pair into the Struct,
/// returns a pair containing the iterator and a boolean which
/// indicates success or not (is true, when insert succeeded, false,
/// when already another element was present, in this case Iterator
/// points to that other element)
{
return _data.insert(aPair);
}
Struct::Data::size_type erase(const std::string& key);
inline SizeType erase(const K& key)
/// Erases the element if found, returns number of elements deleted
{
return _data.erase(key);
}
void erase(Struct::Iterator it);
inline void erase(Iterator& it)
/// Erases the element at the given position
{
_data.erase(it);
}
bool empty() const;
inline bool empty() const
/// Returns true if the Struct doesn't contain any members
{
return _data.empty();
}
Struct::Data::size_type size() const;
SizeType size() const
/// Returns the number of members the Struct contains
{
return _data.size();
}
std::set<std::string> members() const;
inline NameSet members() const
/// Returns a sorted collection containing all member names
{
NameSet keys;
ConstIterator it = begin();
ConstIterator itEnd = end();
for (; it != itEnd; ++it) keys.insert(it->first);
return keys;
}
private:
Data _data;
};
//
// inlines
//
inline Var& Struct::operator [] (const std::string& name)
{
return _data[name];
}
inline bool Struct::contains(const std::string& name) const
{
return find(name) != end();
}
inline Struct::Iterator Struct::find(const std::string& name)
{
return _data.find(name);
}
inline Struct::ConstIterator Struct::find(const std::string& name) const
{
return _data.find(name);
}
inline Struct::Iterator Struct::end()
{
return _data.end();
}
inline Struct::ConstIterator Struct::end() const
{
return _data.end();
}
inline Struct::Iterator Struct::begin()
{
return _data.begin();
}
inline Struct::ConstIterator Struct::begin() const
{
return _data.begin();
}
inline std::pair<Struct::Iterator, bool> Struct::insert(const std::string& key, const Var& value)
{
return insert(std::make_pair(key, value));
}
inline std::pair<Struct::Iterator, bool> Struct::insert(const Struct::Data::value_type& aPair)
{
return _data.insert(aPair);
}
inline Struct::Data::size_type Struct::erase(const std::string& key)
{
return _data.erase(key);
}
inline void Struct::erase(Struct::Iterator it)
{
_data.erase(it);
}
inline bool Struct::empty() const
{
return _data.empty();
}
inline Struct::Data::size_type Struct::size() const
{
return _data.size();
}
template <>
class VarHolderImpl<Struct>: public VarHolder
class VarHolderImpl<Struct<std::string> >: public VarHolder
{
public:
VarHolderImpl(const Struct& val): _val(val)
VarHolderImpl(const Struct<std::string>& val): _val(val)
{
}
@ -234,7 +211,7 @@ public:
const std::type_info& type() const
{
return typeid(Struct);
return typeid(Struct<std::string>);
}
void convert(Int8& val) const
@ -303,8 +280,8 @@ public:
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ ");
Struct::ConstIterator it = _val.begin();
Struct::ConstIterator itEnd = _val.end();
Struct<std::string>::ConstIterator it = _val.begin();
Struct<std::string>::ConstIterator itEnd = _val.end();
if (!_val.empty())
{
Var key(it->first);
@ -344,7 +321,7 @@ public:
return new VarHolderImpl(_val);
}
const Struct& value() const
const Struct<std::string>& value() const
{
return _val;
}
@ -390,7 +367,181 @@ public:
}
private:
Struct _val;
Struct<std::string> _val;
};
template <>
class VarHolderImpl<Struct<int> >: public VarHolder
{
public:
VarHolderImpl(const Struct<int>& val): _val(val)
{
}
~VarHolderImpl()
{
}
const std::type_info& type() const
{
return typeid(Struct<int>);
}
void convert(Int8& val) const
{
throw BadCastException("Cannot cast Struct type to Int8");
}
void convert(Int16& val) const
{
throw BadCastException("Cannot cast Struct type to Int16");
}
void convert(Int32& val) const
{
throw BadCastException("Cannot cast Struct type to Int32");
}
void convert(Int64& val) const
{
throw BadCastException("Cannot cast Struct type to Int64");
}
void convert(UInt8& val) const
{
throw BadCastException("Cannot cast Struct type to UInt8");
}
void convert(UInt16& val) const
{
throw BadCastException("Cannot cast Struct type to UInt16");
}
void convert(UInt32& val) const
{
throw BadCastException("Cannot cast Struct type to UInt32");
}
void convert(UInt64& val) const
{
throw BadCastException("Cannot cast Struct type to UInt64");
}
void convert(bool& val) const
{
throw BadCastException("Cannot cast Struct type to bool");
}
void convert(float& val) const
{
throw BadCastException("Cannot cast Struct type to float");
}
void convert(double& val) const
{
throw BadCastException("Cannot cast Struct type to double");
}
void convert(char& val) const
{
throw BadCastException("Cannot cast Struct type to char");
}
void convert(std::string& val) const
{
// Serialize in JSON format: equals an object
// JSON format definition: { string ':' value } string:value pair n-times, sep. by ','
val.append("{ ");
Struct<int>::ConstIterator it = _val.begin();
Struct<int>::ConstIterator itEnd = _val.end();
if (!_val.empty())
{
Var key(it->first);
appendJSONString(val, key);
val.append(" : ");
appendJSONString(val, it->second);
++it;
}
for (; it != itEnd; ++it)
{
val.append(", ");
Var key(it->first);
appendJSONString(val, key);
val.append(" : ");
appendJSONString(val, it->second);
}
val.append(" }");
}
void convert(Poco::DateTime&) const
{
throw BadCastException("Struct -> Poco::DateTime");
}
void convert(Poco::LocalDateTime&) const
{
throw BadCastException("Struct -> Poco::LocalDateTime");
}
void convert(Poco::Timestamp&) const
{
throw BadCastException("Struct -> Poco::Timestamp");
}
VarHolder* clone() const
{
return new VarHolderImpl(_val);
}
const Struct<int>& value() const
{
return _val;
}
bool isArray() const
{
return false;
}
bool isStruct() const
{
return true;
}
bool isInteger() const
{
return false;
}
bool isSigned() const
{
return false;
}
bool isNumeric() const
{
return false;
}
bool isString() const
{
return false;
}
Var& operator [] (std::size_t name)
{
return _val[name];
}
const Var& operator [] (std::size_t name) const
{
return _val[name];
}
private:
Struct<int> _val;
};
@ -398,7 +549,7 @@ private:
//@ deprecated
typedef Dynamic::Struct DynamicStruct;
typedef Dynamic::Struct<std::string> DynamicStruct;
} // namespace Poco

View File

@ -50,6 +50,10 @@ namespace Poco {
namespace Dynamic {
template <typename T>
class Struct;
class Foundation_API Var
/// Var allows to store data of different types and to convert between these types transparently.
/// Var puts forth the best effort to provide intuitive and reasonable conversion semantics and prevent
@ -424,20 +428,32 @@ public:
template <typename T>
Var& operator [] (T n)
/// Index operator, only use on Vars where isArray
/// Index operator, only use on Vars where isArray() or isStruct()
/// returns true! In all other cases InvalidAccessException is thrown.
{
return holderImpl<std::vector<Var>,
InvalidAccessException>("Not an array.")->operator[](n);
if (isArray())
return holderImpl<std::vector<Var>,
InvalidAccessException>("Not an array.")->operator[](n);
else if (isStruct())
return holderImpl<Struct<int>,
InvalidAccessException>("Not a struct.")->operator[](n);
else
throw InvalidAccessException("Must be struct or array.");
}
template <typename T>
const Var& operator [] (T n) const
/// const Index operator, only use on Vars where isArray
/// const Index operator, only use on Vars where isArray() or isStruct()
/// returns true! In all other cases InvalidAccessException is thrown.
{
return const_cast<const Var&>(holderImpl<std::vector<Var>,
InvalidAccessException>("Not an array.")->operator[](n));
if (isArray())
return const_cast<const Var&>(holderImpl<std::vector<Var>,
InvalidAccessException>("Not an array.")->operator[](n));
else if (isStruct())
return const_cast<const Var&>(holderImpl<Struct<int>,
InvalidAccessException>("Not a struct.")->operator[](n));
else
throw InvalidAccessException("Must be struct or array.");
}
Var& operator [] (const std::string& name);

View File

@ -1,82 +0,0 @@
//
// Struct.cpp
//
// $Id: //poco/Main/Foundation/src/Struct.cpp#4 $
//
// Library: Foundation
// Package: Core
// Module: Struct
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Dynamic/Struct.h"
#include "Poco/Exception.h"
namespace Poco {
namespace Dynamic {
Struct::Struct():
_data()
{
}
Struct::Struct(const Data& d):
_data(d)
{
}
Struct::~Struct()
{
}
const Var& Struct::operator[](const std::string& name) const
{
ConstIterator it = find(name);
if (it == end())
throw NotFoundException(name);
return it->second;
}
std::set<std::string> Struct::members() const
{
std::set<std::string> keys;
ConstIterator it = begin();
ConstIterator itEnd = end();
for (; it != itEnd; ++it)
keys.insert(it->first);
return keys;
}
} } // namespace Poco::Dynamic

View File

@ -1940,7 +1940,7 @@ void VarTest::testDynamicStructBasics()
}
void VarTest::testDynamicStruct()
void VarTest::testDynamicStructString()
{
DynamicStruct aStruct;
aStruct["First Name"] = "Junior";
@ -1954,6 +1954,22 @@ void VarTest::testDynamicStruct()
}
void VarTest::testDynamicStructInt()
{
Dynamic::Struct<int> aStruct;
aStruct[0] = "Junior";
aStruct[1] = "POCO";
Var a1(aStruct);
assert (a1[0] == "Junior");
assert (a1[1] == "POCO");
a1[0] = "Senior";
assert (a1[0] == "Senior");
}
void VarTest::testArrayToString()
{
std::string s1("string");
@ -2380,7 +2396,8 @@ CppUnit::Test* VarTest::suite()
CppUnit_addTest(pSuite, VarTest, testIsArray);
CppUnit_addTest(pSuite, VarTest, testArrayIdxOperator);
CppUnit_addTest(pSuite, VarTest, testDynamicStructBasics);
CppUnit_addTest(pSuite, VarTest, testDynamicStruct);
CppUnit_addTest(pSuite, VarTest, testDynamicStructString);
CppUnit_addTest(pSuite, VarTest, testDynamicStructInt);
CppUnit_addTest(pSuite, VarTest, testArrayToString);
CppUnit_addTest(pSuite, VarTest, testStructToString);
CppUnit_addTest(pSuite, VarTest, testArrayOfStructsToString);

View File

@ -73,7 +73,8 @@ public:
void testIsArray();
void testArrayIdxOperator();
void testDynamicStructBasics();
void testDynamicStruct();
void testDynamicStructString();
void testDynamicStructInt();
void testArrayToString();
void testStructToString();
void testArrayOfStructsToString();