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 : #include <string> 13 : #include <unordered_map> 14 : #include <vector> 15 : #include <optional> 16 : 17 : #include "libmesh/simple_range.h" 18 : 19 : class MaterialPropertyStorage; 20 : 21 : /** 22 : * Registry class for material property IDs and names. 23 : * 24 : * Not thread safe. 25 : */ 26 : class MaterialPropertyRegistry 27 : { 28 : public: 29 : /** 30 : * @return True if a material property is registered with the name \p name 31 : */ 32 66670 : bool hasProperty(const std::string & name) const { return _name_to_id.count(name); } 33 : 34 : /** 35 : * @return True if a material property is registered with the ID \p id 36 : */ 37 852 : bool hasProperty(const unsigned int id) const { return id < _id_to_name.size(); } 38 : 39 : /** 40 : * Key that restricts writing data to the registry. 41 : */ 42 : class WriteKey 43 : { 44 : friend class MaterialPropertyStorage; 45 118873 : WriteKey() {} 46 : WriteKey(const WriteKey &) {} 47 : }; 48 : 49 : /** 50 : * @return The property ID for the given name, adding the property and 51 : * creating a new ID if it hasn't already been created. 52 : * 53 : * Protected with the MaterialPropertyRegistry::WriteKey. 54 : */ 55 : unsigned int addOrGetID(const std::string & name, const WriteKey); 56 : 57 : /** 58 : * @return The property ID for the property with the name \p name 59 : * 60 : * Will not create an ID if one does not exist, unlike addOrGetPropertyId 61 : */ 62 : unsigned int getID(const std::string & name) const; 63 : 64 : /** 65 : * @return The property ID for the property with the name \p name if a propery exists 66 : * with the name, otherwise an empty optional 67 : */ 68 : std::optional<unsigned int> queryID(const std::string & name) const; 69 : 70 : /** 71 : * @return The property name for the property with the ID \p id 72 : */ 73 : const std::string & getName(const unsigned int id) const; 74 : 75 : /** 76 : * @return A beginning iterator to the property ID to name map 77 : */ 78 165101 : auto idsToNamesBegin() const { return _id_to_name.begin(); } 79 : /** 80 : * @return An end iterator to the property ID to name map 81 : */ 82 165101 : auto idsToNamesEnd() const { return _id_to_name.end(); } 83 : /** 84 : * @return An iterator range to the property ID to name map 85 : */ 86 : auto idsToNamesRange() const { return libMesh::SimpleRange(idsToNamesBegin(), idsToNamesEnd()); } 87 : 88 : /** 89 : * @return The number of registered properties 90 : */ 91 153052 : std::size_t size() const { return _id_to_name.size(); } 92 : 93 : private: 94 : /// Map of material property name -> material property id 95 : std::unordered_map<std::string, unsigned int> _name_to_id; 96 : /// Map of material property id -> material property name 97 : std::vector<std::string> _id_to_name; 98 : };