Skip to content

Commit

Permalink
ConfigItemReg replace GET macro methods (#323)
Browse files Browse the repository at this point in the history
* ConfigItemReg  methods

* fixed function name "Get_"

Signed-off-by: shewer <[email protected]>

---------

Signed-off-by: shewer <[email protected]>
  • Loading branch information
shewer authored Apr 13, 2024
1 parent 20ddea9 commit 7c1b939
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions src/types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,11 @@ namespace ConfigItemReg {
using L = ConfigList;
using V = ConfigValue;

template <class R>
an<R> Get(an<T> t) {
return std::dynamic_pointer_cast<R>(t);
};

string type(T &t){
switch (t.type()) {
case T::kNull: return "kNull";
Expand All @@ -1119,30 +1124,37 @@ namespace ConfigItemReg {
return "";
}

//START_GET_
//sed sed -n -e'/\/\/START_GET_/,/\/\/END_GET_/p' src/types.cc | gcc -E -
#define GET_(f_name,from ,rt, k_type) \
an<rt> f_name( an<from> t) { \
if (t->type() == from::k_type) \
return std::dynamic_pointer_cast<rt> (t);\
return nullptr;\
int get_obj(lua_State *L_) {
if (an<T> t = LuaType<an<T>>::todata(L_, 1)) {
auto t_type = t->type();
if (T::kScalar == t_type) {
lua_pushcfunction(L_, WRAP(Get<V>));
}
else if (T::kList == t_type) {
lua_pushcfunction(L_, WRAP(Get<L>));
}
else if (T::kMap == t_type) {
lua_pushcfunction(L_, WRAP(Get<M>));
}
else {
return 0;
}
lua_pushvalue(L_, 1);
lua_call(L_, 1, 1);
return 1;
}
return 0;
}

GET_( get_value,T, V, kScalar );
GET_( get_list, T, L, kList );
GET_( get_map, T, M, kMap );

#undef GET_
//END_GET_

static const luaL_Reg funcs[] = {
{ NULL, NULL },
};

static const luaL_Reg methods[] = {
{"get_value",WRAP(get_value)},
{"get_list",WRAP(get_list)},
{"get_map",WRAP(get_map)},
{"get_value",WRAP(Get<V>)},
{"get_list",WRAP(Get<L>)},
{"get_map",WRAP(Get<M>)},
{"get_obj", get_obj},
{ NULL, NULL },
};

Expand Down

0 comments on commit 7c1b939

Please sign in to comment.