2014-11-28 06:13:18 -05:00
|
|
|
#ifndef SHARED_MEMORY_VECTOR_WRAPPER_HPP
|
|
|
|
#define SHARED_MEMORY_VECTOR_WRAPPER_HPP
|
2013-09-17 08:23:06 -04:00
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
2013-09-23 12:01:30 -04:00
|
|
|
#include <algorithm>
|
2013-09-17 08:23:06 -04:00
|
|
|
#include <iterator>
|
2014-05-07 12:39:16 -04:00
|
|
|
#include <type_traits>
|
2013-09-17 08:23:06 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
template <typename DataT> class ShMemIterator : public std::iterator<std::input_iterator_tag, DataT>
|
|
|
|
{
|
|
|
|
DataT *p;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ShMemIterator(DataT *x) : p(x) {}
|
|
|
|
ShMemIterator(const ShMemIterator &mit) : p(mit.p) {}
|
|
|
|
ShMemIterator &operator++()
|
|
|
|
{
|
2013-09-17 08:23:06 -04:00
|
|
|
++p;
|
|
|
|
return *this;
|
|
|
|
}
|
2014-05-07 12:39:16 -04:00
|
|
|
ShMemIterator operator++(int)
|
|
|
|
{
|
2013-09-17 08:23:06 -04:00
|
|
|
ShMemIterator tmp(*this);
|
|
|
|
operator++();
|
|
|
|
return tmp;
|
|
|
|
}
|
2014-05-07 12:39:16 -04:00
|
|
|
ShMemIterator operator+(std::ptrdiff_t diff)
|
|
|
|
{
|
|
|
|
ShMemIterator tmp(p + diff);
|
2013-09-24 09:10:06 -04:00
|
|
|
return tmp;
|
|
|
|
}
|
2014-05-07 12:39:16 -04:00
|
|
|
bool operator==(const ShMemIterator &rhs) { return p == rhs.p; }
|
|
|
|
bool operator!=(const ShMemIterator &rhs) { return p != rhs.p; }
|
|
|
|
DataT &operator*() { return *p; }
|
2013-09-17 08:23:06 -04:00
|
|
|
};
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
template <typename DataT> class SharedMemoryWrapper
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
DataT *m_ptr;
|
2013-09-17 08:23:06 -04:00
|
|
|
std::size_t m_size;
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
public:
|
|
|
|
SharedMemoryWrapper() : m_ptr(nullptr), m_size(0) {}
|
2013-09-23 12:01:30 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
SharedMemoryWrapper(DataT *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {}
|
2013-09-17 08:23:06 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
void swap(SharedMemoryWrapper<DataT> &other)
|
|
|
|
{
|
2014-10-09 11:26:15 -04:00
|
|
|
// BOOST_ASSERT_MSG(m_size != 0 || other.size() != 0, "size invalid");
|
2014-05-07 12:39:16 -04:00
|
|
|
std::swap(m_size, other.m_size);
|
|
|
|
std::swap(m_ptr, other.m_ptr);
|
2013-09-23 12:01:30 -04:00
|
|
|
}
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
DataT &at(const std::size_t index) { return m_ptr[index]; }
|
2013-09-23 12:01:30 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
const DataT &at(const std::size_t index) const { return m_ptr[index]; }
|
2013-09-23 07:30:21 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
ShMemIterator<DataT> begin() const { return ShMemIterator<DataT>(m_ptr); }
|
2013-09-23 12:01:30 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
ShMemIterator<DataT> end() const { return ShMemIterator<DataT>(m_ptr + m_size); }
|
2013-09-17 08:23:06 -04:00
|
|
|
|
|
|
|
std::size_t size() const { return m_size; }
|
|
|
|
|
2014-02-21 10:55:41 -05:00
|
|
|
bool empty() const { return 0 == size(); }
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
DataT &operator[](const unsigned index)
|
|
|
|
{
|
2013-09-17 08:23:06 -04:00
|
|
|
BOOST_ASSERT_MSG(index < m_size, "invalid size");
|
|
|
|
return m_ptr[index];
|
|
|
|
}
|
2013-09-23 12:01:30 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
const DataT &operator[](const unsigned index) const
|
|
|
|
{
|
2013-09-23 12:01:30 -04:00
|
|
|
BOOST_ASSERT_MSG(index < m_size, "invalid size");
|
|
|
|
return m_ptr[index];
|
|
|
|
}
|
2013-09-17 08:23:06 -04:00
|
|
|
};
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
template <> class SharedMemoryWrapper<bool>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
unsigned *m_ptr;
|
2014-04-11 14:03:09 -04:00
|
|
|
std::size_t m_size;
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
public:
|
|
|
|
SharedMemoryWrapper() : m_ptr(nullptr), m_size(0) {}
|
2014-04-11 14:03:09 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
SharedMemoryWrapper(unsigned *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {}
|
2014-04-11 14:03:09 -04:00
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
void swap(SharedMemoryWrapper<bool> &other)
|
|
|
|
{
|
2014-10-09 11:26:15 -04:00
|
|
|
// BOOST_ASSERT_MSG(m_size != 0 || other.size() != 0, "size invalid");
|
2014-05-07 12:39:16 -04:00
|
|
|
std::swap(m_size, other.m_size);
|
|
|
|
std::swap(m_ptr, other.m_ptr);
|
2014-04-11 14:03:09 -04:00
|
|
|
}
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
bool at(const std::size_t index) const
|
|
|
|
{
|
2014-06-17 09:30:28 -04:00
|
|
|
const std::size_t bucket = index / 32;
|
|
|
|
const unsigned offset = static_cast<unsigned>(index % 32);
|
2014-04-11 14:03:09 -04:00
|
|
|
return m_ptr[bucket] & (1 << offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const { return m_size; }
|
|
|
|
|
|
|
|
bool empty() const { return 0 == size(); }
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
bool operator[](const unsigned index)
|
|
|
|
{
|
2014-04-11 14:03:09 -04:00
|
|
|
BOOST_ASSERT_MSG(index < m_size, "invalid size");
|
|
|
|
const unsigned bucket = index / 32;
|
|
|
|
const unsigned offset = index % 32;
|
|
|
|
return m_ptr[bucket] & (1 << offset);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-07 12:39:16 -04:00
|
|
|
template <typename DataT, bool UseSharedMemory> struct ShM
|
|
|
|
{
|
2014-08-19 07:01:38 -04:00
|
|
|
using vector = typename std::conditional<UseSharedMemory,
|
2015-02-19 03:19:51 -05:00
|
|
|
SharedMemoryWrapper<DataT>,
|
|
|
|
std::vector<DataT>>::type;
|
2014-05-07 12:39:16 -04:00
|
|
|
};
|
2013-09-17 08:23:06 -04:00
|
|
|
|
2014-11-28 06:13:18 -05:00
|
|
|
#endif // SHARED_MEMORY_VECTOR_WRAPPER_HPP
|