v0.16.3
Loading...
Searching...
No Matches
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#ifndef __MOFEMUNKNOWNINTERFACE_HPP__
8#define __MOFEMUNKNOWNINTERFACE_HPP__
9
10namespace MoFEM {
11
12template <int N> struct CoreTmp;
13
14/** Create an owned interface. Specialize for interfaces with hidden
15 * implementations; declare the specialization in the interface header.
16 * The referenced Core must outlive the returned interface.
17 */
18template <class IFACE> IFACE *createInterface(const CoreTmp<0> &core) {
19 return new IFACE(core);
20}
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 SETERRQ(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 reference 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 * // Access 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 = dynamic_cast<IFACE *>(ptr)))
108 SETERRQ(PETSC_COMM_SELF, MOFEM_IMPOSSIBLE_CASE,
109 "Cast impossible %s -> %s",
110 (boost::typeindex::type_id_runtime(*ptr).pretty_name()).c_str(),
111 (boost::typeindex::type_id<IFACE>().pretty_name()).c_str());
113 }
114
115 /**
116 * @brief Get interface pointer to pointer of interface
117 *
118 * \code
119 * // Create moab database
120 * moab::Core mb_instance;
121 * // Access moab database by interface
122 * moab::Interface &moab = mb_instance;
123 *
124 * // Create MoFEM database
125 * MoFEM::Core core(moab);
126 * // Access MoFEM database by Interface
127 * MoFEM::Interface &m_field = core;
128 *
129 * // Get interface
130 * // Get simple interface is simplified version enabling quick and
131 * // easy construction of problem.
132 * Simple *simple_interface;
133 * // Query interface and get pointer to Simple interface
134 * CHKERR m_field.getInterface(&simple_interface);
135 *
136 * \endcode
137 *
138 *
139 * @param iface const pointer to a interface pointer
140 * @return MoFEMErrorCode
141 */
142 template <class IFACE>
143 inline MoFEMErrorCode getInterface(IFACE **const iface) const {
144 return getInterface<IFACE>(*iface);
145 }
146
147 /**
148 * @brief Get interface pointer to pointer of interface
149 *
150 * \code
151 * // Create moab database
152 * moab::Core mb_instance;
153 * // Access moab database by interface
154 * moab::Interface &moab = mb_instance;
155 *
156 * // Create MoFEM database
157 * MoFEM::Core core(moab);
158 * // Access MoFEM database by Interface
159 * MoFEM::Interface &m_field = core;
160 *
161 * // Get interface
162 * // Get simple interface is simplified version enabling quick and
163 * // easy construction of problem.
164 * Simple *simple_interface = m_field.getInterface<Simple*>();
165 *
166 * \endcode
167 *
168 * @return IFACE*
169 */
170 template <class IFACE,
171 typename boost::enable_if<boost::is_pointer<IFACE>, int>::type = 0>
172 inline IFACE getInterface() const {
173 typedef typename boost::remove_pointer<IFACE>::type IFaceType;
174 IFaceType *iface = NULL;
175 ierr = getInterface<IFACE>(iface);
176 CHKERRABORT(PETSC_COMM_SELF, ierr);
177 return iface;
178 }
179
180 /**
181 * @brief Get reference to interface
182 *
183 * \code
184 * // Create moab database
185 * moab::Core mb_instance;
186 * // Access moab database by interface
187 * moab::Interface &moab = mb_instance;
188 *
189 * // Create MoFEM database
190 * MoFEM::Core core(moab);
191 * // Access MoFEM database by Interface
192 * MoFEM::Interface &m_field = core;
193 *
194 * // Get interface
195 * // Get simple interface is simplified version enabling quick and
196 * // easy construction of problem.
197 * Simple &simple_interface = m_field.getInterface<Simple&>();
198 *
199 * \endcode
200 *
201 * @return IFACE&
202 */
203 template <class IFACE, typename boost::enable_if<boost::is_reference<IFACE>,
204 int>::type = 0>
205 inline IFACE getInterface() const {
206 typedef typename boost::remove_reference<IFACE>::type IFaceType;
207 IFaceType *iface = NULL;
208 ierr = getInterface<IFaceType>(iface);
209 CHKERRABORT(PETSC_COMM_SELF, ierr);
210 return *iface;
211 }
212
213 /**
214 * @brief Function returning pointer to interface
215 *
216 * \code
217 * // Create moab database
218 * moab::Core mb_instance;
219 * // Access moab database by interface
220 * moab::Interface &moab = mb_instance;
221 *
222 * // Create MoFEM database
223 * MoFEM::Core core(moab);
224 * // Access MoFEM database by Interface
225 * MoFEM::Interface &m_field = core;
226 *
227 * // Get interface
228 * // Get simple interface is simplified version enabling quick and
229 * // easy construction of problem.
230 * Simple *simple_interface = m_field.getInterface<Simple>();
231 *
232 * \endcode
233 *
234 * @return IFACE*
235 */
236 template <class IFACE> inline IFACE *getInterface() const {
237 IFACE *iface = NULL;
238 ierr = getInterface<IFACE>(iface);
239 CHKERRABORT(PETSC_COMM_SELF, ierr);
240 return iface;
241 }
242
243 virtual ~UnknownInterface() = default;
244
245 /**
246 * \brief Get library version
247 *
248 * This is library version.
249 *
250 * @return error code
251 */
252 static MoFEMErrorCode getLibVersion(Version &version);
253
254 /**
255 * \brief Get database major version
256 *
257 * This is database version. MoFEM can read DataBase from file created by
258 * older version. Then library version and database version could be
259 * different.
260 *
261 * @return error code
262 */
263 static MoFEMErrorCode getFileVersion(moab::Interface &moab, Version &version);
264
265 /**
266 * \brief Get database major version
267 *
268 * This is database version. MoFEM can read DataBase from file created by
269 * older version. Then library version and database version could be
270 * different.
271 *
272 * @return error code
273 */
275 moab::Interface &moab,
276 Version version = Version(MoFEM_VERSION_MAJOR, MoFEM_VERSION_MINOR,
277 MoFEM_VERSION_BUILD));
278
279 /**
280 * \brief Get database major version
281 *
282 * Implementation of particular interface could be different than main lib.
283 * For example user could use older interface, to keep back compatibility.
284 *
285 * @return error code
286 */
288
289protected:
290 struct NotKnownClass {};
291
292private:
293 struct UIdTypeMap {
294 boost::typeindex::type_index classIdx;
295 UIdTypeMap(const boost::typeindex::type_index &idx) : classIdx(idx) {}
296 };
297
298 /// Data structure for interfaces Id and class names
299 typedef multi_index_container<
300 UIdTypeMap,
301 indexed_by<
302
303 hashed_unique<member<UIdTypeMap, boost::typeindex::type_index,
305
306 >>
308
310 iFaceTypeMap; ///< Maps implementation to interface type name
311};
312
313} // namespace MoFEM
314
315#endif // __MOFEMUNKNOWNINTERFACE_HPP__
std::string type
std::string name
#define MoFEMFunctionReturnHot(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_IMPOSSIBLE_CASE
Definition definitions.h:35
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
#define MoFEMFunctionBeginHot
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
const double v
phase velocity of light in medium (cm/ns)
static MoFEMErrorCodeGeneric< PetscErrorCode > ierr
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
IFACE * createInterface(const CoreTmp< 0 > &core)
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 reference 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)