LCOV - code coverage report
Current view: top level - include/utils - MooseEnum.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 10 10 100.0 %
Date: 2026-05-29 20:35:17 Functions: 70 74 94.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #pragma once
      11             : 
      12             : // MOOSE includes
      13             : #include "MooseEnumBase.h"
      14             : #include "MooseStringUtils.h"
      15             : #include "MooseUtils.h"
      16             : 
      17             : #include <optional>
      18             : #include <cctype>
      19             : #include <algorithm>
      20             : 
      21             : #define CreateMooseEnumClass(EnumName, /* enumerators */...)                                       \
      22             :   static inline std::string get##EnumName##Options()                                               \
      23             :   {                                                                                                \
      24             :     /* 1) stringify the enumerator list                                               */           \
      25             :     std::string s = #__VA_ARGS__;                                                                  \
      26             :     /* 2) normalize to the format MooseEnum expects: "A B C=3 D ..."                  */           \
      27             :     std::vector<std::string> elements;                                                             \
      28             :     MooseUtils::tokenize(s, elements, 1, ",");                                                     \
      29             :     for (auto & elem : elements)                                                                   \
      30             :       elem.erase(std::remove_if(elem.begin(), elem.end(), isspace), elem.end());                   \
      31             :     return MooseUtils::join(elements, " ");                                                        \
      32             :   }                                                                                                \
      33             :   enum class EnumName                                                                              \
      34             :   {                                                                                                \
      35             :     __VA_ARGS__                                                                                    \
      36             :   }
      37             : 
      38             : // Forward declarations
      39             : namespace libMesh
      40             : {
      41             : class Parameters;
      42             : }
      43             : 
      44             : /**
      45             :  * This is a "smart" enum class intended to replace many of the
      46             :  * shortcomings in the C++ enum type It should be initialized with a
      47             :  * space-delimited list of strings which become the enum values.  You
      48             :  * may also optionally supply numeric ints for one or more values
      49             :  * similar to a C++ enum.  This is done with the "=" sign (no
      50             :  * spaces). It can be used any place where an integer (switch
      51             :  * statements), const char* or std::string is expected. In addition
      52             :  * the InputParameters system has full support for this Enum type
      53             :  */
      54             : class MooseEnum : public MooseEnumBase
      55             : {
      56             : public:
      57             :   /**
      58             :    * Enum item for controlling comparison in the compareCurrent method.
      59             :    */
      60             :   enum class CompareMode
      61             :   {
      62             :     COMPARE_NAME,
      63             :     COMPARE_ID,
      64             :     COMPARE_BOTH
      65             :   };
      66             : 
      67             :   /**
      68             :    * Constructor that takes a list of enumeration values, and a separate string to set a default for
      69             :    * this instance
      70             :    * @param names - a list of names for this enumeration
      71             :    * @param default_name - the default value for this enumeration instance
      72             :    * @param allow_out_of_range - determines whether this enumeration will accept values outside of
      73             :    * it's range of defined values.
      74             :    */
      75             :   MooseEnum(std::string names, std::string default_name = "", bool allow_out_of_range = false);
      76             : 
      77             :   /**
      78             :    * Copy Constructor for use when creating vectors of MooseEnums
      79             :    * @param other_enum - The other enumeration to copy state from
      80             :    */
      81             :   MooseEnum(const MooseEnum & other_enum);
      82             : 
      83             :   /**
      84             :    * Copy Assignment operator must be explicitly defined when a copy ctor exists and this
      85             :    * method is used.
      86             :    */
      87    26383461 :   MooseEnum & operator=(const MooseEnum & other_enum) = default;
      88             : 
      89    44041845 :   virtual ~MooseEnum() = default;
      90             : 
      91             :   /**
      92             :    * Cast operators to make this object behave as value_types and std::string
      93             :    * these methods can be used so that this class behaves more like a normal value_type enumeration
      94             :    */
      95    13910665 :   operator int() const { return _current.id(); }
      96     2192862 :   operator std::string() const { return _current.rawName(); }
      97             : 
      98             :   /**
      99             :    * Comparison operators for comparing with character constants, MooseEnums
     100             :    * or integer values
     101             :    * @param value - RHS value to compare against
     102             :    * @return bool - the truth value for the comparison
     103             :    */
     104             :   bool operator==(const char * value) const;
     105             :   bool operator!=(const char * value) const;
     106             : 
     107             :   bool operator==(int value) const;
     108             :   bool operator!=(int value) const;
     109             : 
     110             :   bool operator==(unsigned short value) const;
     111             :   bool operator!=(unsigned short value) const;
     112             : 
     113             :   bool operator==(const MooseEnum & value) const;
     114             :   bool operator!=(const MooseEnum & value) const;
     115             : 
     116             :   /**
     117             :    * Method for comparing currently set values between MooseEnum.
     118             :    */
     119             :   bool compareCurrent(const MooseEnum & other, CompareMode mode = CompareMode::COMPARE_NAME) const;
     120             : 
     121             :   ///@{
     122             :   /**
     123             :    * Assignment operators/methods
     124             :    * @param name/int - a string or int representing one of the enumeration values.
     125             :    * @return A reference to this object for chaining
     126             :    */
     127             :   MooseEnum & operator=(const std::string & name);
     128             :   MooseEnum & operator=(int value);
     129             :   MooseEnum & operator=(const MooseEnumItem & item);
     130             :   void assign(const std::string & name, const std::optional<std::string> & context = {});
     131             :   void assign(int value);
     132             :   void assign(const MooseEnumItem & item);
     133             :   ///@}
     134             : 
     135             :   /**
     136             :    * IsValid
     137             :    * @return - a Boolean indicating whether this Enumeration has been set
     138             :    */
     139     7417427 :   virtual bool isValid() const override { return _current.id() > MooseEnumItem::INVALID_ID; }
     140             : 
     141             :   // InputParameters is allowed to create an empty enum but is responsible for
     142             :   // filling it in after the fact
     143             :   friend class libMesh::Parameters;
     144             : 
     145             :   /// Operator for printing to iostreams
     146     1017485 :   friend std::ostream & operator<<(std::ostream & out, const MooseEnum & obj)
     147             :   {
     148     1017485 :     out << obj._current.rawName();
     149     1017485 :     return out;
     150             :   }
     151             : 
     152             :   /// get the current value cast to the enum type T
     153             :   template <typename T>
     154             :   T getEnum() const;
     155             : 
     156             : protected:
     157             :   /// Check whether the current value is deprecated when called
     158             :   virtual void checkDeprecated() const override;
     159             : 
     160             :   /**
     161             :    * Constructor for use by libmesh::Parameters and ReporterMode
     162             :    */
     163             :   MooseEnum();
     164             : 
     165             : private:
     166             :   /// The current id
     167             :   MooseEnumItem _current;
     168             : };
     169             : 
     170             : template <typename T>
     171             : T
     172      469409 : MooseEnum::getEnum() const
     173             : {
     174             : #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
     175             :   static_assert(std::is_enum<T>::value == true,
     176             :                 "The type requested from MooseEnum::getEnum must be an enum type!\n\n");
     177             : #endif
     178      469409 :   return static_cast<T>(_current.id());
     179             : }

Generated by: LCOV version 1.14