summaryrefslogtreecommitdiff
path: root/include/o3tl
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-02-09 11:29:05 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-02-13 09:38:13 +0000
commite2e76df7e48fb77f1e802f57c7d9a22eb8c74c5a (patch)
tree15a072f5563c843939b0900aed256b7c89340407 /include/o3tl
parent417bc898802630c567d970d0283312697acdd5ff (diff)
create strong_int template and use it in tools::UniqueIndex
an experiment to see how useful a strong_int template works out Change-Id: Ib77700350f0fa3b018a1926233adf7a40d728d16 Reviewed-on: https://gerrit.libreoffice.org/34072 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include/o3tl')
-rw-r--r--include/o3tl/strong_int.hxx74
1 files changed, 74 insertions, 0 deletions
diff --git a/include/o3tl/strong_int.hxx b/include/o3tl/strong_int.hxx
new file mode 100644
index 000000000000..7f24714411dc
--- /dev/null
+++ b/include/o3tl/strong_int.hxx
@@ -0,0 +1,74 @@
+/* -*- 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_STRONG_TYPEDEF_HXX
+#define INCLUDED_O3TL_STRONG_TYPEDEF_HXX
+
+#include <sal/config.h>
+
+namespace o3tl
+{
+
+///
+/// Wrap up an integer type so that we prevent accidental conversion to other integer types.
+///
+/// e.g.
+/// struct MyIntTag {};
+/// typedef o3tl::strong_int<unsigned, MyIntTag> MyInt;
+///
+/// \param UNDERLYING_TYPE the underlying scalar type
+/// \param PHANTOM_TYPE a type tag, used to distinguish this instantion of the template
+/// from other instantiantions with the same UNDERLYING_TYPE.
+///
+template <typename UNDERLYING_TYPE, typename PHANTOM_TYPE>
+struct strong_int
+{
+public:
+ strong_int(UNDERLYING_TYPE value) : m_value(value) {}
+ strong_int() : m_value(0) {}
+
+ explicit operator UNDERLYING_TYPE() const { return m_value; }
+ explicit operator bool() const { return m_value != 0; }
+ UNDERLYING_TYPE get() const { return m_value; }
+
+ bool operator<(strong_int const & other) const { return m_value < other.m_value; }
+ bool operator<=(strong_int const & other) const { return m_value <= other.m_value; }
+ bool operator>(strong_int const & other) const { return m_value > other.m_value; }
+ bool operator>=(strong_int const & other) const { return m_value >= other.m_value; }
+ bool operator==(strong_int const & other) const { return m_value == other.m_value; }
+ bool operator!=(strong_int const & other) const { return m_value != other.m_value; }
+ strong_int& operator++() { ++m_value; return *this; }
+ strong_int operator++(int) { UNDERLYING_TYPE nOldValue = m_value; ++m_value; return strong_int(nOldValue); }
+
+private:
+ UNDERLYING_TYPE m_value;
+};
+
+template <typename UT, typename PT>
+strong_int<UT,PT> operator+(strong_int<UT,PT> const & lhs, strong_int<UT,PT> const & rhs)
+{
+ return strong_int<UT,PT>(lhs.get() + rhs.get());
+}
+
+
+}; // namespace o3tl
+
+#endif /* INCLUDED_O3TL_STRONG_TYPEDEF_HXX */
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */