[odb-users] Wrapping existing members with std::atomic<>

Andrew Cunningham odb at a-cunningham.com
Tue Jul 18 13:53:19 EDT 2017


Hi Boris,
Thanks for the useful and comprehensive answer - I certainly learned
something  about #pragma db map..

Andrew

On Sun, Jul 16, 2017 at 9:03 AM, Boris Kolpackov <boris at codesynthesis.com>
wrote:

> Andrew Cunningham <odb at a-cunningham.com> writes:
>
> > I already have defined a wrapper for my 'own' smart pointer, so am pretty
> > clear about the process. I was hoping for something simpler - I think my
> > solution is to use a transient atomic and copy that to/from a persistent
> > field "pre persist" and "postload."
>
> You could use a virtual data member then:
>
> class foo
> {
>
>   #pragma db transient
>   std::atomic<std::uint64_t> count;
>
>   #pragma db member(count_) virtual(std::uint64_t)         \
>     get(this.count.load (std::memory_order_relaxed))       \
>     set(this.count.store ((?), std::memory_order_relaxed))
>
> };
>
> Alternatively, if you have several such members, you can map std::atomic
> along these lines:
>
> using atomic_uint64_t = std::atomic<std::uint64_t>;
>
> #pragma db map type(atomic_uint64_t) as(std::uint64_t) \
>   to((?).load (std::memory_order_relaxed))             \
>   from(atomic_uint64_t (?))
>
> Then in your classes they can be used as any other members:
>
> class foo
> {
>   std::atomic<std::uint64_t> count1;
>   std::atomic<std::uint64_t> count2;
> }
>
> Boris
>


More information about the odb-users mailing list