{"action":"created","issue":{"url":"https://api.github.com/repos/pybind/pybind11/issues/1320","repository_url":"https://api.github.com/repos/pybind/pybind11","labels_url":"https://api.github.com/repos/pybind/pybind11/issues/1320/labels{/name}","comments_url":"https://api.github.com/repos/pybind/pybind11/issues/1320/comments","events_url":"https://api.github.com/repos/pybind/pybind11/issues/1320/events","html_url":"https://github.com/pybind/pybind11/issues/1320","id":304492645,"number":1320,"title":"How to pass a shared pointer by reference to c++","user":{"login":"RiccardoRossi","id":8059665,"avatar_url":"https://avatars1.githubusercontent.com/u/8059665?v=4","gravatar_id":"","url":"https://api.github.com/users/RiccardoRossi","html_url":"https://github.com/RiccardoRossi","followers_url":"https://api.github.com/users/RiccardoRossi/followers","following_url":"https://api.github.com/users/RiccardoRossi/following{/other_user}","gists_url":"https://api.github.com/users/RiccardoRossi/gists{/gist_id}","starred_url":"https://api.github.com/users/RiccardoRossi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RiccardoRossi/subscriptions","organizations_url":"https://api.github.com/users/RiccardoRossi/orgs","repos_url":"https://api.github.com/users/RiccardoRossi/repos","events_url":"https://api.github.com/users/RiccardoRossi/events{/privacy}","received_events_url":"https://api.github.com/users/RiccardoRossi/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-03-12T18:38:34Z","updated_at":"2018-03-13T07:45:15Z","closed_at":null,"author_association":"NONE","body":"Dear list,\r\n    i am cross posting here a problem that i consulted on gitter, so to be able to post some code. \r\n\r\n@yeganer is proposing me to creating an auxiliary class containing the pointer, but i would love to avoid it as possible\r\n\r\nMy situation is the following: i have a linear algebra library (Trilinos Epetra) that provides some distributed vector implementation. I need to implement a resize method, that is not implemented by that library. To do this, i am passing around pointers to my matrices, and when i want to resize, i allocate a new vector and i swap the pointer.\r\nTo do this i need to change the shared_ptr itself and not just its content.\r\n\r\nso i have two types (exported as distinct types in pybind11)\r\n\r\n      Vector\r\n      std::shared_ptr<Vector> \r\n\r\nand i define the functions (pseudo code, but it should be pretty close)\r\n\r\n      std::shared_ptr<Vector> CreateEmptyVectorPointer() \r\n      { return std::make_shared<Vector>(0); }  \r\n\r\n      Resize(std::shared_ptr<Vector>& self, int n)\r\n      {\r\n          auto pnew = std::make_shared<Vector>(n);\r\n          self.swap(pnew);\r\n      }\r\n\r\n     Matrix& GetReference(std::shared_ptr<Vector>& self)\r\n     {\r\n        return *self;\r\n     }\r\n\r\nthen i would like to call it from python as something like\r\n\r\n    def test_resize(self):\r\n        comm = KratosMultiphysics.TrilinosApplication.CreateCommunicator()\r\n        space = KratosMultiphysics.TrilinosApplication.TrilinosSparseSpace()\r\n        \r\n        print(dir(space))\r\n        print(space)\r\n        pb = space.CreateEmptyVectorPointer(comm)\r\n        print(\"000\")\r\n        space.ResizeVector(pb,2)\r\n        print(\"111\")\r\n        rb = pb.GetReference()\r\n        print(\"222\")\r\n        n = space.Size(rb)\r\n        self.assertEqual(n,0)\r\n\r\nthe actual code in which i try to do the export is here: (ported from a working boost::python code)\r\n\r\nhttps://github.com/KratosMultiphysics/Kratos/pull/1513/files#diff-0924c5681634f0687c1b74c47ff15f71R279\r\n\r\nthanks in advance for any help on this\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/pybind/pybind11/issues/comments/372575076","html_url":"https://github.com/pybind/pybind11/issues/1320#issuecomment-372575076","issue_url":"https://api.github.com/repos/pybind/pybind11/issues/1320","id":372575076,"user":{"login":"yeganer","id":6164110,"avatar_url":"https://avatars3.githubusercontent.com/u/6164110?v=4","gravatar_id":"","url":"https://api.github.com/users/yeganer","html_url":"https://github.com/yeganer","followers_url":"https://api.github.com/users/yeganer/followers","following_url":"https://api.github.com/users/yeganer/following{/other_user}","gists_url":"https://api.github.com/users/yeganer/gists{/gist_id}","starred_url":"https://api.github.com/users/yeganer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yeganer/subscriptions","organizations_url":"https://api.github.com/users/yeganer/orgs","repos_url":"https://api.github.com/users/yeganer/repos","events_url":"https://api.github.com/users/yeganer/events{/privacy}","received_events_url":"https://api.github.com/users/yeganer/received_events","type":"User","site_admin":false},"created_at":"2018-03-13T07:45:15Z","updated_at":"2018-03-13T07:45:15Z","author_association":"NONE","body":"@jagerman Could I use something similar to workaround #1278 ?"}}
{"actor":{"display_login":"yeganer"}}