LCOV - code coverage report
Current view: top level - src/utils - MooseEnum.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 62 80 77.5 %
Date: 2025-08-08 20:01:16 Functions: 14 19 73.7 %
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    19699267 : MooseEnum::MooseEnum(std::string names, std::string default_name, bool allow_out_of_range)
      23    19699267 :   : MooseEnumBase(names, allow_out_of_range), _current("", MooseEnumItem::INVALID_ID)
      24             : {
      25    19699267 :   *this = default_name;
      26    19699267 : }
      27             : 
      28     1035434 : MooseEnum::MooseEnum(const MooseEnum & other_enum)
      29     1035434 :   : MooseEnumBase(other_enum), _current(other_enum._current)
      30             : {
      31     1035434 : }
      32             : 
      33             : /**
      34             :  * Private constuctor for use by libmesh::Parameters
      35             :  */
      36    32410854 : MooseEnum::MooseEnum() : _current("", MooseEnumItem::INVALID_ID) {}
      37             : 
      38             : MooseEnum &
      39    21098354 : MooseEnum::operator=(const std::string & name)
      40             : {
      41    21098354 :   assign(name);
      42    21098339 :   return *this;
      43             : }
      44             : 
      45             : MooseEnum &
      46      283409 : MooseEnum::operator=(int value)
      47             : {
      48      283409 :   assign(value);
      49      283408 :   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    21098354 : MooseEnum::assign(const std::string & name, const std::optional<std::string> & context)
      61             : {
      62    21098354 :   if (name == "")
      63             :   {
      64     3386646 :     _current = MooseEnumItem("", MooseEnumItem::INVALID_ID);
      65     3386646 :     return;
      66             :   }
      67             : 
      68    17711708 :   std::set<MooseEnumItem>::const_iterator iter = find(name);
      69    17711708 :   if (iter == _items.end())
      70             :   {
      71         254 :     if (!_allow_out_of_range) // Are out of range values allowed?
      72          16 :       mooseError(context ? (*context + ":\n\n") : std::string(""),
      73             :                  "Invalid option \"",
      74             :                  name,
      75             :                  "\" in MooseEnum.  Valid options (not case-sensitive) are \"",
      76          16 :                  getRawNames(),
      77             :                  "\".");
      78             :     else
      79             :     {
      80         240 :       _current = MooseEnumItem(name, getNextValidID());
      81         240 :       _items.insert(_current);
      82             :     }
      83             :   }
      84             :   else
      85    17711454 :     _current = *iter;
      86             : 
      87    17711694 :   checkDeprecated();
      88             : }
      89             : 
      90             : void
      91      283409 : MooseEnum::assign(int value)
      92             : {
      93      283409 :   if (value == MooseEnumItem::INVALID_ID)
      94             :   {
      95           0 :     _current = MooseEnumItem("", MooseEnumItem::INVALID_ID);
      96           0 :     return;
      97             :   }
      98             : 
      99      283409 :   std::set<MooseEnumItem>::const_iterator iter = find(value);
     100      283409 :   if (iter == _items.end())
     101           1 :     mooseError("Invalid id \"",
     102             :                value,
     103             :                "\" in MooseEnum. Valid ids are \"",
     104           8 :                Moose::stringify(getIDs()),
     105             :                "\".");
     106             :   else
     107      283408 :     _current = *iter;
     108             : 
     109      283408 :   checkDeprecated();
     110             : }
     111             : 
     112             : void
     113       78694 : MooseEnum::assign(const MooseEnumItem & item)
     114             : {
     115       78694 :   std::set<MooseEnumItem>::const_iterator iter = find(item);
     116       78694 :   if (iter == _items.end())
     117           0 :     mooseError("Invalid item \"",
     118             :                item,
     119             :                "\" in MooseEnum. Valid ids are \"",
     120           0 :                Moose::stringify(items()),
     121             :                "\".");
     122             :   else
     123       78694 :     _current = *iter;
     124             : 
     125       78694 :   checkDeprecated();
     126       78694 : }
     127             : 
     128             : bool
     129    50997245 : MooseEnum::operator==(const char * name) const
     130             : {
     131    50997245 :   std::string upper(MooseUtils::toUpper(name));
     132             : 
     133             :   mooseAssert(_allow_out_of_range || find(upper) != _items.end(),
     134             :               "Invalid string comparison \"" + upper +
     135             :                   "\" in MooseEnum.  Valid options (not case-sensitive) are \"" + getRawNames() +
     136             :                   "\".");
     137             : 
     138   101994490 :   return _current == upper;
     139    50997245 : }
     140             : 
     141             : bool
     142     1019465 : MooseEnum::operator!=(const char * name) const
     143             : {
     144     1019465 :   return !(*this == name);
     145             : }
     146             : 
     147             : bool
     148    50507850 : MooseEnum::operator==(int value) const
     149             : {
     150    50507850 :   return value == _current;
     151             : }
     152             : 
     153             : bool
     154         809 : MooseEnum::operator!=(int value) const
     155             : {
     156         809 :   return value != _current;
     157             : }
     158             : 
     159             : bool
     160           0 : MooseEnum::operator==(unsigned short value) const
     161             : {
     162           0 :   return value == _current;
     163             : }
     164             : 
     165             : bool
     166           0 : MooseEnum::operator!=(unsigned short value) const
     167             : {
     168           0 :   return value != _current;
     169             : }
     170             : 
     171             : bool
     172      119886 : MooseEnum::compareCurrent(const MooseEnum & other, CompareMode mode) const
     173             : {
     174      119886 :   switch (mode)
     175             :   {
     176           5 :     case CompareMode::COMPARE_BOTH:
     177           5 :       return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
     178      119876 :     case CompareMode::COMPARE_NAME:
     179      119876 :       return _current.name() == other._current.name();
     180           5 :     case CompareMode::COMPARE_ID:
     181           5 :       return _current.id() == other._current.id();
     182             :   }
     183           0 :   return false;
     184             : }
     185             : 
     186             : bool
     187           0 : MooseEnum::operator==(const MooseEnum & value) const
     188             : {
     189           0 :   mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
     190             :                   "the 'compareCurrent' method instead.");
     191           0 :   return value._current.name() == _current.name();
     192             : }
     193             : 
     194             : bool
     195           0 : MooseEnum::operator!=(const MooseEnum & value) const
     196             : {
     197           0 :   mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
     198             :                   "the 'compareCurrent' method instead.");
     199           0 :   return value._current.name() != _current.name();
     200             : }
     201             : 
     202             : void
     203    18073797 : MooseEnum::checkDeprecated() const
     204             : {
     205    18073797 :   MooseEnumBase::checkDeprecated(_current);
     206    18073796 : }

Generated by: LCOV version 1.14