LCOV - code coverage report
Current view: top level - src/utils - MooseEnum.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 62 80 77.5 %
Date: 2025-07-17 01:28:37 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    18936443 : MooseEnum::MooseEnum(std::string names, std::string default_name, bool allow_out_of_range)
      23    18936443 :   : MooseEnumBase(names, allow_out_of_range), _current("", MooseEnumItem::INVALID_ID)
      24             : {
      25    18936443 :   *this = default_name;
      26    18936443 : }
      27             : 
      28      961364 : MooseEnum::MooseEnum(const MooseEnum & other_enum)
      29      961364 :   : MooseEnumBase(other_enum), _current(other_enum._current)
      30             : {
      31      961364 : }
      32             : 
      33             : /**
      34             :  * Private constuctor for use by libmesh::Parameters
      35             :  */
      36    30933304 : MooseEnum::MooseEnum() : _current("", MooseEnumItem::INVALID_ID) {}
      37             : 
      38             : MooseEnum &
      39    20260333 : MooseEnum::operator=(const std::string & name)
      40             : {
      41    20260333 :   assign(name);
      42    20260318 :   return *this;
      43             : }
      44             : 
      45             : MooseEnum &
      46      260808 : MooseEnum::operator=(int value)
      47             : {
      48      260808 :   assign(value);
      49      260807 :   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    20260333 : MooseEnum::assign(const std::string & name, const std::optional<std::string> & context)
      61             : {
      62    20260333 :   if (name == "")
      63             :   {
      64     3259047 :     _current = MooseEnumItem("", MooseEnumItem::INVALID_ID);
      65     3259047 :     return;
      66             :   }
      67             : 
      68    17001286 :   std::set<MooseEnumItem>::const_iterator iter = find(name);
      69    17001286 :   if (iter == _items.end())
      70             :   {
      71         241 :     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         227 :       _current = MooseEnumItem(name, getNextValidID());
      81         227 :       _items.insert(_current);
      82             :     }
      83             :   }
      84             :   else
      85    17001045 :     _current = *iter;
      86             : 
      87    17001272 :   checkDeprecated();
      88             : }
      89             : 
      90             : void
      91      260808 : MooseEnum::assign(int value)
      92             : {
      93      260808 :   if (value == MooseEnumItem::INVALID_ID)
      94             :   {
      95           0 :     _current = MooseEnumItem("", MooseEnumItem::INVALID_ID);
      96           0 :     return;
      97             :   }
      98             : 
      99      260808 :   std::set<MooseEnumItem>::const_iterator iter = find(value);
     100      260808 :   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      260807 :     _current = *iter;
     108             : 
     109      260807 :   checkDeprecated();
     110             : }
     111             : 
     112             : void
     113       72342 : MooseEnum::assign(const MooseEnumItem & item)
     114             : {
     115       72342 :   std::set<MooseEnumItem>::const_iterator iter = find(item);
     116       72342 :   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       72342 :     _current = *iter;
     124             : 
     125       72342 :   checkDeprecated();
     126       72342 : }
     127             : 
     128             : bool
     129    44884922 : MooseEnum::operator==(const char * name) const
     130             : {
     131    44884922 :   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    89769844 :   return _current == upper;
     139    44884922 : }
     140             : 
     141             : bool
     142      939339 : MooseEnum::operator!=(const char * name) const
     143             : {
     144      939339 :   return !(*this == name);
     145             : }
     146             : 
     147             : bool
     148    46684797 : MooseEnum::operator==(int value) const
     149             : {
     150    46684797 :   return value == _current;
     151             : }
     152             : 
     153             : bool
     154         742 : MooseEnum::operator!=(int value) const
     155             : {
     156         742 :   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      111113 : MooseEnum::compareCurrent(const MooseEnum & other, CompareMode mode) const
     173             : {
     174      111113 :   switch (mode)
     175             :   {
     176           5 :     case CompareMode::COMPARE_BOTH:
     177           5 :       return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
     178      111103 :     case CompareMode::COMPARE_NAME:
     179      111103 :       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    17334422 : MooseEnum::checkDeprecated() const
     204             : {
     205    17334422 :   MooseEnumBase::checkDeprecated(_current);
     206    17334421 : }

Generated by: LCOV version 1.14