Kaydet (Commit) a0032a2d authored tarafından Noel Grandin's avatar Noel Grandin

improve o3tl::enumarray const-ness

Change-Id: I7b0d10b024edf604a7dea0e3b1399073f4bcba92
Reviewed-on: https://gerrit.libreoffice.org/43773Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 6ff4688b
......@@ -28,6 +28,8 @@ namespace o3tl {
template<typename EA>
class enumarray_iterator;
template<typename EA>
class enumarray_const_iterator;
///
/// This is a container convenience class for arrays indexed by enum values.
......@@ -45,6 +47,7 @@ class enumarray final
public:
typedef enumarray<E, V> self_type;
typedef enumarray_iterator<self_type> iterator;
typedef enumarray_const_iterator<self_type> const_iterator;
typedef V value_type;
typedef E key_type;
......@@ -67,11 +70,13 @@ public:
void fill(V val)
{ for (size_type i=0; i<=max_index; ++i) detail_values[i] = val; }
static size_type size() { return max_index + 1; }
iterator begin() { return iterator(*this, 0); }
iterator end() { return iterator(*this, size()); }
static size_type size() { return max_index + 1; }
iterator begin() { return iterator(*this, 0); }
iterator end() { return iterator(*this, size()); }
const_iterator begin() const { return const_iterator(*this, 0); }
const_iterator end() const { return const_iterator(*this, size()); }
V* data() { return detail_values; }
V* data() { return detail_values; }
//private:
V detail_values[max_index + 1];
......@@ -80,7 +85,7 @@ public:
template<typename EA>
class enumarray_iterator {
EA *m_buf;
EA& m_buf;
size_t m_pos;
public:
typedef enumarray_iterator<EA> self_type;
......@@ -95,12 +100,41 @@ public:
typedef typename EA::value_type& reference;
enumarray_iterator(EA& b, size_t start_pos)
: m_buf(&b), m_pos(start_pos) {}
value_type &operator*() { return (*m_buf)[static_cast<key_type>(m_pos)]; }
value_type *operator->() { return &(operator*()); }
self_type &operator++() { ++m_pos; return *this; }
bool operator!=(const self_type& other) const { return m_buf != other.m_buf || m_pos != other.m_pos; }
bool operator==(const self_type& other) const { return m_buf == other.m_buf && m_pos == other.m_pos; }
: m_buf(b), m_pos(start_pos) {}
value_type& operator*() const { return m_buf[static_cast<key_type>(m_pos)]; }
value_type* operator->() const { return &(operator*()); }
self_type& operator++() { ++m_pos; return *this; }
bool operator!=(self_type const & other) const { return &m_buf != &other.m_buf || m_pos != other.m_pos; }
bool operator==(self_type const & other) const { return &m_buf == &other.m_buf && m_pos == other.m_pos; }
enumarray_iterator&
operator=(self_type const & other) { m_buf = other.m_buf; m_pos = other.m_pos; return *this; }
};
template<typename EA>
class enumarray_const_iterator {
EA const & m_buf;
size_t m_pos;
public:
typedef enumarray_const_iterator<EA> self_type;
typedef typename EA::value_type const value_type;
typedef typename EA::key_type key_type;
typedef std::bidirectional_iterator_tag iterator_category; //should be random access, but that would require define subtraction operators on the enums
typedef
typename std::make_signed<
typename std::underlying_type<typename EA::key_type>::type>::type
difference_type;
typedef typename EA::value_type const * pointer;
typedef typename EA::value_type const & reference;
enumarray_const_iterator(EA const & b, size_t start_pos)
: m_buf(b), m_pos(start_pos) {}
value_type& operator*() const { return m_buf[static_cast<key_type>(m_pos)]; }
value_type* operator->() const { return &(operator*()); }
self_type& operator++() { ++m_pos; return *this; }
bool operator!=(self_type const & other) const { return &m_buf != &other.m_buf || m_pos != other.m_pos; }
bool operator==(self_type const & other) const { return &m_buf == &other.m_buf && m_pos == other.m_pos; }
enumarray_const_iterator&
operator=(self_type const & other) { m_buf = other.m_buf; m_pos = other.m_pos; return *this; }
};
}; // namespace o3tl
......
......@@ -29,6 +29,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,o3tl_tests,\
o3tl/qa/cow_wrapper_clients \
o3tl/qa/test-array_view \
o3tl/qa/test-cow_wrapper \
o3tl/qa/test-enumarray \
o3tl/qa/test-lru_map \
o3tl/qa/test-sorted_vector \
o3tl/qa/test-string_view \
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <stdexcept>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <o3tl/enumarray.hxx>
namespace {
class Test: public CppUnit::TestFixture {
private:
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testOperations);
CPPUNIT_TEST_SUITE_END();
void testOperations() {
enum class MyEnum { ONE, TWO, THREE, LAST = THREE };
o3tl::enumarray<MyEnum, int> aModules;
aModules[MyEnum::ONE] = 1;
aModules[MyEnum::TWO] = 1;
aModules[MyEnum::THREE] = 1;
o3tl::enumarray<MyEnum, int> const & rModules = aModules;
for (auto const & i : rModules)
CPPUNIT_ASSERT_EQUAL( 1, i );
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment