Skip to content

Commit bef937c

Browse files
authored
Merge pull request #52 from peacalm/dev
refactor luaw::push<Hint>, push the value as Hint expected
2 parents 98e6cdd + 41f1ead commit bef937c

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

include/peacalm/luaw.h

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,16 +1088,50 @@ class luaw {
10881088
return pusher<std::decay_t<T>>::push(*this, std::forward<T>(value));
10891089
}
10901090

1091-
/// Push with an user given hint type.
1091+
/**
1092+
* @brief Push with an user given hint type.
1093+
*
1094+
* Hint specifies what type of value we want to push into Lua.
1095+
*
1096+
* If Hint is luaw::function_tag, push the value as a funcion.
1097+
*
1098+
* If Hint is luaw::class_tag, push the value as an userdata. And the value
1099+
* must be a class or a raw/smart pointer to class.
1100+
*
1101+
* The origin value must be convertible to Hint.
1102+
* So, Hint Can't be reference.
1103+
*
1104+
* Hint could have cv- property.
1105+
*
1106+
* @tparam Hint Expected type to be pushed as.
1107+
* @tparam T Origin type of value.
1108+
* @param value The value to be pushed.
1109+
* @return int
1110+
*/
10921111
template <typename Hint, typename T>
10931112
std::enable_if_t<!std::is_same<Hint, T>::value, int> push(T&& value) {
1094-
static_assert(
1095-
std::is_same<Hint,
1096-
std::remove_cv_t<std::remove_reference_t<Hint>>>::value,
1097-
"Hint should not be const/volatile/reference");
1113+
static_assert(!std::is_reference<Hint>::value,
1114+
"Hint should not be reference");
1115+
return __push<Hint, T>(std::forward<T>(value), 0);
1116+
}
1117+
1118+
private:
1119+
// Firstly, try pusher<DecayHint>::template push<TargetT, Y>.
1120+
template <typename Hint, typename T>
1121+
auto __push(T&& value, int)
1122+
-> decltype(pusher<std::decay_t<Hint>>::template push<Hint, T>(
1123+
*this, std::forward<T>(value))) {
1124+
return pusher<std::decay_t<Hint>>::template push<Hint, T>(
1125+
*this, std::forward<T>(value));
1126+
}
1127+
1128+
// Secondly, try pusher<DecayHint>::push.
1129+
template <typename Hint, typename T>
1130+
auto __push(T&& value, ...) -> decltype(auto) {
10981131
return pusher<std::decay_t<Hint>>::push(*this, std::forward<T>(value));
10991132
}
11001133

1134+
public:
11011135
/// Copy and push a value already in the stack.
11021136
void pushvalue(int idx = -1) { lua_pushvalue(L_, idx); }
11031137

0 commit comments

Comments
 (0)