print_model_util.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_BINDINGS_GO_PRINT_CLASS_DEFN_HPP
14 #define MLPACK_BINDINGS_GO_PRINT_CLASS_DEFN_HPP
15 
16 #include "strip_type.hpp"
17 
18 namespace mlpack {
19 namespace bindings {
20 namespace go {
21 
26 template<typename T>
28  const util::ParamData& /* d */,
29  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
30  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0,
31  const typename boost::disable_if<std::is_same<T,
32  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
33 {
34  // Do nothing.
35 }
36 
40 template<typename T>
42  const util::ParamData& /* d */,
43  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
44 {
45  // Do nothing.
46 }
47 
51 template<typename T>
53  const util::ParamData& /* d */,
54  const typename boost::enable_if<std::is_same<T,
55  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
56 {
57  // Do nothing.
58 }
59 
63 template<typename T>
65  const util::ParamData& d,
66  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
67  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
68 {
69  // First, we have to parse the type. If we have something like, e.g.,
70  // 'LogisticRegression<>', we must convert this to 'LogisticRegression[].'
71  std::string goStrippedType, strippedType, printedType, defaultsType;
72  StripType(d.cppType, goStrippedType, strippedType, printedType, defaultsType);
85  std::cout << "extern \"C\" void mlpackSet" << strippedType
86  << "Ptr(" << std::endl;
87  std::cout << " const char* identifier, " << std::endl;
88  std::cout << " void* value)" << std::endl;
89  std::cout << "{" << std::endl;
90  std::cout << " SetParamPtr<" << printedType
91  << ">(identifier," << std::endl;
92  std::cout << " static_cast<" << printedType
93  << "*>(value));" << std::endl;
94  std::cout << "}" << std::endl;
95  std::cout << std::endl;
96 
107  std::cout << "extern \"C\" void *mlpackGet" << strippedType
108  << "Ptr(const char* identifier)" << std::endl;
109  std::cout << "{" << std::endl;
110  std::cout << " " << printedType << " *modelptr = GetParamPtr<"
111  << printedType << ">(identifier);" << std::endl;
112  std::cout << " return modelptr;" << std::endl;
113  std::cout << "}" << std::endl;
114  std::cout << std::endl;
115 }
116 
124 template<typename T>
126  const void* /* input */,
127  void* /* output */)
128 {
129  PrintModelUtilCPP<typename std::remove_pointer<T>::type>(d);
130 }
131 
132 
137 template<typename T>
139  const util::ParamData& /* d */,
140  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
141  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0,
142  const typename boost::disable_if<std::is_same<T,
143  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
144 
145 {
146  // Do nothing.
147 }
148 
152 template<typename T>
154  const util::ParamData& /* d */,
155  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
156 {
157  // Do nothing.
158 }
159 
163 template<typename T>
165  const util::ParamData& /* d */,
166  const typename boost::enable_if<std::is_same<T,
167  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
168 {
169  // Do nothing.
170 }
171 
175 template<typename T>
177  const util::ParamData& d,
178  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
179  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
180 {
181  // First, we have to parse the type. If we have something like, e.g.,
182  // 'LogisticRegression<>', we must convert this to 'LogisticRegression[].'
183  std::string goStrippedType, strippedType, printedType, defaultsType;
184  StripType(d.cppType, goStrippedType, strippedType, printedType, defaultsType);
185 
192  std::cout << "extern void mlpackSet" << strippedType
193  << "Ptr(const char* identifier, void* value);" << std::endl;
194  std::cout << std::endl;
195 
202  std::cout << "extern void *mlpackGet" << strippedType
203  << "Ptr(const char* identifier);" << std::endl;
204  std::cout << std::endl;
205 }
206 
214 template<typename T>
216  const void* /* input */,
217  void* /* output */)
218 {
219  PrintModelUtilH<typename std::remove_pointer<T>::type>(d);
220 }
221 
226 template<typename T>
228  const util::ParamData& /* d */,
229  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
230  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0,
231  const typename boost::disable_if<std::is_same<T,
232  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
233 {
234  // Do nothing.
235 }
236 
240 template<typename T>
242  const util::ParamData& /* d */,
243  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
244 {
245  // Do nothing.
246 }
247 
251 template<typename T>
253  const util::ParamData& /* d */,
254  const typename boost::enable_if<std::is_same<T,
255  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
256 {
257  // Do nothing.
258 }
259 
263 template<typename T>
265  const util::ParamData& d,
266  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
267  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
268 {
269  // First, we have to parse the type. If we have something like, e.g.,
270  // 'LogisticRegression<>', we must convert this to 'LogisticRegression[].'
271  std::string goStrippedType, strippedType, printedType, defaultsType;
272  StripType(d.cppType, goStrippedType, strippedType, printedType, defaultsType);
273 
282  std::cout << "type " << goStrippedType << " struct {" << std::endl;
283  std::cout << " mem unsafe.Pointer" << std::endl;
284  std::cout << "}" << std::endl;
285  std::cout << std::endl;
286 
296  std::cout << "func (m *" << goStrippedType << ") alloc"
297  << strippedType << "(identifier string) {" << std::endl;
298  std::cout << " m.mem = C.mlpackGet" << strippedType
299  << "Ptr(C.CString(identifier))" << std::endl;
300  std::cout << " runtime.KeepAlive(m)" << std::endl;
301  std::cout << "}" << std::endl;
302  std::cout << std::endl;
303 
312  std::cout << "func (m *" << goStrippedType << ") get"
313  << strippedType << "(identifier string) {" << std::endl;
314  std::cout << " m.alloc" << strippedType << "(identifier)" << std::endl;
315  std::cout << "}" << std::endl;
316  std::cout << std::endl;
317 
318  // Print function to set specified mlpack parameter object ptr from Go.
319  std::cout << "func set" << strippedType
320  << "(identifier string, ptr *" << goStrippedType << ") {"
321  << std::endl;
322  std::cout << " C.mlpackSet" << strippedType
323  << "Ptr(C.CString(identifier), (unsafe.Pointer)(ptr.mem))"
324  << std::endl;
325  std::cout << "}" << std::endl;
326  std::cout << std::endl;
327 }
328 
337 template<typename T>
339  const void* /* input */,
340  void* /* output */)
341 {
342  PrintModelUtilGo<typename std::remove_pointer<T>::type>(d);
343 }
344 
345 } // namespace go
346 } // namespace bindings
347 } // namespace mlpack
348 
349 #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
strip_type.hpp
Definition: add_to_po.hpp:21
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
void PrintModelUtilGo(const util::ParamData &, 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)
Non-serializable models don&#39;t require any special definitions, so this prints nothing.
void PrintModelUtilCPP(const util::ParamData &, 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)
Non-serializable models don&#39;t require any special definitions, so this prints nothing.
void PrintModelUtilH(const util::ParamData &, 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)
Non-serializable models don&#39;t require any special definitions, so this prints nothing.
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84