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 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 = 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 = 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 io.
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  goParamName = util::CamelCase(goParamName, true);
114  // Print function call to set the given parameter into the io.
115  std::cout << prefix << "setParam" << GetType<T>(d) << "(\""
116  << d.name << "\", " << goParamName << ")"
117  << std::endl;
118 
119  // Print function call to set the given parameter as passed.
120  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
121  }
122  std::cout << std::endl; // Extra line is to clear up the code a bit.
123 }
124 
128 template<typename T>
130  util::ParamData& d,
131  const size_t indent,
132  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
133 {
134  const std::string prefix(indent, ' ');
135 
136  // Capitalize the first letter of parameter name so it is
137  // of exported type in Go.
138  std::string paramName = d.name;
139  std::string goParamName = paramName;
140  if (!paramName.empty())
141  {
142  goParamName = util::CamelCase(goParamName, false);
143  }
144 
154  std::cout << prefix << "// Detect if the parameter was passed; set if so."
155  << std::endl;
156  if (!d.required)
157  {
158  std::cout << prefix << "if param." << goParamName
159  << " != nil {" << std::endl;
160 
161  // Print function call to set the given parameter into the io.
162  std::cout << prefix << prefix << "gonumToArma" << GetType<T>(d)
163  << "(\"" << d.name << "\", param." << goParamName
164  << ")" << std::endl;
165 
166  // Print function call to set the given parameter as passed.
167  std::cout << prefix << prefix << "setPassed(\"" << d.name << "\")"
168  << std::endl;
169  std::cout << prefix << "}" << std::endl; // Closing brace.
170  }
171  else
172  {
173  goParamName = util::CamelCase(goParamName, true);
174  // Print function call to set the given parameter into the io.
175  std::cout << prefix << "gonumToArma" << GetType<T>(d)
176  << "(\"" << d.name << "\", " << goParamName
177  << ")" << std::endl;
178 
179  // Print function call to set the given parameter as passed.
180  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
181  }
182  std::cout << std::endl; // Extra line is to clear up the code a bit.
183 }
184 
188 template<typename T>
190  util::ParamData& d,
191  const size_t indent,
192  const typename boost::enable_if<std::is_same<T,
193  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
194 {
195  const std::string prefix(indent, ' ');
196 
197  // Capitalize the first letter of parameter name so it is
198  // of exported type in Go.
199  std::string paramName = d.name;
200  std::string goParamName = paramName;
201  if (!paramName.empty())
202  {
203  goParamName = util::CamelCase(goParamName, false);
204  }
205 
215  std::cout << prefix << "// Detect if the parameter was passed; set if so."
216  << std::endl;
217  if (!d.required)
218  {
219  std::cout << prefix << "if param." << goParamName
220  << " != nil {" << std::endl;
221 
222  // Print function call to set the given parameter into the io.
223  std::cout << prefix << prefix << "gonumToArmaMatWithInfo"
224  << "(\"" << d.name << "\", param." << goParamName
225  << ")" << std::endl;
226 
227  // Print function call to set the given parameter as passed.
228  std::cout << prefix << prefix << "setPassed(\"" << d.name << "\")"
229  << std::endl;
230  std::cout << prefix << "}" << std::endl; // Closing brace.
231  }
232  else
233  {
234  goParamName = util::CamelCase(goParamName, true);
235  // Print function call to set the given parameter into the io.
236  std::cout << prefix << "gonumToArmaMatWithInfo"
237  << "(\"" << d.name << "\", " << goParamName
238  << ")" << std::endl;
239 
240  // Print function call to set the given parameter as passed.
241  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
242  }
243  std::cout << std::endl; // Extra line is to clear up the code a bit.
244 }
245 
249 template<typename T>
251  util::ParamData& d,
252  const size_t indent,
253  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
254  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
255 {
256  // First, get the correct classparamName if needed.
257  std::string goStrippedType, strippedType, printedType, defaultsType;
258  StripType(d.cppType, goStrippedType, strippedType, printedType, defaultsType);
259 
260  const std::string prefix(indent, ' ');
261 
262  // Capitalize the first letter of parameter name so it is
263  // of exported type in Go.
264  std::string paramName = d.name;
265  std::string goParamName = paramName;
266  if (!paramName.empty())
267  {
268  goParamName = util::CamelCase(goParamName, false);
269  }
270 
280  std::cout << prefix << "// Detect if the parameter was passed; set if so."
281  << std::endl;
282  if (!d.required)
283  {
284  std::cout << prefix << "if param." << goParamName << " != nil {"
285  << std::endl;
286  // Print function call to set the given parameter into the io.
287  std::cout << prefix << prefix << "set" << strippedType << "(\""
288  << d.name << "\", param." << goParamName << ")" << std::endl;
289 
290  // Print function call to set the given parameter as passed.
291  std::cout << prefix << prefix << "setPassed(\"" << d.name << "\")"
292  << std::endl;
293  std::cout << prefix << "}" << std::endl; // Closing brace.
294  }
295  else
296  {
297  goParamName = util::CamelCase(goParamName, true);
298  // Print function call to set the given parameter into the io.
299  std::cout << prefix << "set" << strippedType << "(\"" << d.name
300  << "\", " << goParamName << ")" << std::endl;
301 
302  // Print function call to set the given parameter as passed.
303  std::cout << prefix << "setPassed(\"" << d.name << "\")" << std::endl;
304  }
305  std::cout << std::endl; // Extra line is to clear up the code a bit.
306 }
307 
319 template<typename T>
321  const void* input,
322  void* /* output */)
323 {
324  PrintInputProcessing<typename std::remove_pointer<T>::type>(d,
325  *((size_t*) input));
326 }
327 
328 } // namespace go
329 } // namespace bindings
330 } // namespace mlpack
331 
332 #endif
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
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
go
Definition: CMakeLists.txt:6
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
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
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
void PrintInputProcessing(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.
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84
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