-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathProperties-impl.h
160 lines (151 loc) · 4.9 KB
/
Properties-impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* \file
* \brief Implemenatiom of the template part of the class Properties.
*
* \copyright
* Copyright (c) 2012-2018, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*
*/
template <typename T>
PropertyVector<T>* Properties::createNewPropertyVector(
std::string const& name,
MeshItemType mesh_item_type,
std::size_t n_components)
{
std::map<std::string, PropertyVectorBase*>::const_iterator it(
_properties.find(name)
);
if (it != _properties.end()) {
ERR("A property of the name '%s' is already assigned to the mesh.",
name.c_str());
return nullptr;
}
auto entry_info(
_properties.insert(
std::make_pair(
name, new PropertyVector<T>(name, mesh_item_type, n_components)
)
)
);
return static_cast<PropertyVector<T>*>((entry_info.first)->second);
}
template <typename T>
PropertyVector<T>* Properties::createNewPropertyVector(
std::string const& name,
std::size_t n_prop_groups,
std::vector<std::size_t> const& item2group_mapping,
MeshItemType mesh_item_type,
std::size_t n_components)
{
// check if there is already a PropertyVector with the same name and
// mesh_item_type
std::map<std::string, PropertyVectorBase*>::const_iterator it(
_properties.find(name)
);
if (it != _properties.end()) {
ERR("A property of the name '%s' already assigned to the mesh.",
name.c_str());
return nullptr;
}
// check entries of item2group_mapping for consistence
for (std::size_t k(0); k<item2group_mapping.size(); k++) {
std::size_t const group_id (item2group_mapping[k]);
if (group_id >= n_prop_groups) {
ERR("The mapping to property %d for item %d is not in the correct range [0,%d).", group_id, k, n_prop_groups);
return nullptr;
}
}
auto entry_info(
_properties.insert(
std::pair<std::string, PropertyVectorBase*>(
name,
new PropertyVector<T>(n_prop_groups,
item2group_mapping, name, mesh_item_type, n_components)
)
)
);
return static_cast<PropertyVector<T>*>((entry_info.first)->second);
}
template <typename T>
bool Properties::existsPropertyVector(std::string const& name) const
{
auto it(_properties.find(name));
// Check that a PropertyVector with the approriate name exists.
if (it == _properties.end())
{
return false;
}
// Check that the PropertyVector has the correct data type.
return dynamic_cast<PropertyVector<T> const*>(it->second) != nullptr;
}
template <typename T>
PropertyVector<T> const* Properties::getPropertyVector(
std::string const& name) const
{
auto it(_properties.find(name));
if (it == _properties.end())
{
OGS_FATAL(
"The PropertyVector '%s' is not available in the mesh.",
name.c_str());
}
if (!dynamic_cast<PropertyVector<T> const*>(it->second))
{
OGS_FATAL(
"The PropertyVector '%s' has a different type than the requested "
"PropertyVector.",
name.c_str());
}
return dynamic_cast<PropertyVector<T> const*>(it->second);
}
template <typename T>
PropertyVector<T>* Properties::getPropertyVector(std::string const& name)
{
auto it(_properties.find(name));
if (it == _properties.end())
{
OGS_FATAL(
"A PropertyVector with the specified name '%s' is not available.",
name.c_str());
}
if (!dynamic_cast<PropertyVector<T>*>(it->second))
{
OGS_FATAL(
"The PropertyVector '%s' has a different type than the requested "
"PropertyVector.",
name.c_str());
}
return dynamic_cast<PropertyVector<T>*>(it->second);
}
template <typename T>
PropertyVector<T> const* Properties::getPropertyVector(
std::string const& name, MeshItemType const item_type,
int const n_components) const
{
auto const it = _properties.find(name);
if (it == _properties.end())
{
OGS_FATAL("A property with name '%s' does not exist.", name.c_str());
}
auto property = dynamic_cast<PropertyVector<T>*>(it->second);
if (property == nullptr)
{
OGS_FATAL("Could not cast property '%s' to given type.", name.c_str());
}
if (property->getMeshItemType() != item_type)
{
OGS_FATAL(
"The PropertyVector '%s' has a different type than requested. A "
"'%s' field is requested.",
name.c_str(), toString(item_type));
}
if (property->getNumberOfComponents() != n_components)
{
OGS_FATAL("'%s' does not have the right number of components.",
name.c_str());
}
return property;
}