print_input_processing.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_BINDINGS_GO_PRINT_INPUT_PROCESSING_HPP
13 #define MLPACK_BINDINGS_GO_PRINT_INPUT_PROCESSING_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 #include "get_type.hpp"
17 #include "get_go_type.hpp"
18 #include "strip_type.hpp"
19 #include "camel_case.hpp"
20 
21 namespace mlpack {
22 namespace bindings {
23 namespace go {
24 
28 template<typename T>
30  const util::ParamData& d,
31  const size_t indent,
32  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
33  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0,
34  const typename boost::disable_if<std::is_same<T,
35  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
36 {
37  const std::string prefix(indent, ' ');
38 
39  std::string def = "nil";
40  if (std::is_same<T, bool>::value)
41  def = "false";
42 
43  // Capitalize the first letter of parameter name so it is
44  // of exported type in Go.
45  std::string paramName = d.name;
46  std::string goParamName = paramName;
47  if (!paramName.empty())
48  {
49  goParamName = CamelCase(goParamName);
50  }
51 
61  std::cout << prefix << "// Detect if the parameter was passed; set if so."
62  << std::endl;
63  if (!d.required)
64  {
65  std::cout << prefix << "if param." << goParamName << " != ";
66 
67  // Print out default value.
68  if (d.cppType == "std::string")
69  {
70  std::string value = boost::any_cast<std::string>(d.value);
71  std::cout << "\"" << value << "\"";
72  }
73  else if (d.cppType == "double")
74  {
75  double value = boost::any_cast<double>(d.value);
76  std::cout << value;
77  }
78  else if (d.cppType == "int")
79  {
80  int value = boost::any_cast<int>(d.value);
81  std::cout << value;
82  }
83  else if (d.cppType == "bool")
84  {
85  bool value = boost::any_cast<bool>(d.value);
86  if (value == 0)
87  std::cout << "false";
88  else
89  std::cout << "true";
90  }
91  else if (GetType<T>(d) == "VecString" || GetType<T>(d) == "VecInt")
92  {
93  std::cout << "nil";
94  }
95 
96  // Print function call to set the given parameter into the cli.
97  std::cout << " {" << std::endl;
98  std::cout << prefix << prefix << "setParam" << GetType<T>(d) << "(\""
99  << d.name << "\", param." << goParamName << ")" << std::endl;
100 
101  // Print function call to set the given parameter as passed.
102  std::cout << prefix << prefix << "setPassed(\""
103  << d.name << "\")" << std::endl;
104 
105  // If this parameter is "verbose", then enable verbose output.
106  if (d.name == "verbose")
107  std::cout << prefix << prefix << "enableVerbose()" << std::endl;
108 
109  std::cout << prefix << "}" << std::endl; // Closing brace.
110  }
111  else
112  {
113  // Print function call to set the given parameter into the cli.
114  std::cout << prefix << "setParam" << GetType<T>(d) << "(\""
115  << d.name << "\", " << goParamName << ")"
116  << std::endl;
117 
118  // Print function call to set the given parameter as passed.
119  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
120  }
121  std::cout << std::endl; // Extra line is to clear up the code a bit.
122 }
123 
127 template<typename T>
129  const util::ParamData& d,
130  const size_t indent,
131  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
132 {
133  const std::string prefix(indent, ' ');
134 
135  // Capitalize the first letter of parameter name so it is
136  // of exported type in Go.
137  std::string paramName = d.name;
138  std::string goParamName = paramName;
139  if (!paramName.empty())
140  {
141  goParamName = CamelCase(goParamName);
142  }
143 
153  std::cout << prefix << "// Detect if the parameter was passed; set if so."
154  << std::endl;
155  if (!d.required)
156  {
157  std::cout << prefix << "if param." << goParamName
158  << " != nil {" << std::endl;
159 
160  // Print function call to set the given parameter into the cli.
161  std::cout << prefix << prefix << "gonumToArma" << GetType<T>(d)
162  << "(\"" << d.name << "\", param." << goParamName
163  << ")" << std::endl;
164 
165  // Print function call to set the given parameter as passed.
166  std::cout << prefix << prefix << "setPassed(\"" << d.name << "\")"
167  << std::endl;
168  std::cout << prefix << "}" << std::endl; // Closing brace.
169  }
170  else
171  {
172  // Print function call to set the given parameter into the cli.
173  std::cout << prefix << "gonumToArma" << GetType<T>(d)
174  << "(\"" << d.name << "\", " << goParamName
175  << ")" << std::endl;
176 
177  // Print function call to set the given parameter as passed.
178  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
179  }
180  std::cout << std::endl; // Extra line is to clear up the code a bit.
181 }
182 
186 template<typename T>
188  const util::ParamData& d,
189  const size_t indent,
190  const typename boost::enable_if<std::is_same<T,
191  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
192 {
193  const std::string prefix(indent, ' ');
194 
195  // Capitalize the first letter of parameter name so it is
196  // of exported type in Go.
197  std::string paramName = d.name;
198  std::string goParamName = paramName;
199  if (!paramName.empty())
200  {
201  goParamName = CamelCase(goParamName);
202  }
203 
213  std::cout << prefix << "// Detect if the parameter was passed; set if so."
214  << std::endl;
215  if (!d.required)
216  {
217  std::cout << prefix << "if param." << goParamName
218  << " != nil {" << std::endl;
219 
220  // Print function call to set the given parameter into the cli.
221  std::cout << prefix << prefix << "gonumToArmaMatWithInfo"
222  << "(\"" << d.name << "\", param." << goParamName
223  << ")" << std::endl;
224 
225  // Print function call to set the given parameter as passed.
226  std::cout << prefix << prefix << "setPassed(\"" << d.name << "\")"
227  << std::endl;
228  std::cout << prefix << "}" << std::endl; // Closing brace.
229  }
230  else
231  {
232  // Print function call to set the given parameter into the cli.
233  std::cout << prefix << "gonumToArmaMatWithInfo"
234  << "(\"" << d.name << "\", " << goParamName
235  << ")" << std::endl;
236 
237  // Print function call to set the given parameter as passed.
238  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
239  }
240  std::cout << std::endl; // Extra line is to clear up the code a bit.
241 }
242 
246 template<typename T>
248  const util::ParamData& d,
249  const size_t indent,
250  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
251  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
252 {
253  // First, get the correct classparamName if needed.
254  std::string goStrippedType, strippedType, printedType, defaultsType;
255  StripType(d.cppType, goStrippedType, strippedType, printedType, defaultsType);
256 
257  const std::string prefix(indent, ' ');
258 
259  // Capitalize the first letter of parameter name so it is
260  // of exported type in Go.
261  std::string paramName = d.name;
262  std::string goParamName = paramName;
263  if (!paramName.empty())
264  {
265  goParamName = CamelCase(goParamName);
266  }
267 
277  std::cout << prefix << "// Detect if the parameter was passed; set if so."
278  << std::endl;
279  if (!d.required)
280  {
281  std::cout << prefix << "if param." << goParamName << " != nil {"
282  << std::endl;
283  // Print function call to set the given parameter into the cli.
284  std::cout << prefix << prefix << "set" << strippedType << "(\""
285  << d.name << "\", param." << goParamName << ")" << std::endl;
286 
287  // Print function call to set the given parameter as passed.
288  std::cout << prefix << prefix << "setPassed(\"" << d.name << "\")"
289  << std::endl;
290  std::cout << prefix << "}" << std::endl; // Closing brace.
291  }
292  else
293  {
294  // Print function call to set the given parameter into the cli.
295  std::cout << prefix << "set" << strippedType << "(\"" << goParamName
296  << "\", " << paramName << ")" << std::endl;
297 
298  // Print function call to set the given parameter as passed.
299  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
300  }
301  std::cout << std::endl; // Extra line is to clear up the code a bit.
302 }
303 
315 template<typename T>
317  const void* input,
318  void* /* output */)
319 {
320  PrintInputProcessing<typename std::remove_pointer<T>::type>(d,
321  *((size_t*) input));
322 }
323 
324 } // namespace go
325 } // namespace bindings
326 } // namespace mlpack
327 
328 #endif
std::string CamelCase(std::string s)
Given an snake_case like, e.g., "logistic_regression", return CamelCase(e.g.
Definition: camel_case.hpp:24
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
void StripType(const std::string &inputType, std::string &goStrippedType, std::string &strippedType, std::string &printedType, std::string &defaultsType)
Given an input type like, e.g., "LogisticRegression<>", return four types that can be used in Go code...
Definition: strip_type.hpp:30
strip_type.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
void PrintInputProcessing(const util::ParamData &d, const size_t indent, const typename boost::disable_if< arma::is_arma_type< T >>::type *=0, const typename boost::disable_if< data::HasSerialize< T >>::type *=0, const typename boost::disable_if< std::is_same< T, std::tuple< data::DatasetInfo, arma::mat >>>::type *=0)
Print input processing for a standard option type.
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
if(BUILD_GO_BINDINGS) find_package(Go 1.11.0) if(NOT GO_FOUND) macro(add_go_binding name) endmacro() return() endif() find_package(Gonum) if(NOT GONUM_FOUND) macro(add_go_binding name) endmacro() endif() set(BINDING_SOURCES get_type.hpp get_param.hpp get_printable_param.hpp go_option.hpp mlpack/arma_util.h mlpack/arma_util.hpp mlpack/cli_util.h mlpack/cli_util.hpp print_class_defn.hpp print_cpp.cpp print_cpp.hpp print_defn_input.hpp print_defn_output.hpp print_doc.hpp print_doc_functions.hpp print_doc_functions_impl.hpp print_go.hpp print_go.cpp print_h.hpp print_h.cpp print_import_decl.hpp print_input_processing.hpp print_method_config.hpp print_method_init.hpp print_output_processing.hpp camel_case.hpp strip_type.hpp) set(CGO_SOURCES mlpack/arma_util.go mlpack/cli_util.go mlpack/doc.go) set(CAPI_SOURCES mlpack/capi/arma_util.cpp mlpack/capi/arma_util.h mlpack/capi/arma_util.hpp mlpack/capi/cli_util.cpp mlpack/capi/cli_util.h mlpack/capi/cli_util.hpp) set(UTIL_SOURCES mlpack/capi/arma_util.cpp mlpack/capi/cli_util.cpp) set(TEST_SOURCES tests/go_binding_test.go) add_custom_target(go ALL DEPENDS mlpack) add_custom_target(go_copy ALL DEPENDS mlpack) if(BUILD_TESTS) foreach(test_file $
Definition: CMakeLists.txt:1
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
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84