py_option.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_BINDINGS_PYTHON_PY_OPTION_HPP
13 #define MLPACK_BINDINGS_PYTHON_PY_OPTION_HPP
14 
16 #include "default_param.hpp"
17 #include "get_param.hpp"
18 #include "get_printable_param.hpp"
19 #include "print_class_defn.hpp"
20 #include "print_defn.hpp"
21 #include "print_doc.hpp"
24 #include "import_decl.hpp"
25 #include "is_serializable.hpp"
26 
27 namespace mlpack {
28 namespace bindings {
29 namespace python {
30 
34 template<typename T>
35 class PyOption
36 {
37  public:
42  PyOption(const T defaultValue,
43  const std::string& identifier,
44  const std::string& description,
45  const std::string& alias,
46  const std::string& cppName,
47  const bool required = false,
48  const bool input = true,
49  const bool noTranspose = false,
50  const std::string& bindingName = "")
51  {
52  // Create the ParamData object to give to IO.
53  util::ParamData data;
54 
55  data.desc = description;
56  data.name = identifier;
57  data.tname = TYPENAME(T);
58  data.alias = alias[0];
59  data.wasPassed = false;
60  data.noTranspose = noTranspose;
61  data.required = required;
62  data.input = input;
63  data.loaded = false;
64  data.cppType = cppName;
65 
66  // Every parameter we'll get from Python will have the correct type.
67  data.value = boost::any(defaultValue);
68 
69  // Set the function pointers that we'll need. All of these function
70  // pointers will be used by both the program that generates the pyx, and
71  // also the binding itself. (The binding itself will only use GetParam,
72  // GetPrintableParam, and GetRawParam.)
73  IO::AddFunction(data.tname, "GetParam", &GetParam<T>);
74  IO::AddFunction(data.tname, "GetPrintableParam", &GetPrintableParam<T>);
75  IO::AddFunction(data.tname, "DefaultParam", &DefaultParam<T>);
76 
77  // These are used by the pyx generator.
78  IO::AddFunction(data.tname, "PrintClassDefn", &PrintClassDefn<T>);
79  IO::AddFunction(data.tname, "PrintDefn", &PrintDefn<T>);
80  IO::AddFunction(data.tname, "PrintDoc", &PrintDoc<T>);
81  IO::AddFunction(data.tname, "PrintOutputProcessing",
82  &PrintOutputProcessing<T>);
83  IO::AddFunction(data.tname, "PrintInputProcessing",
84  &PrintInputProcessing<T>);
85  IO::AddFunction(data.tname, "ImportDecl", &ImportDecl<T>);
86  IO::AddFunction(data.tname, "IsSerializable", &IsSerializable<T>);
87 
88  // Add the ParamData object to the IO class for the correct binding name.
89  IO::AddParameter(bindingName, std::move(data));
90  }
91 };
92 
93 } // namespace python
94 } // namespace bindings
95 } // namespace mlpack
96 
97 #endif
boost::any value
The actual value that is held.
Definition: param_data.hpp:79
Linear algebra utility functions, generally performed on matrices or vectors.
bool wasPassed
True if the option was passed to the program.
Definition: param_data.hpp:66
PyOption(const T defaultValue, const std::string &identifier, const std::string &description, const std::string &alias, const std::string &cppName, const bool required=false, const bool input=true, const bool noTranspose=false, const std::string &bindingName="")
Construct a PyOption object.
Definition: py_option.hpp:42
std::string desc
Description of this parameter, if any.
Definition: param_data.hpp:58
bool input
True if this option is an input option (otherwise, it is output).
Definition: param_data.hpp:73
The Python option class.
Definition: py_option.hpp:35
python
Definition: CMakeLists.txt:7
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
bool loaded
If this is an input parameter that needs extra loading, this indicates whether or not it has been loa...
Definition: param_data.hpp:76
#define TYPENAME(x)
The TYPENAME macro is used internally to convert a type into a string.
Definition: param_data.hpp:22
char alias
Alias for this parameter.
Definition: param_data.hpp:63
std::string tname
Type information of this parameter.
Definition: param_data.hpp:61
std::string name
Name of this parameter.
Definition: param_data.hpp:56
static void AddFunction(const std::string &type, const std::string &name, void(*func)(util::ParamData &, const void *, void *))
Add a function to the function map.
bool required
True if this option is required.
Definition: param_data.hpp:71
static void AddParameter(const std::string &bindingName, util::ParamData &&d)
Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e. ...
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:81
bool noTranspose
True if this is a matrix that should not be transposed.
Definition: param_data.hpp:69