Scarab  v2.9.0
Project 8 C++ Utility Library
param_array_pybind.hh
Go to the documentation of this file.
1 /*
2  * KTPyNymph.cc
3  *
4  * Created on: Feb 1, 2018
5  * Author: N. Oblath, L. Gladstone, B.H. LaRoque
6  */
7 
8 #ifndef PARAM_ARRAY_PYBIND_HH_
9 #define PARAM_ARRAY_PYBIND_HH_
10 
11 #include "param.hh"
12 
13 #include "pybind11/pybind11.h"
14 
15 namespace scarab_pybind
16 {
17 
18  std::list< std::string > export_param_array( pybind11::module& mod )
19  {
20  std::list< std::string > all_members;
21 
22  // param_array
23  all_members.push_back( "ParamArray" );
24  pybind11::class_< scarab::param_array, scarab::param >( mod, "ParamArray", "param data structure object for holding an ordered sequence of objects" )
25  .def( pybind11::init< >() )
26  .def( "__str__", &scarab::param_array::to_string )
27  .def( "__len__", &scarab::param_array::size,
28  "Returns the size of the array" )
29  .def( "__getitem__", (scarab::param& (scarab::param_array::*)(unsigned)) &scarab::param_array::operator[],
31  .def( "__setitem__", [](scarab::param_array& an_obj, unsigned a_index, scarab::param& a_value){ an_obj.assign( a_index, a_value ); } )
32  .def( "__iter__", [](const scarab::param_array& an_obj){ return pybind11::make_iterator(an_obj.begin(), an_obj.end()); },
33  pybind11::keep_alive<0, 1>() /* keep object alive while the iterator exists */)
34 
35  //TODO: has_subset()
36 
37  .def( "empty", &scarab::param_array::empty,
38  "True if the length is zero" )
39 
40  .def( "resize", &scarab::param_array::resize, pybind11::arg( "size" ),
41  "Sets the size of the array; if smaller than the current size, the extra elements are deleted" )
42 
43  .def( "assign",
44  (void (scarab::param_array::*)(unsigned, const scarab::param&)) &scarab::param_array::assign,
45  pybind11::arg( "index" ),
46  pybind11::arg( "value" ),
47  "Add a param object [value] to the specified [index] in a array" )
48 
49  // Get value of the parameter, bringing along the default value
50  .def( "get_value",
51  (bool (scarab::param_array::*)(unsigned, bool) const) &scarab::param_array::get_value<bool>,
52  pybind11::arg( "index" ),
53  pybind11::arg( "default" ),
54  "Get boolean parameter array value at index, or return provided value if out of index range" )
55  .def( "get_value",
56  (unsigned (scarab::param_array::*)(unsigned, unsigned) const) &scarab::param_array::get_value<uint>,
57  pybind11::arg( "index" ),
58  pybind11::arg( "default" ),
59  "Get integer parameter array value at index, or return provided value if out of index range" )
60  .def( "get_value",
61  (int (scarab::param_array::*)(unsigned, int) const) &scarab::param_array::get_value<int>,
62  pybind11::arg( "index" ),
63  pybind11::arg( "default" ),
64  "Get signed integer parameter array value at index, or return provided value if out of index range" )
65  .def( "get_value",
66  (double (scarab::param_array::*)(unsigned, double) const) &scarab::param_array::get_value<double>,
67  pybind11::arg( "index" ),
68  pybind11::arg( "default" ),
69  "Get float parameter array value at index, or return provided value if out of index range" )
70  .def( "get_value",
71  (std::string (scarab::param_array::*)(unsigned, const std::string& ) const) &scarab::param_array::get_value,
72  pybind11::arg( "index" ),
73  pybind11::arg( "default" ),
74  "Get string parameter array value at index, or return provided value if out of index range" )
75 
76  .def( "push_back",
78  pybind11::arg( "value" ),
79  "add a param object to the end of the array")
80  .def( "push_front",
82  pybind11::arg( "value" ),
83  "add a param object to the end of the array")
84 
85  .def( "append",
87  pybind11::arg( "array" ),
88  "adds all elements of a param_array to the end of this" )
89  .def( "merge",
91  pybind11::arg( "array" ),
92  "merges provided param_array, effectively replacing the current array")
93  .def( "erase",
95  pybind11::arg( "index" ),
96  "eliminates the value at a position in the array" )
97  .def( "remove",
99  pybind11::arg( "index" ),
100  "returns the param_value at the provided index and removes it from the array" )
101  .def( "clear", &scarab::param_array::clear, "erase all contents and resize to 0" )
102 
103  ;
104  return all_members;
105  }
106 
107 } /* namespace scarab_pybind */
108 #endif /* PARAM_ARRAY_PYBIND_HH_ */
void push_front(const param &a_value)
Definition: param_array.hh:260
void merge(const param_array &an_array)
Definition: param_array.cc:105
Wrapper for Python extension modules.
Definition: pybind11.h:789
virtual std::string to_string() const
Definition: param_array.cc:149
std::list< std::string > export_param_array(pybind11::module &mod)
void append(const param_array &an_array)
Definition: param_array.hh:282
param_ptr_t remove(unsigned a_index)
Definition: param_array.hh:296
Keep patient alive while nurse lives.
Definition: attr.h:45
bool empty() const
Definition: param_array.hh:166
std::string get_value(unsigned a_index, const std::string &a_default) const
Definition: param_array.hh:171
void erase(unsigned a_index)
Definition: param_array.hh:291
void assign(unsigned a_index, const param &a_value)
Definition: param_array.hh:209
void push_back(const param &a_value)
Definition: param_array.hh:238
iterator make_iterator(Iterator first, Sentinel last, Extra &&... extra)
Makes a python iterator from a first and past-the-end C++ InputIterator.
Definition: pybind11.h:1658
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1110
unsigned size() const
Definition: param_array.hh:162
void resize(unsigned a_size)
Definition: param_array.cc:79