mlpack 3.4.2
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
18namespace mlpack {
19namespace bindings {
20namespace julia {
21
25template<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
38template<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
50template<typename T>
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)
62 // <Type>(ccall((:IOGetParam<Type>Ptr, <programName>Library),
63 // Ptr{Nothing}, (Cstring,), paramName))
64 // end
65 //
66 // function IOSetParam<Type>(paramName::String, model::<Type>)
67 // ccall((:IOSetParam<Type>Ptr, <programName>Library), Nothing,
68 // (Cstring, Ptr{Nothing}), paramName, model.ptr)
69 // end
70 //
71 // function serialize<Type>(stream::IO, model::<Type>)
72 // buf_len = UInt[0]
73 // buffer = ccall((:Serialize<Type>Ptr, <programName>Library),
74 // Vector{UInt8}, (Ptr{Nothing}, Ptr{UInt8}), model.ptr,
75 // Base.pointer(buf_len))
76 // buf = Base.unsafe_wrap(buf_ptr, buf_len[0]; own=true)
77 // write(stream, buf)
78 // end
79 //
80 // function deserialize<Type>(stream::IO)::<Type>
81 // buffer = read(stream)
82 // <Type>(ccall((:Deserialize<Type>Ptr, <programName>Library),
83 // Ptr{Nothing}, (Vector{UInt8}, UInt), buffer, length(buffer)))
84 // end
85
86 std::string type = util::StripType(d.cppType);
87
88 // First, print the import of the struct.
89 std::cout << "import ..." << type << std::endl;
90 std::cout << std::endl;
91
92 // Now, IOGetParam<Type>().
93 std::cout << "# Get the value of a model pointer parameter of type " << type
94 << "." << std::endl;
95 std::cout << "function IOGetParam" << type << "(paramName::String)::"
96 << type << std::endl;
97 std::cout << " " << type << "(ccall((:IO_GetParam" << type
98 << "Ptr, " << programName << "Library), Ptr{Nothing}, (Cstring,), "
99 << "paramName))" << std::endl;
100 std::cout << "end" << std::endl;
101 std::cout << std::endl;
102
103 // Next, IOSetParam<Type>().
104 std::cout << "# Set the value of a model pointer parameter of type " << type
105 << "." << std::endl;
106 std::cout << "function IOSetParam" << type << "(paramName::String, "
107 << "model::" << type << ")" << std::endl;
108 std::cout << " ccall((:IO_SetParam" << type << "Ptr, "
109 << programName << "Library), Nothing, (Cstring, "
110 << "Ptr{Nothing}), paramName, model.ptr)" << std::endl;
111 std::cout << "end" << std::endl;
112 std::cout << std::endl;
113
114 // Now the serialization functionality.
115 std::cout << "# Serialize a model to the given stream." << std::endl;
116 std::cout << "function serialize" << type << "(stream::IO, model::" << type
117 << ")" << std::endl;
118 std::cout << " buf_len = UInt[0]" << std::endl;
119 std::cout << " buf_ptr = ccall((:Serialize" << type << "Ptr, " << programName
120 << "Library), Ptr{UInt8}, (Ptr{Nothing}, Ptr{UInt}), model.ptr, "
121 << "Base.pointer(buf_len))" << std::endl;
122 std::cout << " buf = Base.unsafe_wrap(Vector{UInt8}, buf_ptr, buf_len[1]; "
123 << "own=true)" << std::endl;
124 std::cout << " write(stream, buf)" << std::endl;
125 std::cout << "end" << std::endl;
126
127 // And the deserialization functionality.
128 std::cout << "# Deserialize a model from the given stream." << std::endl;
129 std::cout << "function deserialize" << type << "(stream::IO)::" << type
130 << std::endl;
131 std::cout << " buffer = read(stream)" << std::endl;
132 std::cout << " " << type << "(ccall((:Deserialize" << type << "Ptr, "
133 << programName << "Library), Ptr{Nothing}, (Ptr{UInt8}, UInt), "
134 << "Base.pointer(buffer), length(buffer)))" << std::endl;
135 std::cout << "end" << std::endl;
136}
137
142template<typename T>
144 const void* input,
145 void* /* output */)
146{
147 PrintParamDefn<typename std::remove_pointer<T>::type>(d,
148 *(std::string*) input);
149}
150
151} // namespace julia
152} // namespace bindings
153} // namespace mlpack
154
155#endif
julia
Definition: CMakeLists.txt:6
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.
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
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:53
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84