xc
xc_python_utils.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // XC program; finite element analysis code
4 // for structural analysis and design.
5 //
6 // Copyright (C) Luis C. PĂ©rez Tato
7 //
8 // XC is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // This software is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program.
21 // If not, see <http://www.gnu.org/licenses/>.
22 //----------------------------------------------------------------------------
23 //xc_python_utils.h
24 
25 #ifndef XC_PYTHON_UTILS_H
26 #define XC_PYTHON_UTILS_H
27 
28 #include <boost/python/list.hpp>
29 #include <vector>
30 #include "utility/matrices/m_double.h"
31 #include <boost/python/suite/indexing/map_indexing_suite.hpp>
32 
33 namespace XC {
34  class ID;
35 
36 boost::python::list xc_id_to_py_list(const XC::ID &);
37 
38 std::vector<double> vector_double_from_py_object(const boost::python::object &);
39 std::vector<int> vector_int_from_py_object(const boost::python::object &);
40 m_double m_double_from_py_object(const boost::python::object &);
41 
42 
43 // Solution to export std::map<key, T *> to Python as iterable
44 // objects.
45 
46 // CREDITS: Taken from a post of Phil in stackoverflow
47 // https://stackoverflow.com/users/2904770/phil
48 
49 //Forward declaration
50 template <class Container, bool NoProxy, class DerivedPolicies>
52 
53 template <class Container, bool NoProxy>
55  : public mapptr_indexing_suite<Container,
56  NoProxy, final_mapptr_derived_policies<Container, NoProxy> > {};
57 
61 template <
62  class Container,
63  bool NoProxy = false,
64  class DerivedPolicies
67  : public boost::python::map_indexing_suite<
68  Container,
69  NoProxy,
70  DerivedPolicies
71  >
72  {
73  public:
74  // Must be explicit if the compiler is
75  // going to take from the base class
76  using typename boost::python::map_indexing_suite<
77  Container,NoProxy,DerivedPolicies>::data_type;
78  using typename boost::python::map_indexing_suite<
79  Container,NoProxy,DerivedPolicies>::value_type;
80 
81  // Only one class needs to be overridden from the base
82  template <class Class>
83  static void
84  extension_def(Class& cl)
85  {
86  // Wrap the map's element (value_type)
87  std::string elem_name = "mapptr_indexing_suite_";
88  boost::python::object class_name(cl.attr("__name__"));
89  boost::python::extract<std::string> class_name_extractor(class_name);
90  elem_name += class_name_extractor();
91  elem_name += "_entry";
92 
93  // use of is_pointer here is the only
94  // difference to the base map_indexing_suite
95  typedef typename boost::mpl::if_<
96  boost::mpl::and_<std::is_pointer<data_type>, boost::mpl::bool_<!NoProxy> >
97  , boost::python::return_internal_reference<>
98  , boost::python::default_call_policies
99  >::type get_data_return_policy;
100 
101  boost::python::class_<value_type>(elem_name.c_str())
102  .def("__repr__", &DerivedPolicies::print_elem)
103  .def("data", &DerivedPolicies::get_data, get_data_return_policy())
104  .def("key", &DerivedPolicies::get_key)
105  ;
106  }
107  };
108 } // end of XC namespace
109 #endif
Matrix which element type has estructura de anillo respecto a las operaciones + y *...
Definition: ZMatrix.h:37
Solution to export std::map<key, T *> to Python as iterable objects.
Definition: xc_python_utils.h:51
Definition: xc_python_utils.h:54
Vector of integers.
Definition: ID.h:95
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35