ESYS13  Revision_
WrappedArray.h
Go to the documentation of this file.
00001 
00002 /*******************************************************
00003 *
00004 * Copyright (c) 2003-2012 by University of Queensland
00005 * Earth Systems Science Computational Center (ESSCC)
00006 * http://www.uq.edu.au/esscc
00007 *
00008 * Primary Business: Queensland, Australia
00009 * Licensed under the Open Software License version 3.0
00010 * http://www.opensource.org/licenses/osl-3.0.php
00011 *
00012 *******************************************************/
00013 
00014 
00017 #ifndef WrappedArray_20081202_H
00018 #define WrappedArray_20081202_H
00019 #include "system_dep.h"
00020 #include "DataTypes.h"
00021 #include "boost/python/extract.hpp"
00022 
00023 namespace escript
00024 {
00025 
00026 class WrappedArray
00027 {
00028 public:
00029     WrappedArray(const boost::python::object& obj_in);
00030     ~WrappedArray();
00031     unsigned int getRank() const;
00032     const DataTypes::ShapeType& getShape() const;
00033     double getElt() const;
00034     double getElt(unsigned int i) const;
00035     double getElt(unsigned int i, unsigned int j) const;
00036     double getElt(unsigned int i, unsigned int j, unsigned int k) const;
00037     double getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const;
00038     void convertArray() const;
00039 private:
00040     template<typename T> void convertNumpyArray(const T* array, const std::vector<int>& strides) const;
00041     const boost::python::object& obj;
00042     int rank;
00043     escript::DataTypes::ShapeType shape;
00044     double m_scalar;
00045     mutable double* dat;
00046 };
00047 
00048 inline unsigned int 
00049 WrappedArray::getRank() const
00050 {
00051     return rank;
00052 }
00053 
00054 inline const DataTypes::ShapeType& 
00055 WrappedArray::getShape() const
00056 {
00057     return shape;
00058 }
00059 
00060 inline double
00061 WrappedArray::getElt() const
00062 {
00063     return m_scalar;
00064 }
00065 
00066 
00067 inline double
00068 WrappedArray::getElt(unsigned int i) const
00069 {  // __float__ added to deal with numpy. If this causes problems we may have to register a custom converter
00070         return (dat!=0)?dat[i]:(boost::python::extract<double>(obj[i].attr("__float__")()));    
00071 }
00072 
00073 inline
00074 double 
00075 WrappedArray::getElt(unsigned int i, unsigned int j) const
00076 {
00077     return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j)]:(boost::python::extract<double>(obj[i][j].attr("__float__")()));
00078 }
00079 
00080 inline
00081 double 
00082 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k) const
00083 {
00084     return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k)]:(boost::python::extract<double>(obj[i][j][k].attr("__float__")()));
00085 }
00086 
00087 inline
00088 double 
00089 WrappedArray::getElt(unsigned int i, unsigned int j, unsigned int k, unsigned int m) const
00090 {
00091     return (dat!=0)?dat[DataTypes::getRelIndex(shape,i,j,k,m)]:(boost::python::extract<double>(obj[i][j][k][m].attr("__float__")()));
00092 }
00093 
00094 }
00095 
00096 #endif
00097