{"action":"created","issue":{"url":"https://api.github.com/repos/ThePhD/sol2/issues/472","repository_url":"https://api.github.com/repos/ThePhD/sol2","labels_url":"https://api.github.com/repos/ThePhD/sol2/issues/472/labels{/name}","comments_url":"https://api.github.com/repos/ThePhD/sol2/issues/472/comments","events_url":"https://api.github.com/repos/ThePhD/sol2/issues/472/events","html_url":"https://github.com/ThePhD/sol2/issues/472","id":248071539,"number":472,"title":"how to return self reference or dependency with usertype","user":{"login":"mrgreywater","id":2902403,"avatar_url":"https://avatars2.githubusercontent.com/u/2902403?v=4","gravatar_id":"","url":"https://api.github.com/users/mrgreywater","html_url":"https://github.com/mrgreywater","followers_url":"https://api.github.com/users/mrgreywater/followers","following_url":"https://api.github.com/users/mrgreywater/following{/other_user}","gists_url":"https://api.github.com/users/mrgreywater/gists{/gist_id}","starred_url":"https://api.github.com/users/mrgreywater/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mrgreywater/subscriptions","organizations_url":"https://api.github.com/users/mrgreywater/orgs","repos_url":"https://api.github.com/users/mrgreywater/repos","events_url":"https://api.github.com/users/mrgreywater/events{/privacy}","received_events_url":"https://api.github.com/users/mrgreywater/received_events","type":"User","site_admin":false},"labels":[{"id":338361628,"url":"https://api.github.com/repos/ThePhD/sol2/labels/Feature.CanDo","name":"Feature.CanDo","color":"006b75","default":false},{"id":383475508,"url":"https://api.github.com/repos/ThePhD/sol2/labels/Feature.Shiny","name":"Feature.Shiny","color":"1d76db","default":false}],"state":"open","locked":false,"assignee":{"login":"ThePhD","id":2886436,"avatar_url":"https://avatars0.githubusercontent.com/u/2886436?v=4","gravatar_id":"","url":"https://api.github.com/users/ThePhD","html_url":"https://github.com/ThePhD","followers_url":"https://api.github.com/users/ThePhD/followers","following_url":"https://api.github.com/users/ThePhD/following{/other_user}","gists_url":"https://api.github.com/users/ThePhD/gists{/gist_id}","starred_url":"https://api.github.com/users/ThePhD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ThePhD/subscriptions","organizations_url":"https://api.github.com/users/ThePhD/orgs","repos_url":"https://api.github.com/users/ThePhD/repos","events_url":"https://api.github.com/users/ThePhD/events{/privacy}","received_events_url":"https://api.github.com/users/ThePhD/received_events","type":"User","site_admin":false},"assignees":[{"login":"ThePhD","id":2886436,"avatar_url":"https://avatars0.githubusercontent.com/u/2886436?v=4","gravatar_id":"","url":"https://api.github.com/users/ThePhD","html_url":"https://github.com/ThePhD","followers_url":"https://api.github.com/users/ThePhD/followers","following_url":"https://api.github.com/users/ThePhD/following{/other_user}","gists_url":"https://api.github.com/users/ThePhD/gists{/gist_id}","starred_url":"https://api.github.com/users/ThePhD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ThePhD/subscriptions","organizations_url":"https://api.github.com/users/ThePhD/orgs","repos_url":"https://api.github.com/users/ThePhD/repos","events_url":"https://api.github.com/users/ThePhD/events{/privacy}","received_events_url":"https://api.github.com/users/ThePhD/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/ThePhD/sol2/milestones/11","html_url":"https://github.com/ThePhD/sol2/milestone/11","labels_url":"https://api.github.com/repos/ThePhD/sol2/milestones/11/labels","id":2138303,"number":11,"title":"Feature","description":"New features and tweaks.","creator":{"login":"ThePhD","id":2886436,"avatar_url":"https://avatars0.githubusercontent.com/u/2886436?v=4","gravatar_id":"","url":"https://api.github.com/users/ThePhD","html_url":"https://github.com/ThePhD","followers_url":"https://api.github.com/users/ThePhD/followers","following_url":"https://api.github.com/users/ThePhD/following{/other_user}","gists_url":"https://api.github.com/users/ThePhD/gists{/gist_id}","starred_url":"https://api.github.com/users/ThePhD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ThePhD/subscriptions","organizations_url":"https://api.github.com/users/ThePhD/orgs","repos_url":"https://api.github.com/users/ThePhD/repos","events_url":"https://api.github.com/users/ThePhD/events{/privacy}","received_events_url":"https://api.github.com/users/ThePhD/received_events","type":"User","site_admin":false},"open_issues":5,"closed_issues":16,"state":"open","created_at":"2016-11-16T04:26:51Z","updated_at":"2017-08-04T18:09:32Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2017-08-04T17:40:25Z","updated_at":"2017-08-05T10:52:40Z","closed_at":null,"body":"In luabind, you're able to return a reference to \"self\" from a usertype function. See http://www.rasterbar.com/products/luabind/docs.html#return-reference-to\r\n\r\nIn sol2, you seem to only be able to use sol::object as first argument in usertype methods, and manually parse the type, and finally return self as sol::object which is not really nice (if you return it as pointer, it won't be gc'd properly). It would be great if we could have sol::this_self, similar to sol::this_state, so you can simply return the reference of self.\r\n\r\nIn luabind you can also return a usertype member as reference with a dependency to the self instance:\r\nhttp://www.rasterbar.com/products/luabind/docs.html#dependency\r\nI would appreciate any help to get similar behaviour without wrapping all my members with std::shared_ptr.\r\n\r\nThese are just feature requests that would make the code cleaner to look at and you can workaround it, but it would be nice to have regardless :)"},"comment":{"url":"https://api.github.com/repos/ThePhD/sol2/issues/comments/320437047","html_url":"https://github.com/ThePhD/sol2/issues/472#issuecomment-320437047","issue_url":"https://api.github.com/repos/ThePhD/sol2/issues/472","id":320437047,"user":{"login":"mrgreywater","id":2902403,"avatar_url":"https://avatars2.githubusercontent.com/u/2902403?v=4","gravatar_id":"","url":"https://api.github.com/users/mrgreywater","html_url":"https://github.com/mrgreywater","followers_url":"https://api.github.com/users/mrgreywater/followers","following_url":"https://api.github.com/users/mrgreywater/following{/other_user}","gists_url":"https://api.github.com/users/mrgreywater/gists{/gist_id}","starred_url":"https://api.github.com/users/mrgreywater/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mrgreywater/subscriptions","organizations_url":"https://api.github.com/users/mrgreywater/orgs","repos_url":"https://api.github.com/users/mrgreywater/repos","events_url":"https://api.github.com/users/mrgreywater/events{/privacy}","received_events_url":"https://api.github.com/users/mrgreywater/received_events","type":"User","site_admin":false},"created_at":"2017-08-05T10:52:40Z","updated_at":"2017-08-05T10:52:40Z","body":"Another example where a dependency would be nice (without having to wrap unit::pos in a shared_ptr)\r\n```\r\nsol::state lua;\r\nlua.open_libraries();\r\nstruct vec2 { float x, y; };\r\nstruct unit { vec2 pos; };\r\nlua.new_usertype<vec2>(\"vec2\",\r\n    sol::call_constructor, sol::factories([]() {\r\n        return vec2{};\r\n    }),\r\n    \"x\", &vec2::x,\r\n    \"y\", &vec2::y\r\n);\r\nlua.new_usertype<unit>(\"unit\",\r\n    sol::call_constructor, sol::factories([](){\r\n        return unit{};\r\n    }),\r\n    \"pos\", sol::property([](unit& self)->vec2& {\r\n        return self.pos; //somehow return the pos as reference with dependency\r\n    }),\r\n    sol::meta_function::garbage_collect, sol::destructor([](unit& self){\r\n        //write bogus data, to show self is now invalid and points to free'd memory \r\n        self.pos.x = std::numeric_limits<float>::quiet_NaN();\r\n        self.pos.y = std::numeric_limits<float>::quiet_NaN();\r\n    })\r\n);\r\nsol::protected_function_result result = lua.do_string(\r\n    \"local u = unit()\"                                                              \"\\r\\n\"\r\n    \"local pos = u.pos\"                                                             \"\\r\\n\"\r\n    \"u.pos.x, u.pos.y = 1, 0\"                                                       \"\\r\\n\"\r\n    \"assert(u.pos.x == 1 and u.pos.y == 0)\"                                         \"\\r\\n\"\r\n    \"assert(pos.x == 1 and pos.y == 0)\"                                             \"\\r\\n\"\r\n    \"u = nil; collectgarbage()\"                                                     \"\\r\\n\"\r\n    \"assert(pos.x == 1 and pos.y == 0)\"                                             \"\\r\\n\" //test if reference still points to valid mem. will fail (may cause segfault/access violation like this)\r\n);\r\nif (result.valid()) {\r\n    std::cout << \"Success\" << std::endl;\r\n} else {\r\n    sol::error e = result;\r\n    std::cout << \"Fail: \" << e.what() << std::endl;\r\n}\r\n```"}}
{"actor":{"display_login":"mrgreywater"}}