Debian Codegraph @

packages/seacas/libraries/ioss/src/Ioss_ChangeSetFactory.C

raw
1 // Copyright(C) 2024 National Technology & Engineering Solutions
2 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 // NTESS, the U.S. Government retains certain rights in this software.
4 //
5 // See packages/seacas/LICENSE for details
6
7 #include "Ioss_ChangeSet.h"
8 #include "Ioss_ChangeSetFactory.h"
9 #include "Ioss_Utils.h" // for IOSS_ERROR
10 #include "Ioss_Version.h"
11 #include <fmt/format.h>
12 #include <fmt/ostream.h>
13 #include <fmt/ranges.h>
14 #include <map> // for _Rb_tree_iterator, etc
15 #include <ostream> // for basic_ostream, etc
16 #include <set>
17 #include <string> // for char_traits, string, etc
18
19 #include "Ioss_CodeTypes.h"
20 #include "Ioss_DBUsage.h" // for DatabaseUsage
21 #include "Ioss_PropertyManager.h"
22 #include "Ioss_Region.h"
23
24 namespace {
25 #if defined(IOSS_THREADSAFE)
26 std::mutex m_;
27 #endif
28
29 int describe_nl(Ioss::ChangeSetFactoryMap *registry, Ioss::NameList *names)
30 {
31 int count = 0;
32 Ioss::ChangeSetFactoryMap::const_iterator I;
33 for (I = registry->begin(); I != registry->end(); ++I) {
34 names->push_back((*I).first);
35 ++count;
36 }
37 return count;
38 }
39 } // namespace
40
41 namespace Ioss {
42 class ChangeSet;
43
44 using ChangeSetFactoryValuePair = ChangeSetFactoryMap::value_type;
45 } // namespace Ioss
46
47 const Ioss::ChangeSetFactory *Ioss::ChangeSetFactory::factory()
48 {
49 static Ioss::ChangeSetFactory registerThis("ioss");
50 return &registerThis;
51 }
52
53 std::shared_ptr<Ioss::ChangeSet> Ioss::ChangeSetFactory::create(Ioss::Region *region)
54 {
55 IOSS_FUNC_ENTER(m_);
56 std::string dbType = region->get_property("database_type").get_string();
57
58 auto iter = registry()->find(dbType);
59 if (iter == registry()->end()) {
60 if (registry()->empty()) {
61 std::ostringstream errmsg;
62 fmt::print(errmsg, "ERROR: No change set types have been registered.\n"
63 " Was Ioss::Init::Initializer() called?\n\n");
64 IOSS_ERROR(errmsg);
65 }
66 else {
67 iter = registry()->find("ioss");
68 }
69 }
70
71 Ioss::ChangeSetFactory *factory = (*iter).second;
72 Ioss::ChangeSet *csPtr = factory->make_ChangeSet(region);
73 std::shared_ptr<Ioss::ChangeSet> cs(csPtr);
74 return cs;
75 }
76
77 std::shared_ptr<Ioss::ChangeSet> Ioss::ChangeSetFactory::create(Ioss::DatabaseIO *db,
78 const std::string &dbName,
79 const std::string &dbType,
80 unsigned fileCyclicCount)
81 {
82 IOSS_FUNC_ENTER(m_);
83
84 auto iter = registry()->find(dbType);
85 if (iter == registry()->end()) {
86 if (registry()->empty()) {
87 std::ostringstream errmsg;
88 fmt::print(errmsg, "ERROR: No change set types have been registered.\n"
89 " Was Ioss::Init::Initializer() called?\n\n");
90 IOSS_ERROR(errmsg);
91 }
92 else {
93 iter = registry()->find("ioss");
94 }
95 }
96
97 Ioss::ChangeSetFactory *factory = (*iter).second;
98 Ioss::ChangeSet *csPtr = factory->make_ChangeSet(db, dbName, dbType, fileCyclicCount);
99 std::shared_ptr<Ioss::ChangeSet> cs(csPtr);
100 return cs;
101 }
102
103 Ioss::ChangeSet *Ioss::ChangeSetFactory::make_ChangeSet(Ioss::Region *region) const
104 {
105 return new ChangeSet(region);
106 }
107
108 Ioss::ChangeSet *Ioss::ChangeSetFactory::make_ChangeSet(Ioss::DatabaseIO *db,
109 const std::string &dbName,
110 const std::string &dbType,
111 unsigned fileCyclicCount) const
112 {
113 return new ChangeSet(db, dbName, dbType, fileCyclicCount);
114 }
115
116 /** \brief Get the names of change set types known to IOSS.
117 *
118 * \param[out] names The list of known change set types.
119 * \returns The number of known change set types.
120 */
121 int Ioss::ChangeSetFactory::describe(NameList *names)
122 {
123 IOSS_FUNC_ENTER(m_);
124 return ::describe_nl(registry(), names);
125 }
126
127 /** \brief Get the names of change set types known to IOSS.
128 *
129 * \returns The list of known change set types.
130 */
131 Ioss::NameList Ioss::ChangeSetFactory::describe()
132 {
133 Ioss::NameList names;
134 describe(&names);
135 return names;
136 }
137
138 Ioss::ChangeSetFactory::ChangeSetFactory(const std::string &type)
139 {
140 registry()->insert(ChangeSetFactoryValuePair(type, this));
141 }
142
143 void Ioss::ChangeSetFactory::alias(const std::string &base, const std::string &syn)
144 {
145 Ioss::ChangeSetFactory *factory = (*registry()->find(base)).second;
146 registry()->insert(ChangeSetFactoryValuePair(syn, factory));
147 }
148
149 Ioss::ChangeSetFactoryMap *Ioss::ChangeSetFactory::registry()
150 {
151 static ChangeSetFactoryMap registry_;
152 return &registry_;
153 }