forked from organicmaps/organicmaps
Add C-style array adapters for reading and writing.
This commit is contained in:
parent
d331a08bbc
commit
733a9f0a4e
2 changed files with 70 additions and 1 deletions
68
base/array_adapters.h
Normal file
68
base/array_adapters.h
Normal file
|
@ -0,0 +1,68 @@
|
|||
#pragma once
|
||||
#include "assert.hpp"
|
||||
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class T, class TPtr> class array_impl
|
||||
{
|
||||
protected:
|
||||
TPtr m_p;
|
||||
size_t m_size;
|
||||
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T const & const_reference;
|
||||
typedef T & reference;
|
||||
|
||||
array_impl(TPtr p, size_t sz) : m_p(p), m_size(sz) {}
|
||||
|
||||
T const & operator[](size_t i) const
|
||||
{
|
||||
ASSERT_LESS ( i, m_size, () );
|
||||
return m_p[i];
|
||||
}
|
||||
|
||||
T const & back() const
|
||||
{
|
||||
ASSERT_GREATER ( m_size, 0, () );
|
||||
return m_p[m_size-1];
|
||||
}
|
||||
|
||||
size_t size() const { return m_size; }
|
||||
|
||||
bool empty() const { return (m_size == 0); }
|
||||
};
|
||||
}
|
||||
|
||||
template <class T> class array_read : public detail::array_impl<T, T const *>
|
||||
{
|
||||
public:
|
||||
array_read(T const * p, size_t sz)
|
||||
: detail::array_impl<T, T const *>(p, sz)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <class TCont>
|
||||
array_read<typename TCont::value_type> make_read_adapter(TCont const & cont)
|
||||
{
|
||||
return array_read<typename TCont::value_type>(cont.empty() ? 0 : &cont[0], cont.size());
|
||||
}
|
||||
|
||||
template <class T> class array_write : public detail::array_impl<T, T *>
|
||||
{
|
||||
size_t m_capacity;
|
||||
|
||||
public:
|
||||
template <class TCont> explicit array_write(TCont & cont)
|
||||
: detail::array_impl<T, T *>(cont.empty() ? 0 : &cont[0], 0), m_capacity(cont.size())
|
||||
{
|
||||
}
|
||||
|
||||
void push_back(T const & t)
|
||||
{
|
||||
ASSERT_LESS ( m_size, m_capacity, () );
|
||||
m_p[m_size++] = t;
|
||||
}
|
||||
};
|
|
@ -59,4 +59,5 @@ HEADERS += \
|
|||
shared_buffer_manager.hpp \
|
||||
memory_mapped_file.hpp \
|
||||
buffer_vector.hpp \
|
||||
path_utils.hpp
|
||||
path_utils.hpp \
|
||||
array_adapters.hpp \
|
||||
|
|
Loading…
Add table
Reference in a new issue