Skip to content

Commit 99b8f13

Browse files
committed
Merge pull request #55 from gicmo/vanilla_mptr_ref
GLUE for non member functions
2 parents 653a65a + 38b28b9 commit 99b8f13

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

nix_mx.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,7 @@ struct fendpoint {
5353
};
5454

5555
const std::vector<fendpoint> funcs = {
56-
{ "Entity::destroy", entity_destroy },
57-
{ "Entity::updatedAt", entity_updated_at },
58-
5956
// File
60-
{ "File::open", nixfile::open },
61-
{ "File::describe", nixfile::describe },
6257
{ "File::listBlocks", nixfile::list_blocks },
6358
{ "File::listSections", nixfile::list_sections },
6459
{ "File::createBlock", nixfile::create_block },
@@ -147,7 +142,12 @@ void mexFunction(int nlhs,
147142

148143
methods = new registry{};
149144

145+
methods->add("Entity::destory", entity_destroy);
146+
methods->add("Entity::updatedAt", entity_updated_at);
147+
150148
classdef<nix::File>("File", methods)
149+
.desc(&nixfile::describe)
150+
.add("open", nixfile::open)
151151
.reg("blocks", GETTER(std::vector<nix::Block>, nix::File, blocks))
152152
.reg("sections", GETTER(std::vector<nix::Section>, nix::File, sections))
153153
.reg("deleteBlock", REMOVER(nix::Block, nix::File, deleteBlock))

src/nixfile.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,17 @@ void open(const extractor &input, infusor &output)
3030
output.set(0, h);
3131
}
3232

33-
void describe(const extractor &input, infusor &output)
34-
{
35-
nix::File fd = input.entity<nix::File>(1);
36-
33+
mxArray *describe(const nix::File &fd) {
3734
struct_builder sb({ 1 }, { "format", "version", "location", "createdAt", "updatedAt" });
3835
sb.set(fd.format());
3936
sb.set(fd.version());
4037
sb.set(fd.location());
4138
sb.set(static_cast<uint64_t>(fd.createdAt()));
4239
sb.set(static_cast<uint64_t>(fd.updatedAt()));
43-
44-
output.set(0, sb.array());
40+
return sb.array();
4541
}
4642

43+
4744
void list_blocks(const extractor &input, infusor &output)
4845
{
4946
nix::File fd = input.entity<nix::File>(1);

src/nixfile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace nixfile {
77

88
void open(const extractor &input, infusor &output);
99

10-
void describe(const extractor &input, infusor &output);
10+
mxArray *describe(const nix::File &f);
1111

1212
void list_blocks(const extractor &input, infusor &output);
1313

src/utils/glue.h

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,34 @@ struct funcbox : box {
211211
matryoshka_t args;
212212
};
213213

214+
template<typename Klazz>
215+
struct getter : box {
216+
typedef mxArray *(*get_fun)(const Klazz &k);
217+
218+
getter(get_fun f) : fun(f) { }
219+
220+
void operator()(const extractor &input, infusor &output) {
221+
Klazz entity = input.entity<Klazz>(1);
222+
mxArray *res = fun(entity);
223+
output.set(0, res);
224+
}
225+
226+
get_fun fun;
227+
};
228+
229+
230+
struct fntbox : box {
231+
typedef void(*fn_t)(const extractor &input, infusor &output);
232+
233+
fntbox(fn_t f) : fun(f) { }
234+
235+
void operator()(const extractor &input, infusor &output) {
236+
fun(input, output);
237+
}
238+
239+
private:
240+
fn_t fun;
241+
};
214242

215243
};
216244

@@ -228,8 +256,14 @@ struct registry {
228256
return false;
229257
}
230258

231-
void add(const std::string &name, funky::box *b) {
259+
registry& add(const std::string &name, funky::box *b) {
232260
funcs[name] = b;
261+
return *this;
262+
}
263+
264+
registry &add(const std::string &name, funky::fntbox::fn_t fun) {
265+
funky::box *b = new funky::fntbox(fun);
266+
return add(name, b);
233267
}
234268

235269
~registry() {
@@ -255,6 +289,18 @@ struct classdef {
255289
return *this;
256290
}
257291

292+
classdef &desc(typename funky::getter<Klass>::get_fun fun) {
293+
funky::box *b = new funky::getter<Klass>(fun);
294+
lib->add(prefix + "::describe", b);
295+
return *this;
296+
}
297+
298+
classdef &add(const std::string &name, funky::fntbox::fn_t fun) {
299+
lib->add(prefix + "::" + name, fun);
300+
return *this;
301+
}
302+
303+
258304
private:
259305
std::string prefix;
260306
registry *lib;

0 commit comments

Comments
 (0)