LCOV - code coverage report
Current view: top level - include/o3tl - typed_flags_set.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 25 25 100.0 %
Date: 2014-11-03 Functions: 41 41 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_O3TL_TYPED_FLAGS_SET_HXX
      21             : #define INCLUDED_O3TL_TYPED_FLAGS_SET_HXX
      22             : 
      23             : #include <sal/config.h>
      24             : 
      25             : #include <cassert>
      26             : #include <type_traits>
      27             : 
      28             : namespace o3tl {
      29             : 
      30             : template<typename T> struct typed_flags {};
      31             : 
      32             : #if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ <= 7 && \
      33             :     !defined __clang__
      34             : #define O3TL_STD_UNDERLYING_TYPE_E signed int
      35             : #else
      36             : #define O3TL_STD_UNDERLYING_TYPE_E typename std::underlying_type<E>::type
      37             : #endif
      38             : 
      39             : /// Mark a (scoped) enumeration as a set of bit flags, with accompanying
      40             : /// operations.
      41             : ///
      42             : ///   template<>
      43             : ///   struct o3tl::typed_flags<TheE>: o3tl::is_typed_flags<TheE, TheM> {};
      44             : ///
      45             : /// All relevant values must be non-negative.  (Typically, the enumeration's
      46             : /// underlying type will either be fixed and unsigned, or it will be unfixed---
      47             : /// and can thus default to a signed type---and all enumerators will have non-
      48             : /// negative values.)
      49             : ///
      50             : /// \param E the enumeration type.
      51             : /// \param M the all-bits-set value for the bit flags.
      52             : template<typename E, O3TL_STD_UNDERLYING_TYPE_E M>
      53             : struct is_typed_flags {
      54             :     static_assert(
      55             :         M >= 0, "is_typed_flags expects only non-negative bit values");
      56             : 
      57             :     typedef E Self;
      58             : 
      59             :     class Wrap {
      60             :     public:
      61    47889438 :         explicit Wrap(O3TL_STD_UNDERLYING_TYPE_E value):
      62    47889438 :             value_(value)
      63    47889438 :         { assert(value >= 0); }
      64             : 
      65     1589995 :         operator E() { return static_cast<E>(value_); }
      66             : 
      67             : #if !defined _MSC_VER || _MSC_VER > 1700
      68             :         explicit
      69             : #endif
      70     1861797 :         operator O3TL_STD_UNDERLYING_TYPE_E() { return value_; }
      71             : 
      72             : #if !defined _MSC_VER || _MSC_VER > 1700
      73             :         explicit
      74             : #endif
      75    44437646 :         operator bool() { return value_ != 0; }
      76             : 
      77             :     private:
      78             :         O3TL_STD_UNDERLYING_TYPE_E value_;
      79             :     };
      80             : 
      81             :     static O3TL_STD_UNDERLYING_TYPE_E const mask = M;
      82             : };
      83             : 
      84             : }
      85             : 
      86             : template<typename E>
      87      148492 : inline typename o3tl::typed_flags<E>::Wrap operator ~(E rhs) {
      88             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs) >= 0);
      89             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
      90             :         o3tl::typed_flags<E>::mask
      91      148492 :         & ~static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
      92             : }
      93             : 
      94             : template<typename E>
      95             : inline typename o3tl::typed_flags<E>::Wrap operator ~(
      96             :     typename o3tl::typed_flags<E>::Wrap rhs)
      97             : {
      98             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
      99             :         o3tl::typed_flags<E>::mask
     100             :         & ~static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     101             : }
     102             : 
     103             : template<typename E>
     104    44323789 : inline typename o3tl::typed_flags<E>::Wrap operator &(E lhs, E rhs) {
     105             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     106             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs) >= 0);
     107             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     108             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     109    44323789 :         & static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     110             : }
     111             : 
     112             : template<typename E>
     113     1145302 : inline typename o3tl::typed_flags<E>::Wrap operator &(
     114             :     E lhs, typename o3tl::typed_flags<E>::Wrap rhs)
     115             : {
     116             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     117             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     118             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     119     1145302 :         & static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     120             : }
     121             : 
     122             : template<typename E>
     123             : inline typename o3tl::typed_flags<E>::Wrap operator &(
     124             :     typename o3tl::typed_flags<E>::Wrap lhs, E rhs)
     125             : {
     126             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs) >= 0);
     127             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     128             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     129             :         & static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     130             : }
     131             : 
     132             : template<typename E>
     133             : inline typename o3tl::typed_flags<E>::Wrap operator &(
     134             :     typename o3tl::typed_flags<E>::Wrap lhs,
     135             :     typename o3tl::typed_flags<E>::Wrap rhs)
     136             : {
     137             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     138             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     139             :         & static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     140             : }
     141             : 
     142             : template<typename E>
     143     1555360 : inline typename o3tl::typed_flags<E>::Wrap operator |(E lhs, E rhs) {
     144             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     145             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs) >= 0);
     146             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     147             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     148     1555360 :         | static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     149             : }
     150             : 
     151             : template<typename E>
     152             : inline typename o3tl::typed_flags<E>::Wrap operator |(
     153             :     E lhs, typename o3tl::typed_flags<E>::Wrap rhs)
     154             : {
     155             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     156             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     157             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     158             :         | static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     159             : }
     160             : 
     161             : template<typename E>
     162      716495 : inline typename o3tl::typed_flags<E>::Wrap operator |(
     163             :     typename o3tl::typed_flags<E>::Wrap lhs, E rhs)
     164             : {
     165             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs) >= 0);
     166             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     167             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     168      716495 :         | static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     169             : }
     170             : 
     171             : template<typename E>
     172             : inline typename o3tl::typed_flags<E>::Wrap operator |(
     173             :     typename o3tl::typed_flags<E>::Wrap lhs,
     174             :     typename o3tl::typed_flags<E>::Wrap rhs)
     175             : {
     176             :     return static_cast<typename o3tl::typed_flags<E>::Wrap>(
     177             :         static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs)
     178             :         | static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs));
     179             : }
     180             : 
     181             : template<typename E>
     182           4 : inline typename o3tl::typed_flags<E>::Self operator &=(E & lhs, E rhs) {
     183             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     184             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs) >= 0);
     185           4 :     lhs = lhs & rhs;
     186           4 :     return lhs;
     187             : }
     188             : 
     189             : template<typename E>
     190      571846 : inline typename o3tl::typed_flags<E>::Self operator &=(
     191             :     E & lhs, typename o3tl::typed_flags<E>::Wrap rhs)
     192             : {
     193             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     194      571846 :     lhs = lhs & rhs;
     195      571846 :     return lhs;
     196             : }
     197             : 
     198             : template<typename E>
     199       93947 : inline typename o3tl::typed_flags<E>::Self operator |=(E & lhs, E rhs) {
     200             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     201             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(rhs) >= 0);
     202       93947 :     lhs = lhs | rhs;
     203       93947 :     return lhs;
     204             : }
     205             : 
     206             : template<typename E>
     207             : inline typename o3tl::typed_flags<E>::Self operator |=(
     208             :     E & lhs, typename o3tl::typed_flags<E>::Wrap rhs)
     209             : {
     210             :     assert(static_cast<O3TL_STD_UNDERLYING_TYPE_E>(lhs) >= 0);
     211             :     lhs = lhs | rhs;
     212             :     return lhs;
     213             : }
     214             : 
     215             : #undef O3TL_STD_UNDERLYING_TYPE_E
     216             : 
     217             : #endif /* INCLUDED_O3TL_TYPED_FLAGS_SET_HXX */
     218             : 
     219             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10