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 GetParam<Type>(params::Ptr{Nothing},
62  // paramName::String,
63  // modelPtrs::Set{Ptr{Nothing}})
64  // ptr = ccall((:GetParam<Type>Ptr, <programName>Library),
65  // Ptr{Nothing}, (Ptr{Nothing}, Cstring,), params, paramName)
66  // return <Type>(ptr; finalize=!(ptr in modelPtrs))
67  // end
68  //
69  // function SetParam<Type>(params::Ptr{Nothing},
70  // paramName::String,
71  // model::<Type>)
72  // ccall((:SetParam<Type>Ptr, <programName>Library), Nothing,
73  // (Ptr{Nothing}, Cstring, Ptr{Nothing}), params, paramName, model.ptr)
74  // end
75  //
76  // function Delete<Type>(ptr::Ptr{Nothing})
77  // ccall((:Delete<Type>Ptr, <programName>Library), Nothing,
78  // (Ptr{Nothing},), ptr)
79  // end
80  //
81  // function serialize<Type>(stream::IO, model::<Type>)
82  // buf_len = UInt[0]
83  // buffer = ccall((:Serialize<Type>Ptr, <programName>Library),
84  // Vector{UInt8}, (Ptr{Nothing}, Ptr{UInt8}), model.ptr,
85  // Base.pointer(buf_len))
86  // buf = Base.unsafe_wrap(buf_ptr, buf_len[1]; own=true)
87  // write(stream, buf_len[1])
88  // write(stream, buf)
89  // end
90  //
91  // function deserialize<Type>(stream::IO)::<Type>
92  // buf_len = read(stream, UInt)
93  // buffer = read(stream, buf_len)
94  // <Type>(ccall((:Deserialize<Type>Ptr, <programName>Library),
95  // Ptr{Nothing}, (Vector{UInt8}, UInt), buffer, length(buffer)))
96  // end
97 
98  std::string type = util::StripType(d.cppType);
99 
100  // First, print the import of the struct.
101  std::cout << "import ..." << type << std::endl;
102  std::cout << std::endl;
103 
104  // Now, GetParam<Type>().
105  std::cout << "# Get the value of a model pointer parameter of type " << type
106  << "." << std::endl;
107  std::cout << "function GetParam" << type << "(params::Ptr{Nothing}, "
108  << "paramName::String, modelPtrs::Set{Ptr{Nothing}})::" << type
109  << std::endl;
110  std::cout << " ptr = ccall((:GetParam" << type
111  << "Ptr, " << programName << "Library), Ptr{Nothing}, (Ptr{Nothing}, "
112  << "Cstring,), params, paramName)" << std::endl;
113  std::cout << " return " << type << "(ptr; finalize=!(ptr in modelPtrs))"
114  << std::endl;
115  std::cout << "end" << std::endl;
116  std::cout << std::endl;
117 
118  // Next, IOSetParam<Type>().
119  std::cout << "# Set the value of a model pointer parameter of type " << type
120  << "." << std::endl;
121  std::cout << "function SetParam" << type << "(params::Ptr{Nothing}, "
122  << "paramName::String, model::" << type << ")" << std::endl;
123  std::cout << " ccall((:SetParam" << type << "Ptr, "
124  << programName << "Library), Nothing, (Ptr{Nothing}, Cstring, "
125  << "Ptr{Nothing}), params, paramName, model.ptr)" << std::endl;
126  std::cout << "end" << std::endl;
127  std::cout << std::endl;
128 
129  // Next, Delete<Type>().
130  std::cout << "# Delete an instantiated model pointer." << std::endl;
131  std::cout << "function Delete" << type << "(ptr::Ptr{Nothing})"
132  << std::endl;
133  std::cout << " ccall((:Delete" << type << "Ptr, " << programName
134  << "Library), Nothing, (Ptr{Nothing},), ptr)" << std::endl;
135  std::cout << "end" << std::endl;
136  std::cout << std::endl;
137 
138  // Now the serialization functionality.
139  std::cout << "# Serialize a model to the given stream." << std::endl;
140  std::cout << "function serialize" << type << "(stream::IO, model::" << type
141  << ")" << std::endl;
142  std::cout << " buf_len = UInt[0]" << std::endl;
143  std::cout << " buf_ptr = ccall((:Serialize" << type << "Ptr, " << programName
144  << "Library), Ptr{UInt8}, (Ptr{Nothing}, Ptr{UInt}), model.ptr, "
145  << "Base.pointer(buf_len))" << std::endl;
146  std::cout << " buf = Base.unsafe_wrap(Vector{UInt8}, buf_ptr, buf_len[1]; "
147  << "own=true)" << std::endl;
148  std::cout << " write(stream, buf_len[1])" << std::endl;
149  std::cout << " write(stream, buf)" << std::endl;
150  std::cout << "end" << std::endl;
151 
152  // And the deserialization functionality.
153  std::cout << "# Deserialize a model from the given stream." << std::endl;
154  std::cout << "function deserialize" << type << "(stream::IO)::" << type
155  << std::endl;
156  std::cout << " buf_len = read(stream, UInt)" << std::endl;
157  std::cout << " buffer = read(stream, buf_len)" << std::endl;
158  std::cout << " " << type << "(ccall((:Deserialize" << type << "Ptr, "
159  << programName << "Library), Ptr{Nothing}, (Ptr{UInt8}, UInt), "
160  << "Base.pointer(buffer), length(buffer)))" << std::endl;
161  std::cout << "end" << std::endl;
162 }
163 
168 template<typename T>
170  const void* input,
171  void* /* output */)
172 {
173  PrintParamDefn<typename std::remove_pointer<T>::type>(d,
174  *(std::string*) input);
175 }
176 
177 } // namespace julia
178 } // namespace bindings
179 } // namespace mlpack
180 
181 #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:7
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:81