ENH: Remove non const getValuesPointer for now. Rename getValues as...

ENH: Remove non const getValuesPointer for now.  Rename getValues as getValuesString() and add getValues that returns a smart pointer to the internal array.
parent 4e98359c
......@@ -136,8 +136,8 @@ class XdmfArrayPtr(_object):
def getHDF5Type(self): return _Xdmf.XdmfArrayPtr_getHDF5Type(self)
def getPrecision(self): return _Xdmf.XdmfArrayPtr_getPrecision(self)
def getSize(self): return _Xdmf.XdmfArrayPtr_getSize(self)
def getValues(self): return _Xdmf.XdmfArrayPtr_getValues(self)
def getValuesPointer(self, *args): return _Xdmf.XdmfArrayPtr_getValuesPointer(self, *args)
def getValuesPointer(self): return _Xdmf.XdmfArrayPtr_getValuesPointer(self)
def getValuesString(self): return _Xdmf.XdmfArrayPtr_getValuesString(self)
def printSelf(self): return _Xdmf.XdmfArrayPtr_printSelf(self)
def write(self, *args): return _Xdmf.XdmfArrayPtr_write(self, *args)
def copyValues(self, *args): return _Xdmf.XdmfArrayPtr_copyValues(self, *args)
......@@ -534,8 +534,8 @@ class XdmfArray(XdmfItem):
def getHDF5Type(self): return _Xdmf.XdmfArray_getHDF5Type(self)
def getPrecision(self): return _Xdmf.XdmfArray_getPrecision(self)
def getSize(self): return _Xdmf.XdmfArray_getSize(self)
def getValues(self): return _Xdmf.XdmfArray_getValues(self)
def getValuesPointer(self, *args): return _Xdmf.XdmfArray_getValuesPointer(self, *args)
def getValuesPointer(self): return _Xdmf.XdmfArray_getValuesPointer(self)
def getValuesString(self): return _Xdmf.XdmfArray_getValuesString(self)
def printSelf(self): return _Xdmf.XdmfArray_printSelf(self)
def write(self, *args): return _Xdmf.XdmfArray_write(self, *args)
def copyValues(self, *args): return _Xdmf.XdmfArray_copyValues(self, *args)
......
......@@ -178,10 +178,23 @@ public:
}
};
class XdmfArrayGetValues : public boost::static_visitor <std::string> {
class XdmfArrayGetValuesPointer : public boost::static_visitor <const void* const> {
public:
XdmfArrayGetValues()
XdmfArrayGetValuesPointer()
{
}
template<typename T> const void* const operator()(const boost::shared_ptr<std::vector<T> > array) const
{
return &array->operator[](0);
}
};
class XdmfArrayGetValuesString : public boost::static_visitor <std::string> {
public:
XdmfArrayGetValuesString()
{
}
......@@ -206,31 +219,6 @@ public:
}
};
class XdmfArrayGetValuesPointer : public boost::static_visitor <void*> {
public:
XdmfArrayGetValuesPointer()
{
}
template<typename T> void* operator()(boost::shared_ptr<std::vector<T> > array) const
{
return &array->operator[](0);
}
};
class XdmfArrayGetValuesPointerConst : public boost::static_visitor <const void* const> {
public:
XdmfArrayGetValuesPointerConst()
{
}
template<typename T> const void* const operator()(const boost::shared_ptr<std::vector<T> > array) const
{
return &array->operator[](0);
}
};
XdmfArray::XdmfArray() :
mInitialized(false)
{
......@@ -262,19 +250,19 @@ std::string XdmfArray::getType() const
return boost::apply_visitor( XdmfArrayGetType(), mArray);
}
std::string XdmfArray::getValues() const
{
return boost::apply_visitor( XdmfArrayGetValues(), mArray);
}
//const boost::shared_ptr<std::vector<void> > getValues() const
//{
//return boost::apply_visitor( XdmfArrayGetValues(), mArray);
//}
void* XdmfArray::getValuesPointer()
const void* const XdmfArray::getValuesPointer() const
{
return boost::apply_visitor( XdmfArrayGetValuesPointer(), mArray);
}
const void* const XdmfArray::getValuesPointer() const
std::string XdmfArray::getValuesString() const
{
return boost::apply_visitor( XdmfArrayGetValuesPointerConst(), mArray);
return boost::apply_visitor( XdmfArrayGetValuesString(), mArray);
}
std::string XdmfArray::printSelf() const
......
......@@ -14,6 +14,26 @@
#include <hdf5.h>
#include <vector>
template <typename T>
class XdmfArrayGetValues : public boost::static_visitor <boost::shared_ptr<std::vector<T> > > {
public:
XdmfArrayGetValues()
{
}
boost::shared_ptr<std::vector<T> > operator()(const boost::shared_ptr<std::vector<T> > array) const
{
return array;
}
template <typename U>
boost::shared_ptr<std::vector<T> > operator()(const boost::shared_ptr<std::vector<U> > array) const
{
// Not implemented
}
};
template<typename T>
class XdmfArraySetValues : public boost::static_visitor <void> {
public:
......@@ -99,18 +119,22 @@ public:
virtual int getSize() const;
/**
* Get the values stored in this array as a string.
* Get a smart pointer to the values stored in this array
*
* @return a string containing the contents of the array.
* @return a smart pointer to the internal vector of values stored in this array
*/
virtual std::string getValues() const;
template <typename T>
boost::shared_ptr<std::vector<T> > getValues()
{
return boost::apply_visitor( XdmfArrayGetValues<T>(), mArray);
}
/**
* Get a pointer to the values stored in this array.
* Get a smart pointer to the values stored in this array (const version)
*
* @return a void pointer to the first value stored in this array.
* @return a smart pointer to the internal vector of values stored in this array
*/
virtual void* getValuesPointer();
//virtual const boost::shared_ptr<std::vector<void> > getValues() const;
/**
* Get a pointer to the values stored in this array (const version).
......@@ -119,6 +143,13 @@ public:
*/
virtual const void* const getValuesPointer() const;
/**
* Get the values stored in this array as a string.
*
* @return a string containing the contents of the array.
*/
virtual std::string getValuesString() const;
/**
* Insert the values from a pointer into this array.
*
......
This diff is collapsed.
......@@ -103,7 +103,7 @@ void XdmfVisitor::visit(const XdmfArray * const array)
}
else
{
xmlTextValues << array->getValues();
xmlTextValues << array->getValuesString();
}
xmlAddChild(xmlCurrentNode, xmlNewText((xmlChar*)xmlTextValues.str().c_str()));
......
......@@ -12,10 +12,18 @@ int main(int argc, char* argv[])
assert(array->getSize() == 9);
assert(array->getType().compare("Float") == 0);
assert(array->getPrecision() == 8);
assert(array->getValues().compare("1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 ") == 0);
assert(array->getValuesString().compare("1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 ") == 0);
// Assert we copied values correctly
boost::shared_ptr<std::vector<double> > storedValues = array->getValues<double>();
int i = 0;
for(std::vector<double>::const_iterator iter = storedValues->begin(); iter!= storedValues->end(); ++iter, ++i)
{
assert(*iter == values[i]);
}
// Now modify original array
values[0] = 0;
assert(array->getValues().compare("1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 ") == 0);
assert(array->getValuesString().compare("1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 ") == 0);
/**
* Test simple copy from vector
......@@ -28,13 +36,13 @@ int main(int argc, char* argv[])
assert(array2->getSize() == 2);
assert(array2->getType().compare("Int") == 0);
assert(array2->getPrecision() == 4);
assert(array2->getValues().compare("100 200 ") == 0);
assert(array2->getValuesString().compare("100 200 ") == 0);
// Now modify original array
values2.push_back(300);
assert(array2->getSize() == 2);
assert(array2->getType().compare("Int") == 0);
assert(array2->getPrecision() == 4);
assert(array2->getValues().compare("100 200 ") == 0);
assert(array2->getValuesString().compare("100 200 ") == 0);
/**
* Test shared vector assignment
......@@ -50,12 +58,11 @@ int main(int argc, char* argv[])
assert(array3->getSize() == 5);
assert(array3->getType().compare("Char") == 0);
assert(array3->getPrecision() == 1);
assert(array3->getValues().compare("-2 -1 0 1 2 ") == 0);
assert(array3->getValuesString().compare("-2 -1 0 1 2 ") == 0);
// Now modify original array
values3->push_back(8);
assert(array3->getSize() == 6);
std::cout << array3->getValues() << std::endl;
assert(array3->getValues().compare("-2 -1 0 1 2 8 ") == 0);
assert(array3->getValuesString().compare("-2 -1 0 1 2 8 ") == 0);
return 0;
}
......@@ -5,9 +5,9 @@ if __name__ == "__main__":
values1 = [0, 1, 2, 3]
array1 = XdmfArray.New()
array1.copyValues(IntVector(values1))
print array1.getValues()
print array1.getValuesString()
print array1.getType()
array1.copyValues(IntVector(values1[0:2]))
print array1.getValues()
print array1.getValuesString()
print array1.getType()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment