md_option.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_BINDINGS_MARKDOWN_MD_OPTION_HPP
13 #define MLPACK_BINDINGS_MARKDOWN_MD_OPTION_HPP
14 
16 #include <mlpack/core/util/cli.hpp>
17 #include "default_param.hpp"
18 #include "get_param.hpp"
19 #include "get_printable_param.hpp"
20 #include "get_printable_param_name.hpp" // For cli bindings.
21 #include "get_printable_param_value.hpp" // For cli bindings.
22 #include "get_printable_type.hpp"
23 #include "is_serializable.hpp"
24 
25 namespace mlpack {
26 namespace bindings {
27 namespace markdown {
28 
32 template<typename T>
33 class MDOption
34 {
35  public:
41  MDOption(const T defaultValue,
42  const std::string& identifier,
43  const std::string& description,
44  const std::string& alias,
45  const std::string& cppName,
46  const bool required = false,
47  const bool input = true,
48  const bool noTranspose = false,
49  const std::string& bindingName = "")
50  {
51  // Create the ParamData object to give to CLI.
52  util::ParamData data;
53 
54  data.desc = description;
55  data.name = identifier;
56  data.tname = TYPENAME(T);
57  data.alias = alias[0];
58  data.wasPassed = false;
59  data.noTranspose = noTranspose;
60  data.required = required;
61  data.input = input;
62  data.loaded = false;
63  // Several options from Python and CLI bindings are persistent.
64  if (identifier == "verbose" || identifier == "copy_all_inputs" ||
65  identifier == "help" || identifier == "info" || identifier == "version")
66  data.persistent = true;
67  else
68  data.persistent = false;
69  data.cppType = cppName;
70 
71  // Every parameter we'll get from Markdown will have the correct type.
72  data.value = boost::any(defaultValue);
73 
74  // Restore the parameters for this program.
75  if (identifier != "verbose" && identifier != "copy_all_inputs")
76  CLI::RestoreSettings(bindingName, false);
77 
78  // Set the function pointers that we'll need. Most of these simply delegate
79  // to the current binding type's implementation. Any new language will need
80  // to have all of these implemented, and the Markdown implementation will
81  // need to properly delegate.
82  CLI::GetSingleton().functionMap[data.tname]["DefaultParam"] =
83  &DefaultParam<T>;
84  CLI::GetSingleton().functionMap[data.tname]["GetParam"] = &GetParam<T>;
85  CLI::GetSingleton().functionMap[data.tname]["GetPrintableParam"] =
86  &GetPrintableParam<T>;
87  CLI::GetSingleton().functionMap[data.tname]["GetPrintableParamName"] =
88  &GetPrintableParamName<T>;
89  CLI::GetSingleton().functionMap[data.tname]["GetPrintableParamValue"] =
90  &GetPrintableParamValue<T>;
91  CLI::GetSingleton().functionMap[data.tname]["GetPrintableType"] =
92  &GetPrintableType<T>;
93  CLI::GetSingleton().functionMap[data.tname]["IsSerializable"] =
94  &IsSerializable<T>;
95 
96  // Add the option.
97  CLI::Add(std::move(data));
98  if (identifier != "verbose" && identifier != "copy_all_inputs" &&
99  identifier != "help" && identifier != "info" && identifier != "version")
100  CLI::StoreSettings(bindingName);
102  }
103 };
104 
105 } // namespace markdown
106 } // namespace bindings
107 } // namespace mlpack
108 
109 #endif
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_po.hpp:21
MDOption(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 an MDOption object.
Definition: md_option.hpp:41
bool wasPassed
True if the option was passed to the program.
Definition: param_data.hpp:66
The Markdown option class.
Definition: md_option.hpp:33
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
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. ...
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:302
bool noTranspose
True if this is a matrix that should not be transposed.
Definition: param_data.hpp:69