LCOV - code coverage report
Current view: top level - gjs - enum-utils.h (source / functions) Coverage Total Hit
Test: gjs- Code Coverage Lines: 100.0 % 12 12
Test Date: 2024-03-26 02:45:07 Functions: 85.7 % 7 6
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
       2                 :             : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
       3                 :             : // SPDX-FileCopyrightText: 2020 Marco Trevisan <marco.trevisan@canonical.com>
       4                 :             : 
       5                 :             : #pragma once
       6                 :             : 
       7                 :             : #include <config.h>
       8                 :             : 
       9                 :             : #include <type_traits>
      10                 :             : 
      11                 :             : namespace GjsEnum {
      12                 :             : 
      13                 :             : template <typename T>
      14                 :             : constexpr bool is_class() {
      15                 :             :     if constexpr (std::is_enum_v<T>) {
      16                 :             :         return !std::is_convertible_v<T, std::underlying_type_t<T>>;
      17                 :             :     }
      18                 :             :     return false;
      19                 :             : }
      20                 :             : 
      21                 :             : template <class EnumType>
      22                 :             : struct WrapperImpl {
      23                 :             :     EnumType e;
      24                 :             : 
      25                 :      183838 :     constexpr explicit WrapperImpl(EnumType const& en) : e(en) {}
      26                 :       91919 :     constexpr explicit WrapperImpl(std::underlying_type_t<EnumType> const& en)
      27                 :       91919 :         : e(static_cast<EnumType>(en)) {}
      28                 :        7430 :     constexpr explicit operator bool() const { return static_cast<bool>(e); }
      29                 :             :     constexpr operator EnumType() const { return e; }
      30                 :      268327 :     constexpr operator std::underlying_type_t<EnumType>() const {
      31                 :      268327 :         return std::underlying_type_t<EnumType>(e);
      32                 :             :     }
      33                 :             : };
      34                 :             : 
      35                 :             : 
      36                 :             : #if defined (__clang__) || defined (__GNUC__)
      37                 :             : template <class EnumType>
      38                 :             : using Wrapper =
      39                 :             :     std::conditional_t<is_class<EnumType>(), WrapperImpl<EnumType>, void>;
      40                 :             : #else
      41                 :             : template <class EnumType>
      42                 :             : using Wrapper =
      43                 :             :     std::conditional_t<is_class<EnumType>(), std::underlying_type_t<EnumType>, void>;
      44                 :             : #endif
      45                 :             : }  // namespace GjsEnum
      46                 :             : 
      47                 :             : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
      48                 :       91919 : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped> operator&(
      49                 :             :     EnumType const& first, EnumType const& second) {
      50                 :      183838 :     return static_cast<Wrapped>(static_cast<Wrapped>(first) &
      51                 :      183838 :                                 static_cast<Wrapped>(second));
      52                 :             : }
      53                 :             : 
      54                 :             : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
      55                 :             : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped> operator|(
      56                 :             :     EnumType const& first, EnumType const& second) {
      57                 :             :     return static_cast<Wrapped>(static_cast<Wrapped>(first) |
      58                 :             :                                 static_cast<Wrapped>(second));
      59                 :             : }
      60                 :             : 
      61                 :             : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
      62                 :             : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped> operator^(
      63                 :             :     EnumType const& first, EnumType const& second) {
      64                 :             :     return static_cast<Wrapped>(static_cast<Wrapped>(first) ^
      65                 :             :                                 static_cast<Wrapped>(second));
      66                 :             : }
      67                 :             : 
      68                 :             : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
      69                 :       24816 : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator|=(
      70                 :             :     EnumType& first,  //  NOLINT(runtime/references)
      71                 :             :     EnumType const& second) {
      72                 :       24816 :     first = static_cast<EnumType>(first | second);
      73                 :       24816 :     return reinterpret_cast<Wrapped&>(first);
      74                 :             : }
      75                 :             : 
      76                 :             : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
      77                 :             : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), Wrapped&> operator&=(
      78                 :             :     EnumType& first,  //  NOLINT(runtime/references)
      79                 :             :     EnumType const& second) {
      80                 :             :     first = static_cast<EnumType>(first & second);
      81                 :             :     return reinterpret_cast<Wrapped&>(first);
      82                 :             : }
      83                 :             : 
      84                 :             : template <class EnumType, class Wrapped = GjsEnum::Wrapper<EnumType>>
      85                 :             : constexpr std::enable_if_t<GjsEnum::is_class<EnumType>(), EnumType> operator~(
      86                 :             :     EnumType const& first) {
      87                 :             :     return static_cast<EnumType>(~static_cast<Wrapped>(first));
      88                 :             : }
        

Generated by: LCOV version 2.0-1