Skip to content

Commit

Permalink
Merge pull request #90 from amorozov/strings_not_char_pointers
Browse files Browse the repository at this point in the history
Replace (some of) `const char*' with `const std::string&' in methods arguments
  • Loading branch information
jeremyong committed Sep 2, 2015
2 parents bec4bc2 + 91ce6b0 commit 15e2c48
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions include/selene/Selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,16 @@ class Selector {
: _state(s), _registry(r), _name(name), _traversal(traversal),
_get(get), _put(put) {}

Selector(lua_State *s, Registry &r, const char *name)
Selector(lua_State *s, Registry &r, const std::string& name)
: _state(s), _registry(r), _name(name) {
// lambda's lifetime maybe longer than lifetime of `name'
const std::string nCopy{name};
const auto state = _state; // gcc-5.1 doesn't support implicit member capturing
_get = [state, nCopy]() {
lua_getglobal(state, nCopy.c_str());
// `name' is passed by value because lambda's lifetime may be longer than lifetime of `name'
_get = [state, name]() {
lua_getglobal(state, name.c_str());
};
_put = [state, nCopy](Fun fun) {
_put = [state, name](Fun fun) {
fun();
lua_setglobal(state, nCopy.c_str());
lua_setglobal(state, name.c_str());
};
}

Expand Down Expand Up @@ -381,23 +380,25 @@ class Selector {
// Chaining operators. If the selector is an rvalue, modify in
// place. Otherwise, create a new Selector and return it.
#ifdef HAS_REF_QUALIFIERS
Selector&& operator[](const char *name) && {
Selector&& operator[](const std::string& name) && {
_name += std::string(".") + name;
_check_create_table();
_traversal.push_back(_get);
// explicitly copy `name' because lambda lifetime may be longer
const std::string nCopy{name};
const auto state = _state; // gcc-5.1 doesn't support implicit member capturing
_get = [state, nCopy]() {
lua_getfield(state, -1, nCopy.c_str());
// `name' is passed by value because lambda lifetime may be longer than `name'
_get = [state, name]() {
lua_getfield(state, -1, name.c_str());
};
_put = [state, nCopy](Fun fun) {
_put = [state, name](Fun fun) {
fun();
lua_setfield(state, -2, nCopy.c_str());
lua_setfield(state, -2, name.c_str());
lua_pop(state, 1);
};
return std::move(*this);
}
Selector&& operator[](const char* name) && {
return std::move(*this)[std::string{name}];
}
Selector&& operator[](const int index) && {
_name += std::string(".") + std::to_string(index);
_check_create_table();
Expand All @@ -416,24 +417,26 @@ class Selector {
return std::move(*this);
}
#endif // HAS_REF_QUALIFIERS
Selector operator[](const char *name) const REF_QUAL_LVALUE {
Selector operator[](const std::string& name) const REF_QUAL_LVALUE {
auto n = _name + "." + name;
_check_create_table();
auto traversal = _traversal;
traversal.push_back(_get);
// explicitly copy `name' because lambda lifetime may be longer
const std::string nCopy{name};
const auto state = _state; // gcc-5.1 doesn't support implicit member capturing
Fun get = [state, nCopy]() {
lua_getfield(state, -1, nCopy.c_str());
// `name' is passed by value because lambda lifetime may be longer than `name'
Fun get = [state, name]() {
lua_getfield(state, -1, name.c_str());
};
PFun put = [state, nCopy](Fun fun) {
PFun put = [state, name](Fun fun) {
fun();
lua_setfield(state, -2, nCopy.c_str());
lua_setfield(state, -2, name.c_str());
lua_pop(state, 1);
};
return Selector{_state, _registry, n, traversal, get, put};
}
Selector operator[](const char* name) const REF_QUAL_LVALUE {
return (*this)[std::string{name}];
}
Selector operator[](const int index) const REF_QUAL_LVALUE {
auto name = _name + "." + std::to_string(index);
_check_create_table();
Expand Down

0 comments on commit 15e2c48

Please sign in to comment.