Files
IfcOpenShell/src/ifcparse/IfcBaseClass.h
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
5.0 KiB
C++
Raw Normal View History

2017-06-05 17:26:58 +02:00
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef IFCBASECLASS_H
#define IFCBASECLASS_H
#include "ifc_parse_api.h"
#include "../ifcparse/IfcEntityInstanceData.h"
2017-12-11 14:20:30 +01:00
#include "../ifcparse/IfcSchema.h"
#include "../ifcparse/utils.h"
#include <boost/shared_ptr.hpp>
2017-06-05 17:26:58 +02:00
2021-09-11 12:59:48 +02:00
#include <atomic>
2017-06-05 17:26:58 +02:00
class Argument;
2021-07-29 14:54:51 +02:00
class aggregate_of_instance;
2017-06-05 17:26:58 +02:00
namespace IfcUtil {
2021-09-06 18:52:37 +02:00
class IFC_PARSE_API IfcBaseInterface {
protected:
static bool is_null(const IfcBaseInterface* not_this) {
2018-09-19 14:05:19 +02:00
return !not_this;
}
2021-09-06 18:52:37 +02:00
2022-02-10 13:05:05 +01:00
template<typename T>
std::enable_if_t<!std::is_same<IfcBaseClass, T>::value && !std::is_same<IfcBaseEntity, T>::value && !std::is_same<IfcBaseType, T>::value> raise_error_on_concrete_class() const {
throw IfcParse::IfcException("Instance of type " + this->declaration().name() + " cannot be cast to " + T::Class().name());
}
template<typename T>
std::enable_if_t<std::is_same<IfcBaseClass, T>::value || std::is_same<IfcBaseEntity, T>::value || std::is_same<IfcBaseType, T>::value> raise_error_on_concrete_class() const {
throw IfcParse::IfcException("Instance of type " + this->declaration().name() + " cannot be cast to base class");
}
2017-06-05 17:26:58 +02:00
public:
2021-09-06 18:52:37 +02:00
virtual const IfcEntityInstanceData& data() const = 0;
virtual IfcEntityInstanceData& data() = 0;
virtual const IfcParse::declaration& declaration() const = 0;
2017-06-05 17:26:58 +02:00
template <class T>
2022-02-10 13:05:05 +01:00
T* as(bool do_throw = false) {
2018-09-19 14:05:19 +02:00
// @todo: do not allow this to be null in the first place
if (is_null(this)) {
2018-08-13 11:43:28 +02:00
return static_cast<T*>(0);
}
2022-02-10 13:05:05 +01:00
auto t = dynamic_cast<T*>(this);
if (do_throw && !t) {
raise_error_on_concrete_class<T>();
}
return t;
2017-06-05 17:26:58 +02:00
}
template <class T>
2022-02-10 13:05:05 +01:00
const T* as(bool do_throw = false) const {
2018-09-19 14:05:19 +02:00
if (is_null(this)) {
2018-08-13 11:43:28 +02:00
return static_cast<const T*>(0);
}
2022-02-10 13:05:05 +01:00
auto t = dynamic_cast<const T*>(this);
if (do_throw && !t) {
raise_error_on_concrete_class<T>();
}
return t;
2017-06-05 17:26:58 +02:00
}
};
2021-09-06 18:52:37 +02:00
class IFC_PARSE_API IfcBaseClass : public virtual IfcBaseInterface {
2021-09-11 12:59:48 +02:00
private:
uint32_t identity_;
static std::atomic_uint32_t counter_;
protected:
2021-09-06 18:52:37 +02:00
IfcEntityInstanceData* data_;
static bool is_null(const IfcBaseClass* not_this) {
return !not_this;
}
public:
2021-09-11 12:59:48 +02:00
IfcBaseClass() : identity_(counter_++), data_(0) {}
IfcBaseClass(IfcEntityInstanceData* d) : identity_(counter_++), data_(d) {}
2021-09-06 18:52:37 +02:00
virtual ~IfcBaseClass() { delete data_; }
const IfcEntityInstanceData& data() const { return *data_; }
IfcEntityInstanceData& data() { return *data_; }
void data(IfcEntityInstanceData* d);
virtual const IfcParse::declaration& declaration() const = 0;
2021-09-11 12:59:48 +02:00
uint32_t identity() const { return identity_; }
2021-09-06 18:52:37 +02:00
};
class IFC_PARSE_API IfcLateBoundEntity : public IfcBaseClass {
private:
const IfcParse::declaration* decl_;
public:
IfcLateBoundEntity(const IfcParse::declaration* decl, IfcEntityInstanceData* data) : IfcBaseClass(data), decl_(decl) {}
virtual const IfcParse::declaration& declaration() const {
return *decl_;
}
};
2017-06-05 17:26:58 +02:00
class IFC_PARSE_API IfcBaseEntity : public IfcBaseClass {
public:
IfcBaseEntity() : IfcBaseClass() {}
2017-12-11 14:20:30 +01:00
IfcBaseEntity(IfcEntityInstanceData* d) : IfcBaseClass(d) {}
virtual const IfcParse::entity& declaration() const = 0;
2017-12-11 14:47:30 +01:00
2017-12-20 12:40:15 +01:00
Argument* get(const std::string& name) const;
2021-07-29 14:54:51 +02:00
boost::shared_ptr<aggregate_of_instance> get_inverse(const std::string& a) const;
2017-06-05 17:26:58 +02:00
};
// TODO: Investigate whether these should be template classes instead
2017-12-11 14:20:30 +01:00
class IFC_PARSE_API IfcBaseType : public IfcBaseClass {
2017-06-05 17:26:58 +02:00
public:
IfcBaseType() : IfcBaseClass() {}
2017-12-11 14:20:30 +01:00
IfcBaseType(IfcEntityInstanceData* d) : IfcBaseClass(d) {}
virtual const IfcParse::declaration& declaration() const = 0;
2017-06-05 17:26:58 +02:00
};
}
#endif