b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2object.H
Go to the documentation of this file.
1//------------------------------------------------------------------------
2// b2object.H --
3//
4//
5// written by Mathias Doreille
6// Neda Ebrahimi Pour <neda.ebrahimipour@dlr.de>
7// Harald Klimach <harald.klimach@dlr.de>
8// Thomas Blome <thomas.blome@dlr.de>
9//
10// (c) 2004-2012 SMR Engineering & Development SA
11// 2502 Bienne, Switzerland
12//
13// (c) 2023 Deutsches Zentrum für Luft- und Raumfahrt (DLR) e.V.
14// Linder Höhe, 51147 Köln
15//
16// All Rights Reserved. Proprietary source code. The contents of
17// this file may not be disclosed to third parties, copied or
18// duplicated in any form, in whole or in part, without the prior
19// written permission of SMR.
20//------------------------------------------------------------------------
21
22#ifndef __B2OBJECT_H__
23#define __B2OBJECT_H__
24
25// This text is now in the B2000++ Programming Manual
26
182#include <map>
183#include <sstream>
184#include <string>
185
186#include "b2ppconfig.h"
187#include "utils/b2allocator.H"
188#include "utils/b2exception.H"
189#include "utils/b2module.H"
190#include "utils/b2util.H"
191
192namespace b2000 {
193
194class ObjectType;
195class Object;
196
197inline std::ostream& operator<<(std::ostream& o, const ObjectType& m);
198
204public:
206 const std::string& name_, const std::string& suffix = "",
207 const StringList& aliases = StringList(), Module& module_ = b2000_module,
208 ObjectType* base_type_ = nullptr, const char* doc_ = nullptr)
209 : name(name_),
210 suffix(suffix),
211 module(module_),
212 base_type(base_type_),
213 doc(doc_),
214 aliases(aliases) {
215 module.set_object_type(this, name + suffix, aliases, suffix);
216 }
217
218 virtual ~ObjectType() {}
219
222 const std::string get_name() const { return name + suffix; }
223
228 bool normalize_name(std::string& identifier) const {
229 bool matching = identifier == name;
230 auto alias = aliases.begin();
231 while (!matching && alias != aliases.end()) {
232 matching = (*alias == identifier);
233 alias++;
234 }
235 if (matching) { identifier = get_name(); }
236
237 return matching;
238 }
239
240 virtual const std::string get_cpp_name() const { return "??"; }
241
247 virtual Object* new_object() {
248 TypeError() << "This object type is incomplete." << THROW;
249 return nullptr;
250 }
251
259 virtual Object* new_object(Allocator& allocator) {
260 TypeError() << "This object type is incomplete." << THROW;
261 return nullptr;
262 }
263
265 virtual bool is_type(const Object* o) { return typeid(o) == typeid(const Object*); }
266
269 inline virtual bool is_subtype(const Object* o) { return true; }
270
285 virtual ObjectType* get_subtype(const std::string& name_, Module& module_ = b2000_module) {
286 return module_.get_object_type(name_);
287 }
288
289private:
290 const std::string name;
291 const std::string suffix;
292 Module& module;
293 ObjectType* base_type;
294 const char* doc;
295 StringList aliases;
296
297 using children_t = std::map<std::string, ObjectType*>;
298 children_t children;
299
300 // The copy and the heap allocation ob ObjectType are forbidden.
301 // These members are declared private.
302 ObjectType(const ObjectType&);
303};
304
305inline std::ostream& operator<<(std::ostream& o, const ObjectType& ot) {
306 return o << ot.get_name() << " (c++ type=" << ot.get_cpp_name() << ")";
307}
308
339template <typename OBJECT, typename BASE_OBJECT_TYPE = ObjectType>
340class ObjectTypeIncomplete : public BASE_OBJECT_TYPE {
341public:
343 const std::string& name_, const std::string& suffix,
344 const StringList& aliases = StringList(), Module& module_ = b2000_module,
345 ObjectType* base_type_ = nullptr, const char* doc_ = nullptr)
346 : BASE_OBJECT_TYPE(name_, suffix, aliases, module_, base_type_, doc_) {}
347
348 OBJECT* new_object() {
349 TypeError() << "Cannot create an object of type " << *this
350 << " because this type is incomplete." << THROW;
351 return 0;
352 }
353
354 OBJECT* new_object(Allocator& allocator) {
355 TypeError() << "Cannot create an object of type " << *this
356 << " because this type is incomplete." << THROW;
357 return 0;
358 }
359
360 const std::string get_cpp_name() const { return demangle(typeid(OBJECT)); }
361
362 bool is_type(const Object* o) { return typeid(o) == typeid(const OBJECT*); }
363
364 bool is_subtype(const Object* o) {
366 return false; // dynamic_cast<const OBJECT*>(o) != 0;
367 }
368
369 ObjectTypeIncomplete* get_subtype(const std::string& name_, Module& module_ = b2000_module) {
370 ObjectType* ot = module_.get_object_type(name_);
371 if (ot == nullptr) {
372 KeyError() << "The type " << name_ << " is not a type of the module \"" << module_
373 << "\"." << THROW;
374 }
375 ObjectTypeIncomplete* o = dynamic_cast<ObjectTypeIncomplete*>(ot);
376 if (o == 0) {
377 KeyError() << "The type " << *ot << " is not a subtype of the type " << *this << "."
378 << THROW;
379 }
380 return o;
381 }
382};
383
414template <typename OBJECT, typename BASE_OBJECT_TYPE = ObjectType>
415class ObjectTypeComplete : public BASE_OBJECT_TYPE {
416public:
418 const std::string& name_, const std::string& suffix,
419 const StringList& aliases = StringList(), Module& module_ = b2000_module,
420 ObjectType* base_type_ = nullptr, const char* doc_ = nullptr)
421 : BASE_OBJECT_TYPE(name_, suffix, aliases, module_, base_type_, doc_) {}
422
423 OBJECT* new_object() { return new OBJECT{}; }
424
425 OBJECT* new_object(Allocator& allocator) {
426 return new (allocator.allocate(sizeof(OBJECT))) OBJECT{};
427 }
428
429 const std::string get_cpp_name() const { return demangle(typeid(OBJECT)); }
430
431 bool is_type(const Object* o) { return typeid(o) == typeid(const OBJECT*); }
432
433 bool is_subtype(const Object* o) {
435 return false; // dynamic_cast<const OBJECT*>(o) != 0;
436 }
437
438 ObjectTypeComplete* get_subtype(const std::string& name_, Module& module_ = b2000_module) {
439 ObjectType* ot = module_.get_object_type(name_);
440 if (ot == nullptr) {
441 KeyError() << "The type " << name_ << " is not a type of the module \"" << module_
442 << "\"." << THROW;
443 }
444 ObjectTypeComplete* o = dynamic_cast<ObjectTypeComplete*>(ot);
445 if (o == 0) {
446 KeyError() << "The type " << *ot << " is not a subtype of the type " << *this << "."
447 << THROW;
448 }
449 return o;
450 }
451};
452
456class Object {
457public:
460 virtual const std::string& get_object_name() const { return unknown_object_name; }
461
464 virtual ~Object(){};
465
468 virtual void incref() const {}
469
472 virtual void decref() const {}
473
476 virtual void free() { this->~Object(); }
477
481 virtual void decref_or_free() const { this->~Object(); }
482
485
486private:
487 static std::string unknown_object_name;
488};
489
490inline std::string type_name(const std::string& s = "") { return s; }
491
492template <typename T1>
493inline std::string type_name(const std::string& s = "") {
494 return s + '<' + demangle(typeid(T1)) + '>';
495}
496
497template <typename T1, typename T2>
498inline std::string type_name(const std::string& s = "") {
499 return s + '<' + demangle(typeid(T1)) + "," + demangle(typeid(T2)) + '>';
500}
501
502template <typename T1, typename T2, typename T3>
503inline std::string type_name(const std::string& s = "") {
504 return s + '<' + demangle(typeid(T1)) + "," + demangle(typeid(T2)) + "," + demangle(typeid(T3))
505 + '>';
506}
507
508template <typename T1, typename T2, typename T3, typename T4>
509inline std::string type_name(const std::string& s = "") {
510 return s + '<' + demangle(typeid(T1)) + "," + demangle(typeid(T2)) + "," + demangle(typeid(T3))
511 + "," + demangle(typeid(T4)) + '>';
512}
513
514} // namespace b2000
515
516#endif
#define THROW
Definition b2exception.H:198
Definition b2allocator.H:63
Definition b2object.H:415
bool is_subtype(const Object *o)
Definition b2object.H:433
bool is_type(const Object *o)
Definition b2object.H:431
ObjectTypeComplete * get_subtype(const std::string &name_, Module &module_=b2000_module)
Definition b2object.H:438
OBJECT * new_object(Allocator &allocator)
Definition b2object.H:425
OBJECT * new_object()
Definition b2object.H:423
Definition b2object.H:340
bool is_type(const Object *o)
Definition b2object.H:362
ObjectTypeIncomplete * get_subtype(const std::string &name_, Module &module_=b2000_module)
Definition b2object.H:369
OBJECT * new_object()
Definition b2object.H:348
bool is_subtype(const Object *o)
Definition b2object.H:364
OBJECT * new_object(Allocator &allocator)
Definition b2object.H:354
Definition b2object.H:203
bool normalize_name(std::string &identifier) const
Definition b2object.H:228
const std::string get_name() const
Definition b2object.H:222
virtual Object * new_object(Allocator &allocator)
Definition b2object.H:259
virtual ObjectType * get_subtype(const std::string &name_, Module &module_=b2000_module)
Definition b2object.H:285
virtual Object * new_object()
Definition b2object.H:247
virtual bool is_type(const Object *o)
Definition b2object.H:265
virtual bool is_subtype(const Object *o)
Definition b2object.H:269
Definition b2object.H:456
virtual ~Object()
Definition b2object.H:464
static ObjectType type
Definition b2object.H:484
virtual const std::string & get_object_name() const
Definition b2object.H:460
Definition b2util.H:54
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32
GenericException< KeyError_name > KeyError
Definition b2exception.H:320
GenericException< TypeError_name > TypeError
Definition b2exception.H:325
GenericException< UnimplementedError_name > UnimplementedError
Definition b2exception.H:314