mlpack  3.0.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 "get_param.hpp"
17 #include "get_printable_param.hpp"
18 #include "print_class_defn.hpp"
19 #include "print_defn.hpp"
20 #include "print_doc.hpp"
23 #include "import_decl.hpp"
24 
25 namespace mlpack {
26 namespace bindings {
27 namespace python {
28 
29 // Defined in mlpack_main.hpp.
30 extern std::string programName;
31 
35 template<typename T>
36 class PyOption
37 {
38  public:
44  PyOption(const T defaultValue,
45  const std::string& identifier,
46  const std::string& description,
47  const std::string& alias,
48  const std::string& cppName,
49  const bool required = false,
50  const bool input = true,
51  const bool noTranspose = false,
52  const std::string& /*testName*/ = "")
53  {
54  // Create the ParamData object to give to CLI.
55  util::ParamData data;
56 
57  data.desc = description;
58  data.name = identifier;
59  data.tname = TYPENAME(T);
60  data.alias = alias[0];
61  data.wasPassed = false;
62  data.noTranspose = noTranspose;
63  data.required = required;
64  data.input = input;
65  data.loaded = false;
66  // Only "verbose" and "copy_all_inputs" will be persistent.
67  if (identifier == "verbose" || identifier == "copy_all_inputs")
68  data.persistent = true;
69  else
70  data.persistent = false;
71  data.cppType = cppName;
72 
73  // Every parameter we'll get from Python will have the correct type.
74  data.value = boost::any(defaultValue);
75 
76  // Restore the parameters for this program.
77  if (identifier != "verbose" && identifier != "copy_all_inputs")
79 
80  // Set the function pointers that we'll need. All of these function
81  // pointers will be used by both the program that generates the pyx, and
82  // also the binding itself. (The binding itself will only use GetParam,
83  // GetPrintableParam, and GetRawParam.)
84  CLI::GetSingleton().functionMap[data.tname]["GetParam"] = &GetParam<T>;
85  CLI::GetSingleton().functionMap[data.tname]["GetPrintableParam"] =
86  &GetPrintableParam<T>;
87 
88  // These are used by the pyx generator.
89  CLI::GetSingleton().functionMap[data.tname]["PrintClassDefn"] =
90  &PrintClassDefn<T>;
91  CLI::GetSingleton().functionMap[data.tname]["PrintDefn"] = &PrintDefn<T>;
92  CLI::GetSingleton().functionMap[data.tname]["PrintDoc"] = &PrintDoc<T>;
93  CLI::GetSingleton().functionMap[data.tname]["PrintOutputProcessing"] =
94  &PrintOutputProcessing<T>;
95  CLI::GetSingleton().functionMap[data.tname]["PrintInputProcessing"] =
96  &PrintInputProcessing<T>;
97  CLI::GetSingleton().functionMap[data.tname]["ImportDecl"] = &ImportDecl<T>;
98 
99  // Add the ParamData object, then store. This is necessary because we may
100  // import more than one .so that uses CLI, so we have to keep the options
101  // separate. programName is a global variable from mlpack_main.hpp.
102  CLI::Add(std::move(data));
103  if (identifier != "verbose" && identifier != "copy_all_inputs")
106  }
107 };
108 
109 } // namespace python
110 } // namespace bindings
111 } // namespace mlpack
112 
113 #endif
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
bool wasPassed
True if the option was passed to the program.
Definition: param_data.hpp:66
static CLI & GetSingleton()
Retrieve the singleton.
bool persistent
If this should be preserved across different settings (i.e.
Definition: param_data.hpp:79
static void StoreSettings(const std::string &name)
Take all parameters and function mappings and store them, under the given name.
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:36
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
static void ClearSettings()
Clear all of the settings, removing all parameters and function mappings.
std::string name
Name of this parameter.
Definition: param_data.hpp:56
bool required
True if this option is required.
Definition: param_data.hpp:71
static void Add(util::ParamData &&d)
Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e. ...
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 &="")
Construct a PyOption object.
Definition: py_option.hpp:44
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84
static void RestoreSettings(const std::string &name, const bool fatal=true)
Restore all of the parameters and function mappings of the given name, if they exist.
FunctionMapType functionMap
Definition: cli.hpp:290
bool noTranspose
True if this is a matrix that should not be transposed.
Definition: param_data.hpp:69