LCOV - code coverage report
Current view: top level - base - Factory.cxx (source / functions) Coverage Total Hit
Test: coverage.info Lines: 66.7 % 30 20
Test Date: 2025-06-29 01:25:44 Functions: 66.7 % 6 4

            Line data    Source code
       1              : // Copyright 2024, UChicago Argonne, LLC
       2              : // All Rights Reserved
       3              : // Software Name: NEML2 -- the New Engineering material Model Library, version 2
       4              : // By: Argonne National Laboratory
       5              : // OPEN SOURCE LICENSE (MIT)
       6              : //
       7              : // Permission is hereby granted, free of charge, to any person obtaining a copy
       8              : // of this software and associated documentation files (the "Software"), to deal
       9              : // in the Software without restriction, including without limitation the rights
      10              : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      11              : // copies of the Software, and to permit persons to whom the Software is
      12              : // furnished to do so, subject to the following conditions:
      13              : //
      14              : // The above copyright notice and this permission notice shall be included in
      15              : // all copies or substantial portions of the Software.
      16              : //
      17              : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      18              : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      19              : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      20              : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      21              : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      22              : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      23              : // THE SOFTWARE.
      24              : 
      25              : #include "neml2/base/Factory.h"
      26              : #include "neml2/base/OptionSet.h"
      27              : #include "neml2/base/NEML2Object.h"
      28              : #include "neml2/base/Registry.h"
      29              : #include "neml2/base/HITParser.h"
      30              : 
      31              : namespace neml2
      32              : {
      33              : std::unique_ptr<Factory>
      34          346 : load_input(const std::filesystem::path & path, const std::string & additional_input)
      35              : {
      36              :   // For now we only support HIT
      37          346 :   if (utils::end_with(path.string(), ".i"))
      38              :   {
      39          346 :     HITParser parser;
      40          346 :     auto inp = parser.parse(path, additional_input);
      41          688 :     return std::make_unique<Factory>(inp);
      42          346 :   }
      43              :   else
      44            0 :     throw ParserException("Unsupported parser type");
      45              : }
      46              : 
      47          345 : Factory::Factory(InputFile inp)
      48          345 :   : _input_file(std::move(inp)),
      49          345 :     _objects()
      50              : {
      51          345 : }
      52              : 
      53              : bool
      54            0 : Factory::has_object(const std::string & section, const std::string & name)
      55              : {
      56            0 :   return _objects.count(section) && _objects.at(section).count(name);
      57              : }
      58              : 
      59              : void
      60         1475 : Factory::create_object(const std::string & section, const OptionSet & options)
      61              : {
      62         1475 :   const std::string & name = options.name();
      63         1475 :   const std::string & type = options.type();
      64              : 
      65         1475 :   auto build = Registry::info(type).build;
      66         1475 :   auto object = (*build)(options);
      67         1473 :   _objects[section][name].push_back(object);
      68              : 
      69              :   try
      70              :   {
      71         1473 :     object->setup();
      72              :   }
      73            0 :   catch (const std::exception & e)
      74              :   {
      75            0 :     throw FactoryException("Failed to setup object '" + name + "' of type '" + type +
      76            0 :                            "' in section '" + section + "':\n" + e.what());
      77            0 :   }
      78         1473 : }
      79              : 
      80              : bool
      81            6 : Factory::options_compatible(const std::shared_ptr<NEML2Object> & obj, const OptionSet & opts) const
      82              : {
      83            6 :   return neml2::options_compatible(obj->input_options(), opts);
      84              : }
      85              : 
      86              : // LCOV_EXCL_START
      87              : void
      88              : Factory::print(std::ostream & os)
      89              : {
      90              :   const auto & all_objects = _objects;
      91              :   for (const auto & [section, objects] : all_objects)
      92              :   {
      93              :     os << "- " << section << ":" << std::endl;
      94              :     for (const auto & object : objects)
      95              :       os << "   " << object.first << ": " << object.second.size() << std::endl;
      96              :   }
      97              : }
      98              : // LCOV_EXCL_STOP
      99              : 
     100              : void
     101            0 : Factory::clear()
     102              : {
     103            0 :   _objects.clear();
     104            0 : }
     105              : } // namespace neml2
        

Generated by: LCOV version 2.0-1