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"
20 
21 namespace mlpack {
22 namespace bindings {
23 namespace go {
24 
28 template<typename T>
30  util::ParamData& d,
31  const size_t indent,
32  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
33  const typename std::enable_if<!data::HasSerialize<T>::value>::type* = 0,
34  const typename std::enable_if<!std::is_same<T,
35  std::tuple<data::DatasetInfo, arma::mat>>::value>::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 = util::CamelCase(goParamName, false);
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 = MLPACK_ANY_CAST<std::string>(d.value);
71  std::cout << "\"" << value << "\"";
72  }
73  else if (d.cppType == "double")
74  {
75  double value = MLPACK_ANY_CAST<double>(d.value);
76  std::cout << value;
77  }
78  else if (d.cppType == "int")
79  {
80  int value = MLPACK_ANY_CAST<int>(d.value);
81  std::cout << value;
82  }
83  else if (d.cppType == "bool")
84  {
85  bool value = MLPACK_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 io.
97  std::cout << " {" << std::endl;
98  std::cout << prefix << prefix << "setParam" << GetType<T>(d) << "(params, "
99  << "\"" << d.name << "\", param." << goParamName << ")"
100  << std::endl;
101 
102  // Print function call to set the given parameter as passed.
103  std::cout << prefix << prefix << "setPassed(params, \""
104  << d.name << "\")" << std::endl;
105 
106  // If this parameter is "verbose", then enable verbose output.
107  if (d.name == "verbose")
108  std::cout << prefix << prefix << "enableVerbose()" << std::endl;
109 
110  std::cout << prefix << "}" << std::endl; // Closing brace.
111  }
112  else
113  {
114  goParamName = util::CamelCase(goParamName, true);
115  // Print function call to set the given parameter into the io.
116  std::cout << prefix << "setParam" << GetType<T>(d) << "(params, \""
117  << d.name << "\", " << goParamName << ")"
118  << std::endl;
119 
120  // Print function call to set the given parameter as passed.
121  std::cout << prefix << "setPassed(params, \"" << d.name << "\")"
122  << std::endl;
123  }
124  std::cout << std::endl; // Extra line is to clear up the code a bit.
125 }
126 
130 template<typename T>
132  util::ParamData& d,
133  const size_t indent,
134  const typename std::enable_if<arma::is_arma_type<T>::value>::type* = 0)
135 {
136  const std::string prefix(indent, ' ');
137 
138  // Capitalize the first letter of parameter name so it is
139  // of exported type in Go.
140  std::string paramName = d.name;
141  std::string goParamName = paramName;
142  if (!paramName.empty())
143  {
144  goParamName = util::CamelCase(goParamName, false);
145  }
146 
156  std::cout << prefix << "// Detect if the parameter was passed; set if so."
157  << std::endl;
158  if (!d.required)
159  {
160  std::cout << prefix << "if param." << goParamName
161  << " != nil {" << std::endl;
162 
163  // Print function call to set the given parameter into the io.
164  std::cout << prefix << prefix << "gonumToArma" << GetType<T>(d)
165  << "(params, \"" << d.name << "\", param." << goParamName
166  << ")" << std::endl;
167 
168  // Print function call to set the given parameter as passed.
169  std::cout << prefix << prefix << "setPassed(params, \"" << d.name << "\")"
170  << std::endl;
171  std::cout << prefix << "}" << std::endl; // Closing brace.
172  }
173  else
174  {
175  goParamName = util::CamelCase(goParamName, true);
176  // Print function call to set the given parameter into the io.
177  std::cout << prefix << "gonumToArma" << GetType<T>(d)
178  << "(params, \"" << d.name << "\", " << goParamName
179  << ")" << std::endl;
180 
181  // Print function call to set the given parameter as passed.
182  std::cout << prefix << "setPassed(params, \"" << d.name << "\")"
183  << std::endl;
184  }
185  std::cout << std::endl; // Extra line is to clear up the code a bit.
186 }
187 
191 template<typename T>
193  util::ParamData& d,
194  const size_t indent,
195  const typename std::enable_if<std::is_same<T,
196  std::tuple<data::DatasetInfo, arma::mat>>::value>::type* = 0)
197 {
198  const std::string prefix(indent, ' ');
199 
200  // Capitalize the first letter of parameter name so it is
201  // of exported type in Go.
202  std::string paramName = d.name;
203  std::string goParamName = paramName;
204  if (!paramName.empty())
205  {
206  goParamName = util::CamelCase(goParamName, false);
207  }
208 
218  std::cout << prefix << "// Detect if the parameter was passed; set if so."
219  << std::endl;
220  if (!d.required)
221  {
222  std::cout << prefix << "if param." << goParamName
223  << " != nil {" << std::endl;
224 
225  // Print function call to set the given parameter into the io.
226  std::cout << prefix << prefix << "gonumToArmaMatWithInfo"
227  << "(params, \"" << d.name << "\", param." << goParamName
228  << ")" << std::endl;
229 
230  // Print function call to set the given parameter as passed.
231  std::cout << prefix << prefix << "setPassed(params, \"" << d.name << "\")"
232  << std::endl;
233  std::cout << prefix << "}" << std::endl; // Closing brace.
234  }
235  else
236  {
237  goParamName = util::CamelCase(goParamName, true);
238  // Print function call to set the given parameter into the io.
239  std::cout << prefix << "gonumToArmaMatWithInfo"
240  << "(params, \"" << d.name << "\", " << goParamName
241  << ")" << std::endl;
242 
243  // Print function call to set the given parameter as passed.
244  std::cout << prefix << "setPassed(params, \"" << d.name << "\")"
245  << std::endl;
246  }
247  std::cout << std::endl; // Extra line is to clear up the code a bit.
248 }
249 
253 template<typename T>
255  util::ParamData& d,
256  const size_t indent,
257  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
258  const typename std::enable_if<data::HasSerialize<T>::value>::type* = 0)
259 {
260  // First, get the correct classparamName if needed.
261  std::string goStrippedType, strippedType, printedType, defaultsType;
262  StripType(d.cppType, goStrippedType, strippedType, printedType, defaultsType);
263 
264  const std::string prefix(indent, ' ');
265 
266  // Capitalize the first letter of parameter name so it is
267  // of exported type in Go.
268  std::string paramName = d.name;
269  std::string goParamName = paramName;
270  if (!paramName.empty())
271  {
272  goParamName = util::CamelCase(goParamName, false);
273  }
274 
284  std::cout << prefix << "// Detect if the parameter was passed; set if so."
285  << std::endl;
286  if (!d.required)
287  {
288  std::cout << prefix << "if param." << goParamName << " != nil {"
289  << std::endl;
290  // Print function call to set the given parameter into the io.
291  std::cout << prefix << prefix << "set" << strippedType << "(params, \""
292  << d.name << "\", param." << goParamName << ")" << std::endl;
293 
294  // Print function call to set the given parameter as passed.
295  std::cout << prefix << prefix << "setPassed(params, \"" << d.name << "\")"
296  << std::endl;
297  std::cout << prefix << "}" << std::endl; // Closing brace.
298  }
299  else
300  {
301  goParamName = util::CamelCase(goParamName, true);
302  // Print function call to set the given parameter into the io.
303  std::cout << prefix << "set" << strippedType << "(params, \"" << d.name
304  << "\", " << goParamName << ")" << std::endl;
305 
306  // Print function call to set the given parameter as passed.
307  std::cout << prefix << "setPassed(params, \"" << d.name << "\")"
308  << std::endl;
309  }
310  std::cout << std::endl; // Extra line is to clear up the code a bit.
311 }
312 
324 template<typename T>
326  const void* input,
327  void* /* output */)
328 {
329  PrintInputProcessing<typename std::remove_pointer<T>::type>(d,
330  *((size_t*) input));
331 }
332 
333 } // namespace go
334 } // namespace bindings
335 } // namespace mlpack
336 
337 #endif
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
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes, Armadillo, cereal, and a few basic mlpa...
go
Definition: CMakeLists.txt:7
MLPACK_ANY value
The actual value that is held.
Definition: param_data.hpp:65
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:38
std::string CamelCase(std::string s, bool lower)
Given an snake_case like, e.g., "logistic_regression", return CamelCase(e.g.
Definition: camel_case.hpp:26
void PrintInputProcessing(util::ParamData &d, const size_t indent, const typename std::enable_if<!arma::is_arma_type< T >::value >::type *=0, const typename std::enable_if<!data::HasSerialize< T >::value >::type *=0, const typename std::enable_if<!std::is_same< T, std::tuple< data::DatasetInfo, arma::mat >>::value >::type *=0)
Print input processing for a standard option type.
std::string name
Name of this parameter.
Definition: param_data.hpp:42
bool required
True if this option is required.
Definition: param_data.hpp:57
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:67
if(NOT BUILD_GO_SHLIB) macro(add_go_binding name) endmacro() return() endif() endmacro() macro(post_go_setup) if(BUILD_GO_BINDINGS) file(APPEND "$
Definition: CMakeLists.txt:3