LCOV - code coverage report
Current view: top level - muse/framework/rcommand - enumcontract.h Coverage Total Hit
Test: filtered_coverage.info Lines: 87.5 % 16 14
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore/Audacity CLA applies
       4              :  *
       5              :  * Copyright (C) MuseScore/Audacity and others
       6              :  *
       7              :  * This program is free software: you can redistribute it and/or modify
       8              :  * it under the terms of the GNU General Public License version 3 as
       9              :  * published by the Free Software Foundation.
      10              :  *
      11              :  * This program is distributed in the hope that it will be useful,
      12              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14              :  * GNU General Public License for more details.
      15              :  *
      16              :  * You should have received a copy of the GNU General Public License
      17              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18              :  */
      19              : 
      20              : #pragma once
      21              : 
      22              : #include <array>
      23              : #include <cstddef>
      24              : #include <optional>
      25              : #include <string>
      26              : #include <string_view>
      27              : #include <type_traits>
      28              : #include <vector>
      29              : 
      30              : #include "log.h"
      31              : 
      32              : namespace muse::rcommand {
      33              : template<typename E>
      34              : struct EnumContract;
      35              : 
      36              : template<typename T>
      37              : concept Enumeration = std::is_enum_v<std::remove_cvref_t<T> >;
      38              : 
      39              : template<Enumeration E>
      40              : struct EnumEntry {
      41              :     std::string_view sval;
      42              :     E eval;
      43              :     std::string_view description;
      44              : };
      45              : 
      46              : template<Enumeration E, std::size_t N>
      47              : using EnumTable = std::array<EnumEntry<E>, N>;
      48              : 
      49              : namespace internal {
      50              : //! NOTE `EnumTable` is an alias template, so it can't be matched as is.
      51              : //! This trait recognizes `std::array<EnumEntry<E>, N>` and recovers `E` and `N`.
      52              : template<typename T>
      53              : struct EnumTableTraits {
      54              :     static constexpr bool valid = false;
      55              : };
      56              : 
      57              : template<Enumeration E, std::size_t N>
      58              : struct EnumTableTraits<std::array<EnumEntry<E>, N> > {
      59              :     static constexpr bool valid = true;
      60              :     using Enum = E;
      61              : };
      62              : }
      63              : 
      64              : template<typename T>
      65              : concept IsEnumTable = internal::EnumTableTraits<std::remove_cvref_t<T> >::valid;
      66              : 
      67              : //! NOTE The enum type of an `EnumTable`, e.g. `Foo` for `EnumTable<Foo, 3>`.
      68              : template<typename T>
      69              : using EnumTableEnum = typename internal::EnumTableTraits<std::remove_cvref_t<T> >::Enum;
      70              : 
      71              : // *INDENT-OFF* // Uncrustify doesn't understand `requires`
      72              : template<typename C>
      73              : concept IsEnumContract = requires {
      74              :     { C::table } -> IsEnumTable<>;
      75              : };
      76              : // *INDENT-ON*
      77              : 
      78              : template<IsEnumContract C>
      79              : struct BaseEnumContract {
      80              :     using Enum = EnumTableEnum<decltype(C::table)>;
      81              : 
      82           48 :     static std::vector<std::string> str_vals()
      83              :     {
      84           48 :         std::vector<std::string> result;
      85          472 :         for (const auto& row : C::table) {
      86          848 :             result.push_back(std::string(row.sval));
      87              :         }
      88           48 :         return result;
      89            0 :     }
      90              : 
      91          502 :     static std::string str_val(Enum eval)
      92              :     {
      93         2976 :         for (const auto& row : C::table) {
      94         2976 :             if (row.eval == eval) {
      95         1004 :                 return std::string(row.sval);
      96              :             }
      97              :         }
      98            0 :         UNREACHABLE;
      99              :         return std::string();
     100              :     }
     101              : 
     102           14 :     static std::optional<Enum> enum_val(const std::string& sval)
     103              :     {
     104           27 :         for (const auto& row : C::table) {
     105           25 :             if (row.sval == sval) {
     106           12 :                 return row.eval;
     107              :             }
     108              :         }
     109            2 :         return std::nullopt;
     110              :     }
     111              : };
     112              : 
     113              : //! NOTE The enum type is deduced from the contract's `table`.
     114              : #define DECLARE_ENUM_CONTRACT(C) template<> struct EnumContract<EnumTableEnum<decltype(C::table)> > : BaseEnumContract<C> {};
     115              : }
        

Generated by: LCOV version 2.0-1