YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Passing an std::shared_ptr<T>
matty
#1 Posted : Thursday, July 20, 2017 5:01:56 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 6/12/2017(UTC)
Posts: 8

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I've been trying to pass an std::shared_ptr to Squirrel, but I'm not exactly sure if my method of sending the shared_ptr is wrong, reading it back from Squirrel, or if I'm going in the completely wrong direction with it.

Code:


// send acorn::Data to Squirrel
SQInteger wrap_newData(SQVM* v) {
    auto instance = newData(); // returns std::shared_ptr<Data>
    auto ret = [&]() -> int {
        sq_pushstring(v, "Data", -1);
        if (SQ_FAILED(sq_get(v, -2))) {
            return sq_throwerror(v, "Couldn't resolve squirrel type 'Data'");
        }

        // im not sure if using &instance is correct...
        if (SQ_FAILED(sq_createinstance(v, -1)) || SQ_FAILED(sq_setinstanceup(v, -1, &instance))) {
            return sq_throwerror(v, "Couldn't setup squirrel instance for object of type 'Data'");
        }

        sq_setreleasehook(v, 1, release_hook<std::shared_ptr<acorn::Data>>);
        sq_remove(v, -2);

        return 1;
    };

    return ret();
}

// receive acorn::Data from squirrel
SQInteger wrap_dataTest(SQVM* v) {
    SQUserPointer ptr;
    if (SQ_FAILED(sq_getinstanceup(v, -1, &ptr, (SQUserPointer) acorn::TYPE_DATA))) {
        return sq_throwerror(v, "insert error string here");
    }

    auto ret = [&]() -> int {
        auto instance = reinterpret_cast<std::shared_ptr<acorn::Data>*>(ptr)->get();
        instance->foo(5); // unexpected behavior;
        return 0;
    };

    return ret();
}



I've also been reading around that using sq_setclassudsize() would be more efficient for this, but I haven't found any good examples of how to use it.

Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.075 seconds.