

// LOVE
#include "common/object.h"

namespace demoloop
{

Object::Object()
  : count(1)
{
}

Object::Object(const Object & /*other*/)
  : count(1) // Always start with a reference count of 1.
{
}

Object::~Object()
{
}

int Object::getReferenceCount() const
{
  return count;
}

void Object::retain()
{
  count.fetch_add(1, std::memory_order_relaxed);
}

void Object::release()
{
  // http://www.boost.org/doc/libs/1_56_0/doc/html/atomic/usage_examples.html
  if (count.fetch_sub(1, std::memory_order_release) == 1)
  {
    std::atomic_thread_fence(std::memory_order_acquire);
    delete this;
  }
}

} // love
