ESyS-Particle  2.3
ListConverter.h
Go to the documentation of this file.
1 // //
3 // Copyright (c) 2003-2014 by The University of Queensland //
4 // Centre for Geoscience Computing //
5 // http://earth.uq.edu.au/centre-geoscience-computing //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.opensource.org/licenses/osl-3.0.php //
10 // //
12 
13 
14 #ifndef ESYS_LSM_BPULISTCONVERTER_H
15 #define ESYS_LSM_BPULISTCONVERTER_H
16 
17 #include <boost/python.hpp>
18 
20 
21 #include <vector>
22 
23 namespace esys
24 {
25  namespace lsm
26  {
27  namespace bpu
28  {
29  template <typename TmplValueType>
31  {
32  public:
33  typedef TmplValueType value_type;
34 
35  value_type operator()(const boost::python::object &pyObject) const
36  {
37  return boost::python::extract<value_type>(pyObject);
38  }
39  };
40 
41  template<typename TmplValue>
42  std::vector<TmplValue> listToVector(const boost::python::list &pythonList)
43  {
45  std::vector<TmplValue> vec;
46  const int numElements = esys::lsm::bpu::len(pythonList);
47  vec.reserve(numElements);
48  for (int i = 0; i < numElements; i++)
49  {
50  vec.push_back(extractor(pythonList[i]));
51  }
52  return vec;
53  }
54 
55  template<typename TmplValue>
56  std::vector<TmplValue> tupleToVector(const boost::python::tuple &pythonTulple)
57  {
59  std::vector<TmplValue> vec;
60  const int numElements = esys::lsm::bpu::len(pythonTulple);
61  vec.reserve(numElements);
62  for (int i = 0; i < numElements; i++)
63  {
64  vec.push_back(extractor(pythonTulple[i]));
65  }
66  return vec;
67  }
68 
69  template<typename TmplValue, typename TmplExtractor>
70  std::vector<TmplValue> listToVector(const boost::python::list &pythonList, TmplExtractor extractor=TmplExtractor())
71  {
72  std::vector<TmplValue> vec;
73  const int numElements = esys::lsm::bpu::len(pythonList);
74  vec.reserve(numElements);
75  for (int i = 0; i < numElements; i++)
76  {
77  vec.push_back(extractor(pythonList[i]));
78  }
79  return vec;
80  }
81 
82  template <typename TmplVector>
83  boost::python::list vectorToList(const TmplVector &vec)
84  {
85  boost::python::list pythonList;
86  for (
87  typename TmplVector::const_iterator it = vec.begin();
88  it != vec.end();
89  it++
90  )
91  {
92  pythonList.append(*it);
93  }
94  return pythonList;
95  }
96  }
97  }
98 }
99 
100 #endif
Definition: ListConverter.h:30
std::vector< TmplValue > listToVector(const boost::python::list &pythonList)
Definition: ListConverter.h:42
Definition: CheckPointable.cpp:16
boost::python::list vectorToList(const TmplVector &vec)
Definition: ListConverter.h:83
value_type operator()(const boost::python::object &pyObject) const
Definition: ListConverter.h:35
std::vector< TmplValue > tupleToVector(const boost::python::tuple &pythonTulple)
Definition: ListConverter.h:56
int len(const boost::python::object &pyOb)
Definition: Util.h:30
TmplValueType value_type
Definition: ListConverter.h:33