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

Boris Kolpackov boris at codesynthesis.com
Sun Jul 16 12:03:44 EDT 2017


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