Scarab  v2.11.1
Project 8 C++ Utility Library
param_node_pybind.hh
Go to the documentation of this file.
1 /*
2  * Created on: Feb 1, 2018
3  * Author: N. Oblath, L. Gladstone, B.H. LaRoque
4  */
5 
6 #ifndef PARAM_NODE_PYBIND_HH_
7 #define PARAM_NODE_PYBIND_HH_
8 
9 #include "param.hh"
10 
11 #include "pybind11/pybind11.h"
12 
13 namespace scarab_pybind
14 {
15 
16  std::list< std::string > export_param_node( pybind11::module& mod )
17  {
18  std::list< std::string > all_members;
19 
20  // param_node
21  all_members.push_back( "ParamNode" );
22  pybind11::class_< scarab::param_node, scarab::param >( mod, "ParamNode", "param data structure object for holding a collection of object indexable by string keys" )
23  .def( pybind11::init< >() )
24 
25  .def( "__str__", &scarab::param_node::to_string )
26  .def( "__len__", &scarab::param_node::size )
27  .def( "__getitem__", (scarab::param& (scarab::param_node::*)(const std::string&)) &scarab::param_node::operator[],
29  .def( "__setitem__", [](scarab::param_node& an_obj, std::string& a_name, scarab::param& a_value){ an_obj.replace( a_name, a_value ); } )
30  .def( "__contains__", &scarab::param_node::has )
31  .def( "__iter__", [](const scarab::param_node& an_obj){ return pybind11::make_iterator(an_obj.begin(), an_obj.end()); },
32  pybind11::keep_alive<0, 1>() /* keep object alive while the iterator exists */ )
33 
34  .def( "count",
36  pybind11::arg( "key" ),
37  "returns the number of occurances of provided key" )
38 
39  //TODO: has_subset()
40 
41  .def( "empty", &scarab::param_node::empty, "returns whether the map container is empty")
42 
43  .def( "add",
44  (bool (scarab::param_node::*)(const std::string&, const scarab::param&)) &scarab::param_node::add,
45  pybind11::arg( "key" ),
46  pybind11::arg( "value" ),
47  "Add a param object to a node at a particular key")
48 
49  // Get value of the parameter, bringing along the default value
50  .def( "get_value",
51  (bool (scarab::param_node::*)(const std::string&, bool) const) &scarab::param_node::get_value<bool>,
52  pybind11::arg( "key" ),
53  pybind11::arg( "default" ),
54  "Get parameter node value at [key] or return [default] (as a bool)" )
55  .def( "get_value",
56  (unsigned (scarab::param_node::*)(const std::string&, unsigned) const) &scarab::param_node::get_value<uint>,
57  pybind11::arg( "key" ),
58  pybind11::arg( "default" ),
59  "Get parameter node value at [key] or return [default] (as an unsigned integer)" )
60  .def( "get_value",
61  (int (scarab::param_node::*)(const std::string&, int) const) &scarab::param_node::get_value<int>,
62  pybind11::arg( "key" ),
63  pybind11::arg( "default" ),
64  "Get parameter node value at [key] or return [default] (as a signed integer)" )
65  .def( "get_value",
66  (double (scarab::param_node::*)(const std::string&, double) const) &scarab::param_node::get_value<double>,
67  pybind11::arg( "key" ),
68  pybind11::arg( "default" ),
69  "Get parameter node value at [key] or return [default] (as a float)" )
70  .def( "get_value",
71  (std::string (scarab::param_node::*)(const std::string&, const std::string& ) const) &scarab::param_node::get_value,
72  pybind11::arg( "key" ),
73  pybind11::arg( "default" ),
74  "Get parameter node value at [key] or return [default] (as a string)" )
75 
76  //TODO: merge(), erase(), remove(), clear()
77 
78 
79  ;
80  return all_members;
81  }
82 } /* namespace scarab_pybind */
83 #endif /* PARAM_NODE_PYBIND_HH_ */
unsigned size() const
Definition: param_node.hh:189
void replace(const std::string &a_name, const param &a_value)
Creates a copy of a_value; overwrites if the key exits.
Definition: param_node.hh:274
iterator begin()
Definition: param_node.hh:328
Wrapper for Python extension modules.
Definition: pybind11.h:789
bool empty() const
Definition: param_node.hh:193
std::string get_value(const std::string &a_name, const std::string &a_default) const
Definition: param_node.hh:208
unsigned count(const std::string &a_name) const
Definition: param_node.hh:203
Keep patient alive while nurse lives.
Definition: attr.h:45
std::list< std::string > export_param_node(pybind11::module &mod)
bool add(const std::string &a_name, const param &a_value)
Definition: param_node.hh:228
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 has(const std::string &a_name) const
Definition: param_node.hh:198
bool typename Extra class_ & def(const char *name_, Func &&f, const Extra &... extra)
Definition: pybind11.h:1110
virtual std::string to_string() const
Definition: param_node.cc:127