// $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/util/threadsafe_vector.cpp 3471 2020-09-21 14:18:40Z depa $

#include "threadsafe_vector.h"

// Partial specialization
template <>
bool threadsafe_vector<IProperty>::try_wait_and_compare(const IProperty& _inValue, IProperty& _outValue)
{
  bool res = false;
  boost::chrono::duration<long, boost::ratio<1> > waitDuration(14);
  boost::unique_lock<boost::mutex> lock(m_mut);
  // m_dataCond.wait(lock, boost::bind(&threadsafe_vector::wait_predicate, this));
  m_dataCond.wait_for(lock, waitDuration);
  _outValue = boost::move(m_dataVector.back());
  if (_inValue.compareProperty(_outValue))
    {
      m_dataVector.pop_back();
      res = true;
    }
  else
    {
      for (BOOST_AUTO(it, m_dataVector.begin()); it < (m_dataVector.end()-1); ++it)
        {
          _outValue = boost::move(*it);
          if (_inValue.compareProperty(_outValue))
            {
              m_dataVector.erase(it);
              res = true;
              break;
            }
        }
      if (!res)
        {
          _outValue.clear();
          m_dataCond.notify_one();
        }
    }
  return res;
}
