Skip to content

Commit

Permalink
Added "const" accessors to Row
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya Lutsenko committed Sep 1, 2015
1 parent 0e691d4 commit 1d89fe4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
23 changes: 22 additions & 1 deletion Data/include/Poco/Data/Row.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ class Data_API Row
Poco::Dynamic::Var& operator [] (const std::string& name);
/// Returns the reference to data value at named column location.

const Poco::Dynamic::Var& get(std::size_t col) const;
/// Returns the reference to data value at column location.

const Poco::Dynamic::Var& operator [] (std::size_t col) const;
/// Returns the reference to data value at column location.

const Poco::Dynamic::Var& operator [] (const std::string& name) const;
/// Returns the reference to data value at named column location.

template <typename T>
void append(const std::string& name, const T& val)
/// Appends the value to the row.
Expand Down Expand Up @@ -225,7 +234,7 @@ class Data_API Row
ValueVec& values();
/// Returns the reference to values vector.

std::size_t getPosition(const std::string& name);
std::size_t getPosition(const std::string& name) const;
bool isEqualSize(const Row& other) const;
bool isEqualType(const Row& other) const;

Expand Down Expand Up @@ -287,6 +296,18 @@ inline Poco::Dynamic::Var& Row::operator [] (const std::string& name)
}


inline const Poco::Dynamic::Var& Row::operator [] (std::size_t col) const
{
return get(col);
}


inline const Poco::Dynamic::Var& Row::operator [] (const std::string& name) const
{
return get(getPosition(name));
}


inline const RowFormatter& Row::getFormatter() const
{
return *_pFormatter;
Expand Down
15 changes: 14 additions & 1 deletion Data/src/Row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,20 @@ Poco::Dynamic::Var& Row::get(std::size_t col)
}


std::size_t Row::getPosition(const std::string& name)
const Poco::Dynamic::Var& Row::get(std::size_t col) const
{
try
{
return _values.at(col);
}
catch (std::out_of_range& re)
{
throw RangeException(re.what());
}
}


std::size_t Row::getPosition(const std::string& name) const
{
if (!_pNames)
throw NullPointerException();
Expand Down
5 changes: 5 additions & 0 deletions Data/testsuite/src/DataTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,11 @@ void DataTest::testRow()
assert (row[3] == 3);
assert (row[4] == 4);

const Row& cr = row;
assert(cr["field0"] == 0);
assert(cr[0] == 0);
assert(cr.get(0) == 0);

try
{
int i; i = row[5].convert<int>(); // to silence gcc
Expand Down

0 comments on commit 1d89fe4

Please sign in to comment.