summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorNoel Grandin <noel@peralex.com>2015-03-30 11:20:37 +0200
committerNoel Grandin <noel@peralex.com>2015-04-01 09:36:19 +0200
commit4b66829390b286010b37b37ec1537a320d8cea8f (patch)
treee3070f55a80dd8d6f5944db4594608865d0fbbcc /include
parent427ef167e1a49ba7fcdef082de43622e02a84ce5 (diff)
convert BOX_LINE and BOXINFO_LINE to enum class
since their usage is intertwined. Also introduce new o3tl utilities enumrange and enumarray to make working with scoped enums a little simpler. Change-Id: I2e1cc65dd7c638e59f17d96dfae504747cad6533
Diffstat (limited to 'include')
-rw-r--r--include/editeng/boxitem.hxx61
-rw-r--r--include/o3tl/enumarray.hxx107
-rw-r--r--include/o3tl/enumrange.hxx90
3 files changed, 228 insertions, 30 deletions
diff --git a/include/editeng/boxitem.hxx b/include/editeng/boxitem.hxx
index c99ef2956776..99379f42fc75 100644
--- a/include/editeng/boxitem.hxx
+++ b/include/editeng/boxitem.hxx
@@ -34,11 +34,10 @@
This item describes a border attribute
(all four edges and the inward distance)
*/
-
-#define BOX_LINE_TOP ((sal_uInt16)0)
-#define BOX_LINE_BOTTOM ((sal_uInt16)1)
-#define BOX_LINE_LEFT ((sal_uInt16)2)
-#define BOX_LINE_RIGHT ((sal_uInt16)3)
+enum class SvxBoxItemLine
+{
+ TOP, BOTTOM, LEFT, RIGHT, LAST = RIGHT
+};
/**
This version causes SvxBoxItem to store the 4 cell spacing distances separately
@@ -93,20 +92,20 @@ public:
const editeng::SvxBorderLine* GetLeft() const { return pLeft; }
const editeng::SvxBorderLine* GetRight() const { return pRight; }
- const editeng::SvxBorderLine* GetLine( sal_uInt16 nLine ) const;
+ const editeng::SvxBorderLine* GetLine( SvxBoxItemLine nLine ) const;
//The Pointers are being copied!
- void SetLine( const editeng::SvxBorderLine* pNew, sal_uInt16 nLine );
+ void SetLine( const editeng::SvxBorderLine* pNew, SvxBoxItemLine nLine );
- sal_uInt16 GetDistance( sal_uInt16 nLine ) const;
+ sal_uInt16 GetDistance( SvxBoxItemLine nLine ) const;
sal_uInt16 GetDistance() const;
- void SetDistance( sal_uInt16 nNew, sal_uInt16 nLine );
+ void SetDistance( sal_uInt16 nNew, SvxBoxItemLine nLine );
inline void SetDistance( sal_uInt16 nNew );
// Line width plus Space plus inward distance
// bIgnoreLine = TRUE -> Also return distance, when no Line is set
- sal_uInt16 CalcLineSpace( sal_uInt16 nLine, bool bIgnoreLine = false ) const;
+ sal_uInt16 CalcLineSpace( SvxBoxItemLine nLine, bool bIgnoreLine = false ) const;
static com::sun::star::table::BorderLine2 SvxLineToLine( const editeng::SvxBorderLine* pLine, bool bConvert );
static bool LineToSvxLine(const ::com::sun::star::table::BorderLine& rLine, editeng::SvxBorderLine& rSvxLine, bool bConvert);
static bool LineToSvxLine(const ::com::sun::star::table::BorderLine2& rLine, editeng::SvxBorderLine& rSvxLine, bool bConvert);
@@ -127,8 +126,10 @@ inline void SvxBoxItem::SetDistance( sal_uInt16 nNew )
transported the borderline for the inner horizontal and vertical lines.
*/
-#define BOXINFO_LINE_HORI ((sal_uInt16)0)
-#define BOXINFO_LINE_VERT ((sal_uInt16)1)
+enum class SvxBoxInfoItemLine
+{
+ HORI, VERT, LAST = VERT
+};
enum class SvxBoxInfoItemValidFlags
{
@@ -181,8 +182,8 @@ public:
SvxBoxInfoItem &operator=( const SvxBoxInfoItem &rCpy );
// "pure virtual Methods" from SfxPoolItem
- virtual bool operator==( const SfxPoolItem& ) const SAL_OVERRIDE;
- virtual bool GetPresentation( SfxItemPresentation ePres,
+ virtual bool operator==( const SfxPoolItem& ) const SAL_OVERRIDE;
+ virtual bool GetPresentation( SfxItemPresentation ePres,
SfxMapUnit eCoreMetric,
SfxMapUnit ePresMetric,
OUString &rText, const IntlWrapper * = 0 ) const SAL_OVERRIDE;
@@ -199,22 +200,22 @@ public:
const editeng::SvxBorderLine* GetVert() const { return pVert; }
//The Pointers are being copied!
- void SetLine( const editeng::SvxBorderLine* pNew, sal_uInt16 nLine );
-
- bool IsTable() const { return mbEnableHor && mbEnableVer; }
- void SetTable( bool bNew ) { mbEnableHor = mbEnableVer = bNew; }
-
- inline bool IsHorEnabled() const { return mbEnableHor; }
- inline void EnableHor( bool bEnable ) { mbEnableHor = bEnable; }
- inline bool IsVerEnabled() const { return mbEnableVer; }
- inline void EnableVer( bool bEnable ) { mbEnableVer = bEnable; }
-
- bool IsDist() const { return bDist; }
- void SetDist( bool bNew ) { bDist = bNew; }
- bool IsMinDist() const { return bMinDist; }
- void SetMinDist( bool bNew ) { bMinDist = bNew; }
- sal_uInt16 GetDefDist() const { return nDefDist; }
- void SetDefDist( sal_uInt16 nNew ) { nDefDist = nNew; }
+ void SetLine( const editeng::SvxBorderLine* pNew, SvxBoxInfoItemLine nLine );
+
+ bool IsTable() const { return mbEnableHor && mbEnableVer; }
+ void SetTable( bool bNew ) { mbEnableHor = mbEnableVer = bNew; }
+
+ inline bool IsHorEnabled() const { return mbEnableHor; }
+ inline void EnableHor( bool bEnable ) { mbEnableHor = bEnable; }
+ inline bool IsVerEnabled() const { return mbEnableVer; }
+ inline void EnableVer( bool bEnable ) { mbEnableVer = bEnable; }
+
+ bool IsDist() const { return bDist; }
+ void SetDist( bool bNew ) { bDist = bNew; }
+ bool IsMinDist() const { return bMinDist; }
+ void SetMinDist( bool bNew ) { bMinDist = bNew; }
+ sal_uInt16 GetDefDist() const { return nDefDist; }
+ void SetDefDist( sal_uInt16 nNew ) { nDefDist = nNew; }
bool IsValid( SvxBoxInfoItemValidFlags nValid ) const
{ return bool( nValidFlags & nValid ); }
diff --git a/include/o3tl/enumarray.hxx b/include/o3tl/enumarray.hxx
new file mode 100644
index 000000000000..89f7e16a6ce1
--- /dev/null
+++ b/include/o3tl/enumarray.hxx
@@ -0,0 +1,107 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#ifndef INCLUDED_O3TL_ENUMARRAY_HXX
+#define INCLUDED_O3TL_ENUMARRAY_HXX
+
+#include <sal/config.h>
+#include <initializer_list>
+#include <iterator>
+
+namespace o3tl {
+
+template<typename EA>
+class enumarray_iterator;
+
+///
+/// This is a container convenience class for arrays indexed by enum values.
+///
+/// This assumes that the 'enum class' definition
+/// - starts at zero
+/// - has no holes in it's sequence of values
+/// - defines a value called LAST which refers to the greatest constant.
+///
+/// \param E the 'enum class' type.
+/// \param V the value type to be stored in the array
+template<typename E, typename V>
+class enumarray SAL_FINAL
+{
+public:
+ typedef enumarray<E, V> self_type;
+ typedef enumarray_iterator<self_type> iterator;
+
+ typedef V value_type;
+ typedef E key_type;
+ typedef size_t size_type;
+
+ static const size_type max_index = static_cast<size_type>(E::LAST);
+
+ /** Create an enumarray with the given elements.
+
+ @param init an initializer_list
+ */
+ enumarray(std::initializer_list<V> init)
+ { std::copy(init.begin(), init.end(), std::begin(values)); }
+
+ enumarray() {}
+
+ const V operator[](E index) const
+ {
+ assert(index>=static_cast<E>(0) && index<=E::LAST);
+ return values[static_cast<size_type>(index)];
+ }
+
+ V& operator[](E index)
+ {
+ assert(index>=static_cast<E>(0) && index<=E::LAST);
+ return values[static_cast<size_type>(index)];
+ }
+
+ void fill(V val)
+ { for (size_type i=0; i<=max_index; ++i) values[i] = val; }
+
+ size_type size() const { return max_index + 1; }
+ iterator begin() { return iterator(this, 0); }
+ iterator end() { return iterator(this, size()); }
+private:
+ V values[max_index + 1];
+};
+
+
+template<typename EA>
+class enumarray_iterator {
+ EA &m_buf;
+ size_t m_pos;
+public:
+ typedef enumarray_iterator<EA> self_type;
+ typedef typename EA::value_type value_type;
+ typedef typename EA::key_type key_type;
+
+ 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; }
+};
+
+}; // namespace o3tl
+
+#endif /* INCLUDED_O3TL_ENUMARRAY_HXX */
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/o3tl/enumrange.hxx b/include/o3tl/enumrange.hxx
new file mode 100644
index 000000000000..b127daf54f15
--- /dev/null
+++ b/include/o3tl/enumrange.hxx
@@ -0,0 +1,90 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * 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/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#ifndef INCLUDED_O3TL_ENUMRANGE_HXX
+#define INCLUDED_O3TL_ENUMRANGE_HXX
+
+#include <sal/config.h>
+
+namespace o3tl {
+
+///
+/// This is a container convenience class iterating over scoped enumerations.
+///
+/// This assumes that the 'enum class' definition
+/// - starts at zero
+/// - has no holes in it's sequence of values
+/// - defines a value called LAST which refers to the greatest constant.
+///
+/// Use like this:
+/// enum class COLOR { RED, GREEN, BLUE, LAST=BLUE };
+/// for( auto e : o3tl::enumrange<Color>() )
+/// .....;
+///
+/// \param T the 'enum class' type.
+template< typename T>
+class enumrange
+{
+public:
+ class Iterator
+ {
+ public:
+ Iterator( int value ) :
+ m_value( value )
+ { }
+
+ T operator*( void ) const
+ {
+ return (T)m_value;
+ }
+
+ void operator++( void )
+ {
+ ++m_value;
+ }
+
+ bool operator!=( Iterator rhs )
+ {
+ return m_value != rhs.m_value;
+ }
+
+ private:
+ int m_value;
+ };
+
+};
+
+template< typename T >
+typename enumrange<T>::Iterator begin( enumrange<T> )
+{
+ return typename enumrange<T>::Iterator( (int)0 );
+}
+
+template< typename T >
+typename enumrange<T>::Iterator end( enumrange<T> )
+{
+ return typename enumrange<T>::Iterator( ((int)T::LAST) + 1 );
+}
+
+
+}; // namespace o3tl
+
+#endif /* INCLUDED_O3TL_ENUMRANGE_HXX */
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */