#ifndef SHARED_MEMORY_VECTOR_WRAPPER_HPP #define SHARED_MEMORY_VECTOR_WRAPPER_HPP #include #include #include #include #include template class ShMemIterator : public std::iterator { DataT *p; public: explicit ShMemIterator(DataT *x) : p(x) {} ShMemIterator(const ShMemIterator &mit) : p(mit.p) {} ShMemIterator &operator++() { ++p; return *this; } ShMemIterator operator++(int) { ShMemIterator tmp(*this); operator++(); return tmp; } ShMemIterator operator+(std::ptrdiff_t diff) { ShMemIterator tmp(p + diff); return tmp; } bool operator==(const ShMemIterator &rhs) { return p == rhs.p; } bool operator!=(const ShMemIterator &rhs) { return p != rhs.p; } DataT &operator*() { return *p; } }; template class SharedMemoryWrapper { private: DataT *m_ptr; std::size_t m_size; public: SharedMemoryWrapper() : m_ptr(nullptr), m_size(0) {} SharedMemoryWrapper(DataT *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {} void swap(SharedMemoryWrapper &other) { // BOOST_ASSERT_MSG(m_size != 0 || other.size() != 0, "size invalid"); std::swap(m_size, other.m_size); std::swap(m_ptr, other.m_ptr); } DataT &at(const std::size_t index) { return m_ptr[index]; } const DataT &at(const std::size_t index) const { return m_ptr[index]; } ShMemIterator begin() const { return ShMemIterator(m_ptr); } ShMemIterator end() const { return ShMemIterator(m_ptr + m_size); } std::size_t size() const { return m_size; } bool empty() const { return 0 == size(); } DataT &operator[](const unsigned index) { BOOST_ASSERT_MSG(index < m_size, "invalid size"); return m_ptr[index]; } const DataT &operator[](const unsigned index) const { BOOST_ASSERT_MSG(index < m_size, "invalid size"); return m_ptr[index]; } }; template <> class SharedMemoryWrapper { private: unsigned *m_ptr; std::size_t m_size; public: SharedMemoryWrapper() : m_ptr(nullptr), m_size(0) {} SharedMemoryWrapper(unsigned *ptr, std::size_t size) : m_ptr(ptr), m_size(size) {} void swap(SharedMemoryWrapper &other) { // BOOST_ASSERT_MSG(m_size != 0 || other.size() != 0, "size invalid"); std::swap(m_size, other.m_size); std::swap(m_ptr, other.m_ptr); } bool at(const std::size_t index) const { const std::size_t bucket = index / 32; const unsigned offset = static_cast(index % 32); return m_ptr[bucket] & (1 << offset); } std::size_t size() const { return m_size; } bool empty() const { return 0 == size(); } bool operator[](const unsigned index) { BOOST_ASSERT_MSG(index < m_size, "invalid size"); const unsigned bucket = index / 32; const unsigned offset = index % 32; return m_ptr[bucket] & (1 << offset); } }; template struct ShM { using vector = typename std::conditional, std::vector>::type; }; #endif // SHARED_MEMORY_VECTOR_WRAPPER_HPP