v0.13.1
UnknownInterface.hpp
Go to the documentation of this file.
1/** \file UnknownInterface.hpp
2 * \brief MoFEM interface
3 *
4 * Low level data structures not used directly by user
5 */
6
7/*
8 * MoFEM is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 * License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with MoFEM. If not, see <http://www.gnu.org/licenses/>
15 */
16
17#ifndef __MOFEMUNKNOWNINTERFACE_HPP__
18#define __MOFEMUNKNOWNINTERFACE_HPP__
19
20namespace MoFEM {
21
22struct Version {
27 : majorVersion(MoFEM_VERSION_MAJOR), minorVersion(MoFEM_VERSION_MINOR),
28 buildVersion(MoFEM_VERSION_BUILD) {}
29 Version(const int v[3])
30 : majorVersion(v[0]), minorVersion(v[1]), buildVersion(v[2]) {}
31 Version(const int minor, const int major, const int build)
32 : majorVersion(minor), minorVersion(major), buildVersion(build) {}
33
34 std::string strVersion() {
35 auto str = [](auto v) { return boost::lexical_cast<std::string>(v); };
36 return str(majorVersion) + "." + str(minorVersion) + "." +
37 str(buildVersion);
38 }
39};
40
41/** \brief base class for all interface classes
42 * \ingroup mofem
43 **/
45
46 virtual MoFEMErrorCode
47 query_interface(boost::typeindex::type_index type_index,
48 UnknownInterface **iface) const = 0;
49
50 /**
51 * @brief Register interface
52 *
53 * Example:
54 * \code
55 * ierr = regSubInterface<Simple>(IDD_MOFEMSimple);
56 * CHKERRABORT(PETSC_COMM_SELF, ierr);
57 * \endcode
58 *
59 * @param uuid
60 * @param true
61 * @return MoFEMErrorCode
62 */
63 template <class IFACE>
64 MoFEMErrorCode registerInterface(bool error_if_registration_failed = true) {
66 auto p =
67 iFaceTypeMap.insert(UIdTypeMap(boost::typeindex::type_id<IFACE>()));
68 if (error_if_registration_failed && (!p.second)) {
69 SETERRQ1(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
70 "Registration of interface typeid(IFACE).name() = %s failed",
71 typeid(IFACE).name());
72 }
74 }
75
76 /**
77 * @brief Get interface refernce to pointer of interface
78 *
79 * \code
80 * // Create moab database
81 * moab::Core mb_instance;
82 * // Access moab database by interface
83 * moab::Interface &moab = mb_instance;
84 *
85 * // Create MoFEM database
86 * MoFEM::Core core(moab);
87 * // Acces MoFEM database by Interface
88 * MoFEM::Interface &m_field = core;
89 *
90 * // Get interface
91 * // Get simple interface is simplified version enabling quick and
92 * // easy construction of problem.
93 * Simple *simple_interface;
94 * // Query interface and get pointer to Simple interface
95 * CHKERR m_field.getInterface(simple_interface);
96 *
97 * \endcode
98 *
99 * @param iface reference to a interface pointer
100 * @return MoFEMErrorCode
101 */
102 template <class IFACE>
103 inline MoFEMErrorCode getInterface(IFACE *&iface) const {
105 UnknownInterface *ptr;
106 CHKERR query_interface(boost::typeindex::type_id<IFACE>(), &ptr);
107 if (!(iface = static_cast<IFACE *>(ptr)))
108 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSIBLE_CASE, "Cast imposible");
110 }
111
112 /**
113 * @brief Get interface pointer to pointer of interface
114 *
115 * \code
116 * // Create moab database
117 * moab::Core mb_instance;
118 * // Access moab database by interface
119 * moab::Interface &moab = mb_instance;
120 *
121 * // Create MoFEM database
122 * MoFEM::Core core(moab);
123 * // Acces MoFEM database by Interface
124 * MoFEM::Interface &m_field = core;
125 *
126 * // Get interface
127 * // Get simple interface is simplified version enabling quick and
128 * // easy construction of problem.
129 * Simple *simple_interface;
130 * // Query interface and get pointer to Simple interface
131 * CHKERR m_field.getInterface(&simple_interface);
132 *
133 * \endcode
134 *
135 *
136 * @param iface const pointer to a interface pointer
137 * @return MoFEMErrorCode
138 */
139 template <class IFACE>
140 inline MoFEMErrorCode getInterface(IFACE **const iface) const {
141 return getInterface<IFACE>(*iface);
142 }
143
144 /**
145 * @brief Get interface pointer to pointer of interface
146 *
147 * \code
148 * // Create moab database
149 * moab::Core mb_instance;
150 * // Access moab database by interface
151 * moab::Interface &moab = mb_instance;
152 *
153 * // Create MoFEM database
154 * MoFEM::Core core(moab);
155 * // Acces MoFEM database by Interface
156 * MoFEM::Interface &m_field = core;
157 *
158 * // Get interface
159 * // Get simple interface is simplified version enabling quick and
160 * // easy construction of problem.
161 * Simple *simple_interface = m_field.getInterface<Simple*>();
162 *
163 * \endcode
164 *
165 * @return IFACE*
166 */
167 template <class IFACE,
168 typename boost::enable_if<boost::is_pointer<IFACE>, int>::type = 0>
169 inline IFACE getInterface() const {
170 typedef typename boost::remove_pointer<IFACE>::type IFaceType;
171 IFaceType *iface = NULL;
172 ierr = getInterface<IFACE>(iface);
173 CHKERRABORT(PETSC_COMM_SELF, ierr);
174 return iface;
175 }
176
177 /**
178 * @brief Get reference to interface
179 *
180 * \code
181 * // Create moab database
182 * moab::Core mb_instance;
183 * // Access moab database by interface
184 * moab::Interface &moab = mb_instance;
185 *
186 * // Create MoFEM database
187 * MoFEM::Core core(moab);
188 * // Acces MoFEM database by Interface
189 * MoFEM::Interface &m_field = core;
190 *
191 * // Get interface
192 * // Get simple interface is simplified version enabling quick and
193 * // easy construction of problem.
194 * Simple &simple_interface = m_field.getInterface<Simple&>();
195 *
196 * \endcode
197 *
198 * @return IFACE&
199 */
200 template <class IFACE, typename boost::enable_if<boost::is_reference<IFACE>,
201 int>::type = 0>
202 inline IFACE getInterface() const {
203 typedef typename boost::remove_reference<IFACE>::type IFaceType;
204 IFaceType *iface = NULL;
205 ierr = getInterface<IFaceType>(iface);
206 CHKERRABORT(PETSC_COMM_SELF, ierr);
207 return *iface;
208 }
209
210 /**
211 * @brief Function returning pointer to interface
212 *
213 * \code
214 * // Create moab database
215 * moab::Core mb_instance;
216 * // Access moab database by interface
217 * moab::Interface &moab = mb_instance;
218 *
219 * // Create MoFEM database
220 * MoFEM::Core core(moab);
221 * // Acces MoFEM database by Interface
222 * MoFEM::Interface &m_field = core;
223 *
224 * // Get interface
225 * // Get simple interface is simplified version enabling quick and
226 * // easy construction of problem.
227 * Simple *simple_interface = m_field.getInterface<Simple>();
228 *
229 * \endcode
230 *
231 * @return IFACE*
232 */
233 template <class IFACE> inline IFACE *getInterface() const {
234 IFACE *iface = NULL;
235 ierr = getInterface<IFACE>(iface);
236 CHKERRABORT(PETSC_COMM_SELF, ierr);
237 return iface;
238 }
239
240 virtual ~UnknownInterface() = default;
241
242 /**
243 * \brief Get library version
244 *
245 * This is library version.
246 *
247 * @return error code
248 */
249 static MoFEMErrorCode getLibVersion(Version &version);
250
251 /**
252 * \brief Get database major version
253 *
254 * This is database version. MoFEM can read DataBase from file created by
255 * older version. Then library version and database version could be
256 * different.
257 *
258 * @return error code
259 */
261
262 /**
263 * \brief Get database major version
264 *
265 * This is database version. MoFEM can read DataBase from file created by
266 * older version. Then library version and database version could be
267 * different.
268 *
269 * @return error code
270 */
272 moab::Interface &moab,
273 Version version = Version(MoFEM_VERSION_MAJOR, MoFEM_VERSION_MINOR,
274 MoFEM_VERSION_BUILD));
275
276 /**
277 * \brief Get database major version
278 *
279 * Implementation of particular interface could be different than main lib.
280 * For example user could use older interface, to keep back compatibility.
281 *
282 * @return error code
283 */
285
286protected:
287 struct NotKnownClass {};
288
289private:
290 struct UIdTypeMap {
291 boost::typeindex::type_index classIdx;
292 UIdTypeMap(const boost::typeindex::type_index &idx) : classIdx(idx) {}
293 };
294
295 /// Data structure for interfaces Id and class names
296 typedef multi_index_container<
297 UIdTypeMap,
298 indexed_by<
299
300 hashed_unique<member<UIdTypeMap, boost::typeindex::type_index,
302
303 >>
305
307 iFaceTypeMap; ///< Maps implementation to interface type name
308};
309
310} // namespace MoFEM
311
312#endif // __MOFEMUNKNOWNINTERFACE_HPP__
static Index< 'p', 3 > p
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:460
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359
@ MOFEM_IMPOSIBLE_CASE
Definition: definitions.h:48
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition: definitions.h:47
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:429
#define CHKERR
Inline error check.
Definition: definitions.h:548
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:453
double v
phase velocity of light in medium (cm/ns)
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
Definition: Exceptions.hpp:87
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
Definition: Exceptions.hpp:67
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:21
DeprecatedCoreInterface Interface
Definition: Interface.hpp:1965
boost::typeindex::type_index classIdx
UIdTypeMap(const boost::typeindex::type_index &idx)
base class for all interface classes
multi_index_container< UIdTypeMap, indexed_by< hashed_unique< member< UIdTypeMap, boost::typeindex::type_index, &UIdTypeMap::classIdx > > > > iFaceTypeMap_multiIndex
Data structure for interfaces Id and class names.
iFaceTypeMap_multiIndex iFaceTypeMap
Maps implementation to interface type name.
IFACE * getInterface() const
Function returning pointer to interface.
virtual MoFEMErrorCode query_interface(boost::typeindex::type_index type_index, UnknownInterface **iface) const =0
static MoFEMErrorCode getLibVersion(Version &version)
Get library version.
MoFEMErrorCode registerInterface(bool error_if_registration_failed=true)
Register interface.
MoFEMErrorCode getInterface(IFACE **const iface) const
Get interface pointer to pointer of interface.
MoFEMErrorCode getInterface(IFACE *&iface) const
Get interface refernce to pointer of interface.
static MoFEMErrorCode setFileVersion(moab::Interface &moab, Version version=Version(MoFEM_VERSION_MAJOR, MoFEM_VERSION_MINOR, MoFEM_VERSION_BUILD))
Get database major version.
IFACE getInterface() const
Get interface pointer to pointer of interface.
static MoFEMErrorCode getInterfaceVersion(Version &version)
Get database major version.
static MoFEMErrorCode getFileVersion(moab::Interface &moab, Version &version)
Get database major version.
virtual ~UnknownInterface()=default
std::string strVersion()
Version(const int v[3])
Version(const int minor, const int major, const int build)