LCOV - code coverage report
Current view: top level - src/utils - MooseEnum.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #32971 (54bef8) with base c6cf66 Lines: 61 82 74.4 %
Date: 2026-05-29 20:35:17 Functions: 13 19 68.4 %
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             : #include "MooseEnum.h"
      11             : #include "MooseUtils.h"
      12             : #include "MooseError.h"
      13             : #include "Conversion.h"
      14             : 
      15             : #include <sstream>
      16             : #include <algorithm>
      17             : #include <iterator>
      18             : #include <limits>
      19             : #include <string>
      20             : #include <iostream>
      21             : 
      22    20762682 : MooseEnum::MooseEnum(std::string names, std::string default_name, bool allow_out_of_range)
      23    41525364 :   : MooseEnumBase(names, allow_out_of_range), _current("", MooseEnumItem::INVALID_ID)
      24             : {
      25    20762682 :   *this = default_name;
      26    20762682 : }
      27             : 
      28      592407 : MooseEnum::MooseEnum(const MooseEnum & other_enum)
      29      592407 :   : MooseEnumBase(other_enum), _current(other_enum._current)
      30             : {
      31      592407 : }
      32             : 
      33             : /**
      34             :  * Private constuctor for use by libmesh::Parameters
      35             :  */
      36    79368405 : MooseEnum::MooseEnum() : _current("", MooseEnumItem::INVALID_ID) {}
      37             : 
      38             : MooseEnum &
      39    21885199 : MooseEnum::operator=(const std::string & name)
      40             : {
      41    21885199 :   assign(name);
      42    21885182 :   return *this;
      43             : }
      44             : 
      45             : MooseEnum &
      46      251284 : MooseEnum::operator=(int value)
      47             : {
      48      251284 :   assign(value);
      49      251282 :   return *this;
      50             : }
      51             : 
      52             : MooseEnum &
      53           0 : MooseEnum::operator=(const MooseEnumItem & item)
      54             : {
      55           0 :   assign(item);
      56           0 :   return *this;
      57             : }
      58             : 
      59             : void
      60    21885199 : MooseEnum::assign(const std::string & name, const std::optional<std::string> & context)
      61             : {
      62    43770398 :   if (MooseUtils::trim(name) == "")
      63             :   {
      64     8481515 :     _current = MooseEnumItem("", MooseEnumItem::INVALID_ID);
      65     8481515 :     return;
      66             :   }
      67             : 
      68             :   // Check if name can be parsed cleanly into an integer
      69    13403684 :   int value = 0;
      70    13403684 :   bool integer = (std::istringstream(MooseUtils::trim(name)) >> value).eof();
      71             : 
      72    13403684 :   if (auto iter = find(name); iter != _items.end())
      73    13403548 :     _current = *iter;
      74         136 :   else if (auto subs = find(value); subs != _items.end() && integer)
      75           0 :     _current = *subs;
      76             :   else
      77             :   {
      78         136 :     if (!_allow_out_of_range) // Are out of range values allowed?
      79          58 :       mooseError(context ? (*context + ":\n\n") : std::string(""),
      80             :                  "Invalid option \"",
      81             :                  name,
      82             :                  "\" in MooseEnum.  Valid options (not case-sensitive) are \"",
      83          43 :                  getRawNames(),
      84             :                  "\".");
      85             : 
      86         121 :     _current = MooseEnumItem(name, integer ? value : getNextValidID());
      87         121 :     _items.insert(_current);
      88             :   }
      89             : 
      90    13403669 :   checkDeprecated();
      91             : }
      92             : 
      93             : void
      94      254361 : MooseEnum::assign(int value)
      95             : {
      96      254361 :   if (value == MooseEnumItem::INVALID_ID)
      97             :   {
      98           0 :     _current = MooseEnumItem("", MooseEnumItem::INVALID_ID);
      99           0 :     return;
     100             :   }
     101             : 
     102      254361 :   if (auto iter = find(value); iter != _items.end())
     103      254359 :     _current = *iter;
     104             :   else
     105           2 :     mooseError("Invalid id \"",
     106             :                value,
     107             :                "\" in MooseEnum. Valid ids are \"",
     108          14 :                Moose::stringify(getIDs()),
     109             :                "\".");
     110             : 
     111      254359 :   checkDeprecated();
     112             : }
     113             : 
     114             : void
     115       77288 : MooseEnum::assign(const MooseEnumItem & item)
     116             : {
     117       77288 :   std::set<MooseEnumItem>::const_iterator iter = find(item);
     118       77288 :   if (iter == _items.end())
     119           0 :     mooseError("Invalid item \"",
     120             :                item,
     121             :                "\" in MooseEnum. Valid ids are \"",
     122           0 :                Moose::stringify(items()),
     123             :                "\".");
     124             :   else
     125       77288 :     _current = *iter;
     126             : 
     127       77288 :   checkDeprecated();
     128       77288 : }
     129             : 
     130             : bool
     131    43565605 : MooseEnum::operator==(const char * name) const
     132             : {
     133    43565605 :   std::string upper(MooseUtils::toUpper(name));
     134             : 
     135             :   mooseAssert(_allow_out_of_range || find(upper) != _items.end(),
     136             :               "Invalid string comparison \"" + upper +
     137             :                   "\" in MooseEnum.  Valid options (not case-sensitive) are \"" + getRawNames() +
     138             :                   "\".");
     139             : 
     140    87131210 :   return _current == upper;
     141    43565605 : }
     142             : 
     143             : bool
     144     1014362 : MooseEnum::operator!=(const char * name) const
     145             : {
     146     1014362 :   return !(*this == name);
     147             : }
     148             : 
     149             : bool
     150    44244284 : MooseEnum::operator==(int value) const
     151             : {
     152    44244284 :   return value == _current;
     153             : }
     154             : 
     155             : bool
     156           0 : MooseEnum::operator!=(int value) const
     157             : {
     158           0 :   return value != _current;
     159             : }
     160             : 
     161             : bool
     162           0 : MooseEnum::operator==(unsigned short value) const
     163             : {
     164           0 :   return value == _current;
     165             : }
     166             : 
     167             : bool
     168           0 : MooseEnum::operator!=(unsigned short value) const
     169             : {
     170           0 :   return value != _current;
     171             : }
     172             : 
     173             : bool
     174      117699 : MooseEnum::compareCurrent(const MooseEnum & other, CompareMode mode) const
     175             : {
     176      117699 :   switch (mode)
     177             :   {
     178          10 :     case CompareMode::COMPARE_BOTH:
     179          10 :       return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
     180      117679 :     case CompareMode::COMPARE_NAME:
     181      117679 :       return _current.name() == other._current.name();
     182          10 :     case CompareMode::COMPARE_ID:
     183          10 :       return _current.id() == other._current.id();
     184             :   }
     185           0 :   return false;
     186             : }
     187             : 
     188             : bool
     189           0 : MooseEnum::operator==(const MooseEnum & value) const
     190             : {
     191           0 :   mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
     192             :                   "the 'compareCurrent' method instead.");
     193           0 :   return value._current.name() == _current.name();
     194             : }
     195             : 
     196             : bool
     197           0 : MooseEnum::operator!=(const MooseEnum & value) const
     198             : {
     199           0 :   mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
     200             :                   "the 'compareCurrent' method instead.");
     201           0 :   return value._current.name() != _current.name();
     202             : }
     203             : 
     204             : void
     205    13735318 : MooseEnum::checkDeprecated() const
     206             : {
     207    13735318 :   MooseEnumBase::checkDeprecated(_current);
     208    13735316 : }

Generated by: LCOV version 1.14