Files
IfcOpenShell/src/ifcparse/express.h
T

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

294 lines
8.9 KiB
C++
Raw Normal View History

/********************************************************************************
* *
* 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
2026-03-31 15:32:36 +02:00
#include "argument.h"
#include "ifc_parse_api.h"
2026-03-31 15:32:36 +02:00
#include "schema.h"
#include "utils.h"
#include <atomic>
#include <boost/shared_ptr.hpp>
class aggregate_of_instance;
2026-03-31 15:32:36 +02:00
namespace ifcopenshell {
#ifdef IFOPSH_SAFE_INSTANCE
using pointer_type = std::weak_ptr<instance_data>;
using shared_pointer_type = shared_pointer_type;
template <typename T, typename... Args>
shared_pointer_type make_pointer_type(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
#else
using pointer_type = instance_data*;
using shared_pointer_type = instance_data*;
template <typename T, typename... Args>
shared_pointer_type make_pointer_type(Args&&... args) {
return new T(std::forward<Args>(args)...);
}
#endif
2026-03-31 15:32:36 +02:00
class file;
namespace impl {
struct in_memory_file_storage;
}
2026-03-31 15:32:36 +02:00
} // namespace ifcopenshell
2026-03-31 15:32:36 +02:00
class instance_data;
class attribute_value;
namespace express {
2026-08-08 07:42:45 +02:00
class base;
class select;
class entity;
class declared_type;
2026-08-08 07:42:45 +02:00
class IFC_PARSE_API base {
protected:
ifcopenshell::pointer_type data_;
2026-03-31 15:32:36 +02:00
const instance_data* data() const;
instance_data* data();
public:
operator bool() const {
#ifdef IFOPSH_SAFE_INSTANCE
return !data_.expired();
#else
return data_ != nullptr;
#endif
2026-01-07 13:51:48 +01:00
}
2026-08-08 07:42:45 +02:00
bool operator<(const base& other) const {
2026-01-07 13:51:48 +01:00
return data() < other.data();
}
2026-08-08 07:42:45 +02:00
bool operator==(const base& other) const {
2026-01-07 13:51:48 +01:00
return data() == other.data();
}
2026-08-08 07:42:45 +02:00
bool operator!=(const base& other) const {
2026-01-07 13:51:48 +01:00
return !(*this == other);
}
2026-08-08 07:42:45 +02:00
base() {
#ifndef IFOPSH_SAFE_INSTANCE
data_ = nullptr;
#endif
};
2026-08-08 07:42:45 +02:00
base(std::nullopt_t) noexcept : base() {}
base(const ifcopenshell::pointer_type& data) : data_(data) {}
// @todo try and make this private over time too
const ifcopenshell::pointer_type& data_weak() const { return data_; }
2026-03-31 15:32:36 +02:00
const ifcopenshell::declaration& declaration() const;
template <typename T>
typename std::enable_if<
2026-08-08 07:42:45 +02:00
(!std::is_base_of_v<express::base, T> || std::is_same_v<express::base, T>),
void>::type
2026-03-31 18:23:13 +02:00
set_attribute_value(size_t attribute_index, const T& value);
template <typename T>
typename std::enable_if<
2026-08-08 07:42:45 +02:00
(!std::is_base_of_v<express::base, T> || std::is_same_v<express::base, T>),
void>::type
2026-03-31 18:23:13 +02:00
set_attribute_value(const std::string& attribute_name, const T& value);
2026-08-08 07:42:45 +02:00
void set_attribute_value(size_t attribute_index, const express::base& value);
void set_attribute_value(const std::string& attribute_name, const express::base& value);
2026-03-31 18:23:13 +02:00
void unset_attribute_value(size_t attribute_index);
2026-03-31 18:23:13 +02:00
attribute_value get_attribute_value(size_t attribute_index) const;
uint32_t identity() const;
uint32_t id() const;
2026-03-31 18:23:13 +02:00
void to_string(std::ostream& stream, bool uppercase = false) const;
template <class T>
T as() const {
2026-08-08 07:42:45 +02:00
if constexpr (std::is_same_v<entity, T>) {
if (declaration().as_entity() != nullptr) {
return T(data_weak());
} else {
return T{};
}
2026-08-08 07:42:45 +02:00
} else if constexpr (std::is_same_v<declared_type, T>) {
if (declaration().as_entity() == nullptr) {
return T(data_weak());
} else {
return T{};
}
2026-08-08 07:42:45 +02:00
} else if constexpr (std::is_same_v<select, T>) {
static_assert(std::is_same_v<select, T>, "select is abstract");
} else {
if (declaration().is(T::Class())) {
return T(data_weak());
} else {
return T{};
}
}
}
2026-03-31 15:32:36 +02:00
ifcopenshell::file* file() const;
};
2026-08-08 07:42:45 +02:00
class IFC_PARSE_API entity : public base {
public:
2026-08-08 07:42:45 +02:00
using base::base;
2026-03-31 18:23:13 +02:00
attribute_value get(const std::string& attribute_name) const;
template <typename T>
2026-03-31 18:23:13 +02:00
T get_value(const std::string& attribute_name) const;
template <typename T>
2026-03-31 18:23:13 +02:00
T get_value(const std::string& attribute_name, const T& default_value) const;
2026-08-08 07:42:45 +02:00
std::vector<express::entity> get_inverse(const std::string& attribute_name) const;
};
2026-08-08 07:42:45 +02:00
class IFC_PARSE_API select : public base {
public:
2026-08-08 07:42:45 +02:00
select() {}
select(std::nullopt_t) noexcept : base() {}
select(const ifcopenshell::pointer_type& data) : base(data) {}
select(const base& value) : base(value.data_weak()) {}
2026-08-08 07:42:45 +02:00
base concrete() const {
return base(data_weak());
}
};
// @todo Investigate whether these should be template classes instead
// @todo currently this class doesn't do much, decide whether to keep
2026-08-08 07:42:45 +02:00
// it or move certain functionality from base downwards to
// entity and declared_type
class IFC_PARSE_API declared_type : public base {
public:
2026-08-08 07:42:45 +02:00
using base::base;
};
2026-08-08 07:42:45 +02:00
// Compatibility aliases for checked-in generated schema sources. New generated
// sources use the snake_case names directly.
using Base = base;
using Entity = entity;
using Select = select;
using DeclaredType = declared_type;
} // namespace express
namespace std {
template <>
2026-08-08 07:42:45 +02:00
struct hash<express::base> {
std::size_t operator()(const express::base& value) const noexcept {
2026-03-31 18:23:13 +02:00
return std::hash<uint32_t>{}(value.identity());
}
};
template <>
2026-08-08 07:42:45 +02:00
struct hash<express::entity> {
std::size_t operator()(const express::entity& value) const noexcept {
2026-03-31 18:23:13 +02:00
return std::hash<uint32_t>{}(value.identity());
}
};
} // namespace std
2026-01-10 10:20:34 +01:00
namespace boost {
template <>
2026-08-08 07:42:45 +02:00
struct hash<express::base> {
std::size_t operator()(const express::base& value) const noexcept {
2026-03-31 18:23:13 +02:00
return std::hash<uint32_t>{}(value.identity());
2026-01-10 10:20:34 +01:00
}
};
template <>
2026-08-08 07:42:45 +02:00
struct hash<express::entity> {
std::size_t operator()(const express::entity& value) const noexcept {
2026-03-31 18:23:13 +02:00
return std::hash<uint32_t>{}(value.identity());
2026-01-10 10:20:34 +01:00
}
};
} // namespace boost
2026-03-26 10:39:29 +01:00
namespace {
template <typename>
struct is_std_vector : std::false_type {};
template <typename X, typename A>
struct is_std_vector<std::vector<X, A>> : std::true_type {};
template <typename T>
constexpr bool is_std_vector_v = is_std_vector<T>::value;
template <typename T>
struct is_std_vector_vector : std::false_type {};
template <typename T, typename Alloc, typename Alloc2>
struct is_std_vector_vector<std::vector<std::vector<T, Alloc>, Alloc2>> : std::true_type {};
template <typename T>
constexpr bool is_std_vector_vector_v = is_std_vector_vector<T>::value;
}
template <typename T, typename U>
typename std::conditional_t<
is_std_vector<U>::value,
std::vector<std::vector<T>>,
std::vector<T>>
2026-03-31 18:23:13 +02:00
cast_vector(const std::vector<U>& values) {
2026-03-26 10:39:29 +01:00
if constexpr (is_std_vector<U>::value) {
2026-08-08 07:42:45 +02:00
using value_type = typename U::value_type;
2026-03-26 10:39:29 +01:00
std::vector<std::vector<T>> result;
2026-03-31 18:23:13 +02:00
result.reserve(values.size());
for (const auto& value : values) {
result.push_back(cast_vector<T>(value));
2026-03-26 10:39:29 +01:00
}
return result;
} else {
std::vector<T> result;
2026-03-31 18:23:13 +02:00
for (const auto& value : values) {
2026-03-26 10:39:29 +01:00
if constexpr (std::is_base_of_v<T, U>) {
// For a base or identity transform we can just rely on static cast
2026-03-31 18:23:13 +02:00
result.push_back(value);
2026-08-08 07:42:45 +02:00
} else if constexpr (std::is_base_of_v<express::select, U> && std::is_same_v<T, express::base>) {
2026-03-26 10:39:29 +01:00
// From a select to concrete we simply call the appropriate method
2026-03-31 18:23:13 +02:00
result.push_back(value.concrete());
2026-03-26 10:39:29 +01:00
} else {
2026-03-31 18:23:13 +02:00
if (auto cast_value = value.template as<T>()) {
result.push_back(cast_value);
2026-03-26 10:39:29 +01:00
}
}
}
return result;
}
}
#endif