print_class_defn.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_BINDINGS_PYTHON_PRINT_CLASS_DEFN_HPP
13 #define MLPACK_BINDINGS_PYTHON_PRINT_CLASS_DEFN_HPP
14 
15 #include "strip_type.hpp"
16 
17 namespace mlpack {
18 namespace bindings {
19 namespace python {
20 
25 template<typename T>
27  const util::ParamData& /* d */,
28  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
29  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0)
30 {
31  // Do nothing.
32 }
33 
37 template<typename T>
39  const util::ParamData& /* d */,
40  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
41 {
42  // Do nothing.
43 }
44 
48 template<typename T>
50  const util::ParamData& d,
51  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
52  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
53 {
54  // First, we have to parse the type. If we have something like, e.g.,
55  // 'LogisticRegression<>', we must convert this to 'LogisticRegression[].'
56  std::string strippedType, printedType, defaultsType;
57  StripType(d.cppType, strippedType, printedType, defaultsType);
58 
80  std::cout << "cdef class " << strippedType << "Type:" << std::endl;
81  std::cout << " cdef " << printedType << "* modelptr" << std::endl;
82  std::cout << std::endl;
83  std::cout << " def __cinit__(self):" << std::endl;
84  std::cout << " self.modelptr = new " << printedType << "()" << std::endl;
85  std::cout << std::endl;
86  std::cout << " def __dealloc__(self):" << std::endl;
87  std::cout << " del self.modelptr" << std::endl;
88  std::cout << std::endl;
89  std::cout << " def __getstate__(self):" << std::endl;
90  std::cout << " return SerializeOut(self.modelptr, \"" << printedType
91  << "\")" << std::endl;
92  std::cout << std::endl;
93  std::cout << " def __setstate__(self, state):" << std::endl;
94  std::cout << " SerializeIn(self.modelptr, state, \"" << printedType
95  << "\")" << std::endl;
96  std::cout << std::endl;
97  std::cout << " def __reduce_ex__(self, version):" << std::endl;
98  std::cout << " return (self.__class__, (), self.__getstate__())"
99  << std::endl;
100  std::cout << std::endl;
101 }
102 
111 template<typename T>
113  const void* /* input */,
114  void* /* output */)
115 {
116  PrintClassDefn<typename std::remove_pointer<T>::type>(d);
117 }
118 
119 } // namespace python
120 } // namespace bindings
121 } // namespace mlpack
122 
123 #endif
void PrintClassDefn(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)
Non-serializable models don&#39;t require any special definitions, so this prints nothing.
strip_type.hpp
Definition: add_to_po.hpp:21
python
Definition: CMakeLists.txt:4
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
void StripType(const std::string &inputType, std::string &strippedType, std::string &printedType, std::string &defaultsType)
Given an input type like, e.g., "LogisticRegression<>", return three types that can be used in Python...
Definition: strip_type.hpp:28
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84