[Libreoffice-commits] core.git: include/o3tl o3tl/CppunitTest_o3tl_tests.mk o3tl/qa
Noel Grandin
noel.grandin at collabora.co.uk
Wed Oct 25 14:42:30 UTC 2017
include/o3tl/enumarray.hxx | 56 ++++++++++++++++++++++++++++++++---------
o3tl/CppunitTest_o3tl_tests.mk | 1
o3tl/qa/test-enumarray.cxx | 48 +++++++++++++++++++++++++++++++++++
3 files changed, 94 insertions(+), 11 deletions(-)
New commits:
commit a0032a2dc2e4ac7615baaacdde5fefa64048822e
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date: Tue Oct 24 14:59:41 2017 +0200
improve o3tl::enumarray const-ness
Change-Id: I7b0d10b024edf604a7dea0e3b1399073f4bcba92
Reviewed-on: https://gerrit.libreoffice.org/43773
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>
diff --git a/include/o3tl/enumarray.hxx b/include/o3tl/enumarray.hxx
index 766d9670b34a..ca012e197f27 100644
--- a/include/o3tl/enumarray.hxx
+++ b/include/o3tl/enumarray.hxx
@@ -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
diff --git a/o3tl/CppunitTest_o3tl_tests.mk b/o3tl/CppunitTest_o3tl_tests.mk
index 01a7cc0cbdc5..e8fd27095b06 100644
--- a/o3tl/CppunitTest_o3tl_tests.mk
+++ b/o3tl/CppunitTest_o3tl_tests.mk
@@ -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 \
diff --git a/o3tl/qa/test-enumarray.cxx b/o3tl/qa/test-enumarray.cxx
new file mode 100644
index 000000000000..9da8dae18f0b
--- /dev/null
+++ b/o3tl/qa/test-enumarray.cxx
@@ -0,0 +1,48 @@
+/* -*- 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: */
More information about the Libreoffice-commits
mailing list