[odb-users] How to use the lazy_weak_ptr?

黄奕 ruffian_hy at 126.com
Sat Nov 16 01:05:18 EST 2019


Actually, we have class Group and class Post, Group and Post is one to many relationship.
But we only need a most recent Post ptr for Group object.
Currently, we have odb::lazy_shared_ptr for each object, like below:
class Group
{
odb::lazy_shared_ptr<Post> mostRecentPost;
};
class Post
{
odb::lazy_shared_ptr<Group> group;
}
How can I break the ownership cycles for this scenario,  and prevent db upgrade incompatibility issues.



At 2019-11-16 13:30:08, "黄奕" <ruffian_hy at 126.com> wrote:

I found that I can not use inverse in our project, 
But if I remove  “#pragma db inverse(m_b)”, all the function below will get a nullptr
if(m_a.loaded())
auto post = m_a.get_eager().lock();
        //auto post1=m_a.get_eager();
        auto post2 = m_a.load();
        auto post3 = m_a.lock();
        auto post4 = post3.load();
        auto post5 = post3.get_eager();
}


How can I get the object from lazy_weak_ptr?


At 2019-11-16 02:52:50, "Boris Kolpackov" <boris at codesynthesis.com> wrote:
>黄奕 <ruffian_hy at 126.com> writes:
>
>> class B
>> {
>> ...
>> private:
>> #pragma db inverse(m_b)
>> 
>> odb::lazy_weak_ptr<A> m_a;
>> ...
>> };
>> #pragma db object(B) pointer(std::shared_ptr)
>> #pragma db member(m_a)
>>  
>> But I get the error of “error: object data member ‘ m_a' specified in db pragma column is inverse”.
>
>Try to move inverse to the member(m_a) pragma:
>
>#pragma db member(m_a) inverse(m_b)
>
>Also, dealing with circular relationship is tricky, especially when
>spread over multiple headers; see "Circular Relationships" in the
>manual for details:
>
>https://codesynthesis.com/products/odb/doc/manual.xhtml#6.3





 





 


More information about the odb-users mailing list