print_param_defn.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_BINDINGS_JULIA_PRINT_PARAM_DEFN_HPP
14 #define MLPACK_BINDINGS_JULIA_PRINT_PARAM_DEFN_HPP
15 
17 
18 namespace mlpack {
19 namespace bindings {
20 namespace julia {
21 
25 template<typename T>
27  util::ParamData& /* d */,
28  const std::string& /* programName */,
29  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
30  const typename std::enable_if<!data::HasSerialize<T>::value>::type* = 0)
31 {
32  // Do nothing.
33 }
34 
38 template<typename T>
40  util::ParamData& /* d */,
41  const std::string& /* programName */,
42  const typename std::enable_if<arma::is_arma_type<T>::value>::type* = 0)
43 {
44  // Do nothing.
45 }
46 
50 template<typename T>
52  util::ParamData& d,
53  const std::string& programName,
54  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
55  const typename std::enable_if<data::HasSerialize<T>::value>::type* = 0)
56 {
57  // We need to print something of the form below:
58  //
59  // import ...<Type>
60  //
61  // function IOGetParam<Type>(paramName::String, modelPtrs::Set{Ptr{Nothing}})
62  // ptr = ccall((:IO_GetParam<Type>Ptr, <programName>Library),
63  // Ptr{Nothing}, (Cstring,), paramName)
64  // return <Type>(ptr; finalize=!(ptr in modelPtrs))
65  // end
66  //
67  // function IOSetParam<Type>(paramName::String, model::<Type>)
68  // ccall((:IO_SetParam<Type>Ptr, <programName>Library), Nothing,
69  // (Cstring, Ptr{Nothing}), paramName, model.ptr)
70  // end
71  //
72  // function Delete<Type>(ptr::Ptr{Nothing})
73  // ccall((:Delete<Type>Ptr, <programName>Library), Nothing,
74  // (Ptr{Nothing},), ptr)
75  // end
76  //
77  // function serialize<Type>(stream::IO, model::<Type>)
78  // buf_len = UInt[0]
79  // buffer = ccall((:Serialize<Type>Ptr, <programName>Library),
80  // Vector{UInt8}, (Ptr{Nothing}, Ptr{UInt8}), model.ptr,
81  // Base.pointer(buf_len))
82  // buf = Base.unsafe_wrap(buf_ptr, buf_len[0]; own=true)
83  // write(stream, buf)
84  // end
85  //
86  // function deserialize<Type>(stream::IO)::<Type>
87  // buffer = read(stream)
88  // <Type>(ccall((:Deserialize<Type>Ptr, <programName>Library),
89  // Ptr{Nothing}, (Vector{UInt8}, UInt), buffer, length(buffer)))
90  // end
91 
92  std::string type = util::StripType(d.cppType);
93 
94  // First, print the import of the struct.
95  std::cout << "import ..." << type << std::endl;
96  std::cout << std::endl;
97 
98  // Now, IOGetParam<Type>().
99  std::cout << "# Get the value of a model pointer parameter of type " << type
100  << "." << std::endl;
101  std::cout << "function IOGetParam" << type << "(paramName::String, "
102  << "modelPtrs::Set{Ptr{Nothing}})::" << type << std::endl;
103  std::cout << " ptr = ccall((:IO_GetParam" << type
104  << "Ptr, " << programName << "Library), Ptr{Nothing}, (Cstring,), "
105  << "paramName)" << std::endl;
106  std::cout << " return " << type << "(ptr; finalize=!(ptr in modelPtrs))"
107  << std::endl;
108  std::cout << "end" << std::endl;
109  std::cout << std::endl;
110 
111  // Next, IOSetParam<Type>().
112  std::cout << "# Set the value of a model pointer parameter of type " << type
113  << "." << std::endl;
114  std::cout << "function IOSetParam" << type << "(paramName::String, "
115  << "model::" << type << ")" << std::endl;
116  std::cout << " ccall((:IO_SetParam" << type << "Ptr, "
117  << programName << "Library), Nothing, (Cstring, "
118  << "Ptr{Nothing}), paramName, model.ptr)" << std::endl;
119  std::cout << "end" << std::endl;
120  std::cout << std::endl;
121 
122  // Next, Delete<Type>().
123  std::cout << "# Delete an instantiated model pointer." << std::endl;
124  std::cout << "function Delete" << type << "(ptr::Ptr{Nothing})"
125  << std::endl;
126  std::cout << " ccall((:Delete" << type << "Ptr, " << programName
127  << "Library), Nothing, (Ptr{Nothing},), ptr)" << std::endl;
128  std::cout << "end" << std::endl;
129  std::cout << std::endl;
130 
131  // Now the serialization functionality.
132  std::cout << "# Serialize a model to the given stream." << std::endl;
133  std::cout << "function serialize" << type << "(stream::IO, model::" << type
134  << ")" << std::endl;
135  std::cout << " buf_len = UInt[0]" << std::endl;
136  std::cout << " buf_ptr = ccall((:Serialize" << type << "Ptr, " << programName
137  << "Library), Ptr{UInt8}, (Ptr{Nothing}, Ptr{UInt}), model.ptr, "
138  << "Base.pointer(buf_len))" << std::endl;
139  std::cout << " buf = Base.unsafe_wrap(Vector{UInt8}, buf_ptr, buf_len[1]; "
140  << "own=true)" << std::endl;
141  std::cout << " write(stream, buf)" << std::endl;
142  std::cout << "end" << std::endl;
143 
144  // And the deserialization functionality.
145  std::cout << "# Deserialize a model from the given stream." << std::endl;
146  std::cout << "function deserialize" << type << "(stream::IO)::" << type
147  << std::endl;
148  std::cout << " buffer = read(stream)" << std::endl;
149  std::cout << " " << type << "(ccall((:Deserialize" << type << "Ptr, "
150  << programName << "Library), Ptr{Nothing}, (Ptr{UInt8}, UInt), "
151  << "Base.pointer(buffer), length(buffer)))" << std::endl;
152  std::cout << "end" << std::endl;
153 }
154 
159 template<typename T>
161  const void* input,
162  void* /* output */)
163 {
164  PrintParamDefn<typename std::remove_pointer<T>::type>(d,
165  *(std::string*) input);
166 }
167 
168 } // namespace julia
169 } // namespace bindings
170 } // namespace mlpack
171 
172 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
void PrintParamDefn(util::ParamData &, const std::string &, const typename std::enable_if<!arma::is_arma_type< T >::value >::type *=0, const typename std::enable_if<!data::HasSerialize< T >::value >::type *=0)
If the type is not serializable, print nothing.
julia
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 StripType(std::string cppType)
Given a C++ type name, turn it into something that has no special characters that can simply be print...
Definition: strip_type.hpp:27
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84