/******************************************************************************** * * * 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 . * * * ********************************************************************************/ #ifndef IFCBASECLASS_H #define IFCBASECLASS_H #include "argument.h" #include "ifc_parse_api.h" #include "schema.h" #include "utils.h" #include #include class aggregate_of_instance; namespace ifcopenshell { #ifdef IFOPSH_SAFE_INSTANCE using pointer_type = std::weak_ptr; using shared_pointer_type = shared_pointer_type; template shared_pointer_type make_pointer_type(Args&&... args) { return std::make_shared(std::forward(args)...); } #else using pointer_type = instance_data*; using shared_pointer_type = instance_data*; template shared_pointer_type make_pointer_type(Args&&... args) { return new T(std::forward(args)...); } #endif class file; namespace impl { struct in_memory_file_storage; } } // namespace ifcopenshell namespace ifcopenshell { class instance_data; class attribute_value; } namespace express { class base; class select; class entity; class declared_type; class IFC_PARSE_API base { protected: ifcopenshell::pointer_type data_; const ifcopenshell::instance_data* data() const; ifcopenshell::instance_data* data(); public: operator bool() const { #ifdef IFOPSH_SAFE_INSTANCE return !data_.expired(); #else return data_ != nullptr; #endif } bool operator<(const base& other) const { return data() < other.data(); } bool operator==(const base& other) const { return data() == other.data(); } bool operator!=(const base& other) const { return !(*this == other); } base() { #ifndef IFOPSH_SAFE_INSTANCE data_ = nullptr; #endif }; 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_; } const ifcopenshell::declaration& declaration() const; template typename std::enable_if< (!std::is_base_of_v || std::is_same_v), void>::type set_attribute_value(size_t attribute_index, const T& value); template typename std::enable_if< (!std::is_base_of_v || std::is_same_v), void>::type set_attribute_value(const std::string& attribute_name, const T& value); 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); void unset_attribute_value(size_t attribute_index); ifcopenshell::attribute_value get_attribute_value(size_t attribute_index) const; uint32_t identity() const; uint32_t id() const; void to_string(std::ostream& stream, bool uppercase = false) const; template T as() const { if constexpr (std::is_same_v) { if (declaration().as_entity() != nullptr) { return T(data_weak()); } else { return T{}; } } else if constexpr (std::is_same_v) { if (declaration().as_entity() == nullptr) { return T(data_weak()); } else { return T{}; } } else if constexpr (std::is_same_v) { static_assert(std::is_same_v, "select is abstract"); } else { if (declaration().is(T::Class())) { return T(data_weak()); } else { return T{}; } } } ifcopenshell::file* file() const; }; class IFC_PARSE_API entity : public base { public: using base::base; ifcopenshell::attribute_value get(const std::string& attribute_name) const; template T get_value(const std::string& attribute_name) const; template T get_value(const std::string& attribute_name, const T& default_value) const; std::vector get_inverse(const std::string& attribute_name) const; }; class IFC_PARSE_API select : public base { public: select() {} select(std::nullopt_t) noexcept : base() {} select(const ifcopenshell::pointer_type& data) : base(data) {} select(const base& value) : base(value.data_weak()) {} 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 // it or move certain functionality from base downwards to // entity and declared_type class IFC_PARSE_API declared_type : public base { public: using base::base; }; // 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 <> struct hash { std::size_t operator()(const express::base& value) const noexcept { return std::hash{}(value.identity()); } }; template <> struct hash { std::size_t operator()(const express::entity& value) const noexcept { return std::hash{}(value.identity()); } }; } // namespace std namespace boost { template <> struct hash { std::size_t operator()(const express::base& value) const noexcept { return std::hash{}(value.identity()); } }; template <> struct hash { std::size_t operator()(const express::entity& value) const noexcept { return std::hash{}(value.identity()); } }; } // namespace boost namespace { template struct is_std_vector : std::false_type {}; template struct is_std_vector> : std::true_type {}; template constexpr bool is_std_vector_v = is_std_vector::value; template struct is_std_vector_vector : std::false_type {}; template struct is_std_vector_vector, Alloc2>> : std::true_type {}; template constexpr bool is_std_vector_vector_v = is_std_vector_vector::value; } template typename std::conditional_t< is_std_vector::value, std::vector>, std::vector> cast_vector(const std::vector& values) { if constexpr (is_std_vector::value) { std::vector> result; result.reserve(values.size()); for (const auto& value : values) { result.push_back(cast_vector(value)); } return result; } else { std::vector result; for (const auto& value : values) { if constexpr (std::is_base_of_v) { // For a base or identity transform we can just rely on static cast result.push_back(value); } else if constexpr (std::is_base_of_v && std::is_same_v) { // From a select to concrete we simply call the appropriate method result.push_back(value.concrete()); } else { if (auto cast_value = value.template as()) { result.push_back(cast_value); } } } return result; } } #endif