2015-01-05 14:11:42 +00:00
|
|
|
/********************************************************************************
|
2011-09-25 09:53:22 +00: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/>. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
2015-01-05 14:11:42 +00:00
|
|
|
|
2011-09-25 09:53:22 +00:00
|
|
|
#ifndef IFCFILE_H
|
|
|
|
|
#define IFCFILE_H
|
|
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
#include "ifc_parse_api.h"
|
|
|
|
|
#include "IfcParse.h"
|
|
|
|
|
#include "IfcSchema.h"
|
|
|
|
|
#include "IfcSpfHeader.h"
|
2025-02-21 16:12:16 +01:00
|
|
|
#include "rocksdb_map_adapter.h"
|
|
|
|
|
#include "map_variant.h"
|
|
|
|
|
#include "map_transformer.h"
|
2021-09-11 12:59:48 +02:00
|
|
|
|
2021-08-12 13:35:45 +02:00
|
|
|
#include <boost/multi_index/ordered_index.hpp>
|
|
|
|
|
#include <boost/multi_index/random_access_index.hpp>
|
2023-09-17 12:30:17 +02:00
|
|
|
#include <boost/multi_index/sequenced_index.hpp>
|
|
|
|
|
#include <boost/multi_index_container.hpp>
|
|
|
|
|
#include <boost/unordered_map.hpp>
|
2024-08-23 20:29:07 +02:00
|
|
|
#include <boost/variant.hpp>
|
2023-09-17 12:30:17 +02:00
|
|
|
#include <iterator>
|
|
|
|
|
#include <map>
|
2011-09-25 09:53:22 +00:00
|
|
|
|
|
|
|
|
namespace IfcParse {
|
2015-01-05 14:11:42 +00:00
|
|
|
|
2021-02-15 11:11:14 +01:00
|
|
|
class IFC_PARSE_API file_open_status {
|
2023-09-17 12:30:17 +02:00
|
|
|
public:
|
|
|
|
|
enum file_open_enum {
|
|
|
|
|
SUCCESS,
|
|
|
|
|
READ_ERROR,
|
|
|
|
|
NO_HEADER,
|
2024-09-23 15:24:28 +02:00
|
|
|
UNSUPPORTED_SCHEMA,
|
|
|
|
|
INVALID_SYNTAX
|
2023-09-17 12:30:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
file_open_enum error_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
file_open_status(file_open_enum error)
|
|
|
|
|
: error_(error) {}
|
|
|
|
|
|
|
|
|
|
operator file_open_enum() const {
|
|
|
|
|
return error_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file_open_enum value() const {
|
|
|
|
|
return error_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator bool() const {
|
|
|
|
|
return error_ == SUCCESS;
|
|
|
|
|
}
|
2021-02-15 11:11:14 +01:00
|
|
|
};
|
|
|
|
|
|
2024-08-23 20:29:07 +02:00
|
|
|
typedef boost::variant<int, IfcUtil::IfcBaseClass*> reference_or_simple_type;
|
|
|
|
|
typedef std::list<std::pair<MutableAttributeValue, boost::variant<reference_or_simple_type, std::vector<reference_or_simple_type>, std::vector<std::vector<reference_or_simple_type>>>>> unresolved_references;
|
|
|
|
|
|
|
|
|
|
struct parse_context {
|
|
|
|
|
std::list<
|
|
|
|
|
boost::variant<
|
|
|
|
|
IfcUtil::IfcBaseClass*,
|
|
|
|
|
Token,
|
|
|
|
|
parse_context*
|
|
|
|
|
>> tokens_;
|
|
|
|
|
|
|
|
|
|
parse_context() {};
|
|
|
|
|
~parse_context();
|
|
|
|
|
|
|
|
|
|
parse_context(const parse_context&) = delete;
|
|
|
|
|
parse_context& operator=(const parse_context&) = delete;
|
|
|
|
|
|
|
|
|
|
parse_context(parse_context&&) = default;
|
|
|
|
|
parse_context& operator=(parse_context&&) = default;
|
|
|
|
|
|
|
|
|
|
parse_context& push();
|
|
|
|
|
|
|
|
|
|
void push(Token t);
|
|
|
|
|
|
|
|
|
|
void push(IfcUtil::IfcBaseClass* inst);
|
|
|
|
|
|
|
|
|
|
IfcEntityInstanceData construct(int name, unresolved_references& references_to_resolve, const IfcParse::declaration* decl, boost::optional<size_t> expected_size);
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
#include <variant>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
|
|
template <typename... Iterators>
|
|
|
|
|
class variant_iterator {
|
|
|
|
|
public:
|
|
|
|
|
// The variant type holding one of the underlying iterators.
|
|
|
|
|
using variant_type = std::variant<Iterators...>;
|
|
|
|
|
|
|
|
|
|
// Assuming that all iterator types have the same value_type, difference_type, etc.
|
|
|
|
|
using value_type = std::common_type_t<typename std::iterator_traits<Iterators>::value_type...>;
|
|
|
|
|
using difference_type = std::common_type_t<typename std::iterator_traits<Iterators>::difference_type...>;
|
|
|
|
|
using pointer = value_type*;
|
|
|
|
|
using reference = value_type&;
|
|
|
|
|
// For simplicity, we use input_iterator_tag; if all underlying iterators support more,
|
|
|
|
|
// you could compute the common iterator_category.
|
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
|
|
|
|
|
|
// Default constructor.
|
|
|
|
|
variant_iterator() = default;
|
|
|
|
|
|
|
|
|
|
// Construct from any one of the underlying iterator types.
|
|
|
|
|
template <typename Iterator>
|
|
|
|
|
variant_iterator(Iterator it) : it_(it) { }
|
|
|
|
|
|
|
|
|
|
// Dereference operator.
|
|
|
|
|
decltype(auto) operator*() const {
|
|
|
|
|
return std::visit([](const auto& iter) -> decltype(auto) {
|
|
|
|
|
return *iter;
|
|
|
|
|
}, it_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Arrow operator.
|
|
|
|
|
decltype(auto) operator->() const {
|
|
|
|
|
return std::visit([](const auto& iter) -> decltype(auto) {
|
|
|
|
|
return iter.operator->();
|
|
|
|
|
}, it_);
|
|
|
|
|
}
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
// Pre-increment operator.
|
|
|
|
|
variant_iterator& operator++() {
|
|
|
|
|
std::visit([](auto& iter) { ++iter; }, it_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
// Post-increment operator.
|
|
|
|
|
variant_iterator operator++(int) {
|
|
|
|
|
variant_iterator temp(*this);
|
|
|
|
|
++(*this);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
// Pre-decrement operator.
|
|
|
|
|
variant_iterator& operator--() {
|
|
|
|
|
std::visit([](auto& iter) { --iter; }, it_);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
// Post-decrement operator.
|
|
|
|
|
variant_iterator operator--(int) {
|
|
|
|
|
variant_iterator temp(*this);
|
|
|
|
|
--(*this);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
// Equality comparison.
|
|
|
|
|
friend bool operator==(const variant_iterator& lhs, const variant_iterator& rhs) {
|
|
|
|
|
return lhs.it_ == rhs.it_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inequality comparison.
|
|
|
|
|
friend bool operator!=(const variant_iterator& lhs, const variant_iterator& rhs) {
|
|
|
|
|
return !(lhs == rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
variant_type it_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
struct in_memory_file_storage {
|
|
|
|
|
IfcParse::IfcSpfLexer* tokens;
|
|
|
|
|
IfcParse::IfcSpfStream* stream;
|
|
|
|
|
|
|
|
|
|
IfcParse::IfcFile* file;
|
|
|
|
|
|
|
|
|
|
unresolved_references references_to_resolve;
|
|
|
|
|
|
|
|
|
|
typedef std::map<const IfcParse::declaration*, aggregate_of_instance::ptr> entities_by_type_t;
|
|
|
|
|
typedef boost::unordered_map<size_t, size_t> identity_by_id_t;
|
|
|
|
|
typedef boost::unordered_map<uint32_t, IfcUtil::IfcBaseClass*> entity_by_iden_t;
|
|
|
|
|
typedef std::map<std::string, IfcUtil::IfcBaseClass*> entity_by_guid_t;
|
|
|
|
|
typedef std::tuple<int, short, short> inverse_attr_record;
|
|
|
|
|
enum INVERSE_ATTR {
|
|
|
|
|
INSTANCE_ID,
|
|
|
|
|
INSTANCE_TYPE,
|
|
|
|
|
ATTRIBUTE_INDEX
|
|
|
|
|
};
|
|
|
|
|
typedef std::map<inverse_attr_record, std::vector<int>> entities_by_ref_t;
|
|
|
|
|
typedef std::map<int, std::vector<int>> entities_by_ref_excl_t;
|
|
|
|
|
typedef std::map<unsigned int, aggregate_of_instance::ptr> ref_map_t;
|
|
|
|
|
typedef map_transformer<identity_by_id_t, std::function<IfcUtil::IfcBaseClass* (size_t)>, std::function<size_t(IfcUtil::IfcBaseClass*)>> entity_by_id_t;
|
|
|
|
|
typedef entity_by_id_t::iterator iterator;
|
|
|
|
|
|
|
|
|
|
identity_by_id_t idenbyid_;
|
|
|
|
|
|
|
|
|
|
in_memory_file_storage()
|
|
|
|
|
: byid_(
|
|
|
|
|
&idenbyid_,
|
|
|
|
|
[this](size_t v) { return byidentity_[v]; },
|
|
|
|
|
[](IfcUtil::IfcBaseClass* inst) { return inst->identity(); }
|
|
|
|
|
)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
class type_iterator : private entities_by_type_t::const_iterator {
|
|
|
|
|
public:
|
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
|
using value_type = entities_by_type_t::key_type;
|
|
|
|
|
using difference_type = typename entities_by_type_t::const_iterator::difference_type;
|
|
|
|
|
using pointer = value_type const*;
|
|
|
|
|
using reference = value_type const&;
|
|
|
|
|
|
|
|
|
|
type_iterator() : entities_by_type_t::const_iterator() {};
|
|
|
|
|
|
|
|
|
|
type_iterator(const entities_by_type_t::const_iterator& iter)
|
|
|
|
|
: entities_by_type_t::const_iterator(iter) {};
|
|
|
|
|
|
|
|
|
|
entities_by_type_t::key_type const* operator->() const {
|
|
|
|
|
return &entities_by_type_t::const_iterator::operator->()->first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entities_by_type_t::key_type const& operator*() const {
|
|
|
|
|
return entities_by_type_t::const_iterator::operator*().first;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type_iterator& operator++() {
|
|
|
|
|
entities_by_type_t::const_iterator::operator++();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type_iterator operator++(int) {
|
|
|
|
|
type_iterator tmp(*this);
|
|
|
|
|
operator++();
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator!=(const type_iterator& other) const {
|
|
|
|
|
const entities_by_type_t::const_iterator& self_ = *this;
|
|
|
|
|
const entities_by_type_t::const_iterator& other_ = other;
|
|
|
|
|
return self_ != other_;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static bool guid_map_;
|
|
|
|
|
static bool guid_map() { return guid_map_; }
|
|
|
|
|
static void guid_map(bool b) { guid_map_ = b; }
|
|
|
|
|
|
|
|
|
|
entity_by_id_t byid_;
|
|
|
|
|
// this is for simple types
|
|
|
|
|
entity_by_iden_t byidentity_;
|
|
|
|
|
// entities_by_type_t bytype_;
|
|
|
|
|
entities_by_type_t bytype_excl_;
|
|
|
|
|
// entities_by_ref_t byref_;
|
|
|
|
|
entities_by_ref_t byref_excl_;
|
|
|
|
|
entity_by_guid_t byguid_;
|
|
|
|
|
|
|
|
|
|
void load(unsigned entity_instance_name, const IfcParse::entity* entity, parse_context&, int attribute_index = -1);
|
|
|
|
|
void try_read_semicolon() const;
|
|
|
|
|
|
|
|
|
|
void register_inverse(unsigned, const IfcParse::entity* from_entity, int inst_id, int attribute_index);
|
|
|
|
|
void unregister_inverse(unsigned, const IfcParse::entity* from_entity, IfcUtil::IfcBaseClass*, int attribute_index);
|
|
|
|
|
|
|
|
|
|
// @todo is this still used
|
|
|
|
|
IfcEntityInstanceData read(unsigned int index);
|
|
|
|
|
void read_from_stream(IfcParse::IfcSpfStream* stream, const IfcParse::schema_definition*& schema, unsigned int& max_id);
|
|
|
|
|
|
|
|
|
|
file_open_status good_ = file_open_status::SUCCESS;
|
|
|
|
|
|
|
|
|
|
IfcUtil::IfcBaseClass* instance_by_id(int id);
|
|
|
|
|
|
|
|
|
|
void add_type_ref(IfcUtil::IfcBaseClass* new_entity) {
|
|
|
|
|
auto ty = new_entity->declaration().as_entity();
|
|
|
|
|
if (bytype_excl_.find(ty) == bytype_excl_.end()) {
|
|
|
|
|
bytype_excl_[ty].reset(new aggregate_of_instance());
|
|
|
|
|
}
|
|
|
|
|
bytype_excl_[ty]->push(new_entity);
|
|
|
|
|
}
|
|
|
|
|
void remove_type_ref(IfcUtil::IfcBaseClass* new_entity) {
|
|
|
|
|
// @todo
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
void add_inverse_ref(IfcUtil::IfcBaseClass* new_entity) {
|
|
|
|
|
auto ty = new_entity->declaration().as_entity();
|
|
|
|
|
if (bytype_excl_.find(ty) == bytype_excl_.end()) {
|
|
|
|
|
bytype_excl_[ty].reset(new aggregate_of_instance());
|
|
|
|
|
}
|
|
|
|
|
bytype_excl_[ty]->push(new_entity);
|
|
|
|
|
}
|
|
|
|
|
void remove_inverse_ref(IfcUtil::IfcBaseClass* new_entity) {
|
|
|
|
|
// @todo
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
void process_deletion_inverse(IfcUtil::IfcBaseClass* inst);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct rocks_db_file_storage {
|
|
|
|
|
// to make sure that instance pointer are constant during file lifetime
|
|
|
|
|
// cache instances because we want stable pointers
|
|
|
|
|
// @todo this is silly, but we cannot have the same type, this should be just a pointer then on the IfcFile side?
|
|
|
|
|
typedef std::map<uint32_t, IfcUtil::IfcBaseClass*> entity_by_iden_cache_t;
|
|
|
|
|
entity_by_iden_cache_t instance_cache_;
|
|
|
|
|
|
|
|
|
|
// lookup id->identity
|
|
|
|
|
typedef rocksdb_map_adapter<size_t, size_t> identity_by_id_t;
|
|
|
|
|
identity_by_id_t byid_;
|
|
|
|
|
|
|
|
|
|
// typedef map_transformer<rocksdb_map_adapter<size_t, size_t>, std::function<IfcUtil::IfcBaseClass*(size_t)>, std::function<size_t(IfcUtil::IfcBaseClass*)>> entity_by_identity_t;
|
|
|
|
|
// storage is now Instance name -> Identity -> Pointer (cached)
|
|
|
|
|
// entity_by_identity_t byidentity_;
|
|
|
|
|
|
|
|
|
|
// index in schema to binary serialized ids
|
|
|
|
|
typedef rocksdb_map_adapter<size_t, std::string> instance_id_str_by_type_t;
|
|
|
|
|
instance_id_str_by_type_t bytype_;
|
|
|
|
|
|
|
|
|
|
// guid -> id
|
|
|
|
|
typedef rocksdb_map_adapter<std::string, size_t> instance_id_by_guid_str_t;
|
|
|
|
|
instance_id_by_guid_str_t byguid_internal_;
|
|
|
|
|
|
|
|
|
|
// guid -> id -> instance
|
|
|
|
|
typedef map_transformer<rocksdb_map_adapter<std::string, size_t>, std::function<IfcUtil::IfcBaseClass* (size_t)>, std::function< size_t(IfcUtil::IfcBaseClass*)>> entity_by_guid_t;
|
|
|
|
|
entity_by_guid_t byguid_;
|
|
|
|
|
|
|
|
|
|
rocksdb::DB* db;
|
|
|
|
|
IfcParse::IfcFile* file;
|
|
|
|
|
|
|
|
|
|
// @todo naming
|
|
|
|
|
rocks_db_file_storage(const std::string& filepath, IfcParse::IfcFile* ffile);
|
|
|
|
|
|
|
|
|
|
bool read_schema(const IfcParse::schema_definition*& schema) {
|
|
|
|
|
// @todo
|
|
|
|
|
schema = nullptr;
|
|
|
|
|
return true;
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
2025-02-21 16:12:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
IfcUtil::IfcBaseClass* assert_existance(size_t instanceId);
|
|
|
|
|
|
|
|
|
|
// @todo this could be another map_adapter?
|
|
|
|
|
class rocksdb_instance_iterator {
|
|
|
|
|
private:
|
|
|
|
|
rocksdb::Iterator* state_;
|
|
|
|
|
rocks_db_file_storage* storage_;
|
|
|
|
|
|
|
|
|
|
static constexpr char prefix_[] = "a|";
|
|
|
|
|
|
|
|
|
|
boost::optional<size_t> read_id_() const {
|
|
|
|
|
auto sv = state_->key().ToStringView();
|
|
|
|
|
auto ii = sv.find("|", 2);
|
|
|
|
|
if (ii != decltype(sv)::npos) {
|
|
|
|
|
char* pEnd;
|
|
|
|
|
long result = strtol(sv.data() + 2, &pEnd, 10);
|
|
|
|
|
if (*pEnd == '|') {
|
|
|
|
|
return (size_t)result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return boost::none;
|
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
rocksdb_instance_iterator()
|
|
|
|
|
: state_(nullptr)
|
|
|
|
|
, storage_(nullptr)
|
|
|
|
|
{}
|
|
|
|
|
rocksdb_instance_iterator(rocks_db_file_storage* fs)
|
|
|
|
|
: storage_(fs)
|
|
|
|
|
{
|
|
|
|
|
state_ = fs->db->NewIterator(rocksdb::ReadOptions());
|
|
|
|
|
state_->Seek(prefix_);
|
|
|
|
|
if (!state_->Valid() || !state_->key().starts_with(prefix_)) {
|
|
|
|
|
delete state_;
|
|
|
|
|
state_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rocksdb_instance_iterator& operator++() {
|
|
|
|
|
if (!state_) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
auto last_id = read_id_();
|
|
|
|
|
while (state_->Valid()) {
|
|
|
|
|
state_->Next();
|
|
|
|
|
// Stop if we've left the prefix range.
|
|
|
|
|
if (!state_->Valid() || !state_->key().starts_with(prefix_)) {
|
|
|
|
|
delete state_;
|
|
|
|
|
state_ = nullptr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (read_id_() != last_id) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
rocksdb_instance_iterator operator++(int) {
|
|
|
|
|
rocksdb_instance_iterator temp = *this;
|
|
|
|
|
++(*this);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
bool operator==(const rocksdb_instance_iterator& other) const {
|
|
|
|
|
if (state_ == nullptr && other.state_ == nullptr) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return read_id_() == other.read_id_();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator!=(const rocksdb_instance_iterator& other) const {
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IfcUtil::IfcBaseClass* operator*() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// @todo merge iterators (template?)
|
|
|
|
|
class rocksdb_types_iterator {
|
|
|
|
|
private:
|
|
|
|
|
rocksdb::Iterator* state_;
|
|
|
|
|
const rocks_db_file_storage* storage_;
|
|
|
|
|
|
|
|
|
|
static constexpr char prefix_[] = "t|";
|
|
|
|
|
|
|
|
|
|
boost::optional<size_t> read_id_() const {
|
|
|
|
|
auto sv = state_->key().ToStringView();
|
|
|
|
|
auto ii = sv.find("|", 2);
|
|
|
|
|
if (ii != decltype(sv)::npos) {
|
|
|
|
|
char* pEnd;
|
|
|
|
|
long result = strtol(sv.data() + 2, &pEnd, 10);
|
|
|
|
|
if (*pEnd == '|') {
|
|
|
|
|
return (size_t)result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return boost::none;
|
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
using iterator_category = std::forward_iterator_tag;
|
|
|
|
|
using value_type = const IfcParse::declaration*;
|
|
|
|
|
// @todo ?
|
|
|
|
|
using difference_type = ptrdiff_t;
|
|
|
|
|
using pointer = value_type const*;
|
|
|
|
|
using reference = value_type const&;
|
|
|
|
|
|
|
|
|
|
rocksdb_types_iterator()
|
|
|
|
|
: state_(nullptr)
|
|
|
|
|
, storage_(nullptr)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
rocksdb_types_iterator(const rocks_db_file_storage* fs)
|
|
|
|
|
: storage_(fs)
|
|
|
|
|
{
|
|
|
|
|
state_ = fs->db->NewIterator(rocksdb::ReadOptions());
|
|
|
|
|
state_->Seek(prefix_);
|
|
|
|
|
if (!state_->Valid() || !state_->key().starts_with(prefix_)) {
|
|
|
|
|
delete state_;
|
|
|
|
|
state_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rocksdb_types_iterator& operator++() {
|
|
|
|
|
if (!state_) {
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
auto last_id = read_id_();
|
|
|
|
|
while (state_->Valid()) {
|
|
|
|
|
state_->Next();
|
|
|
|
|
// Stop if we've left the prefix range.
|
|
|
|
|
if (!state_->Valid() || !state_->key().starts_with(prefix_)) {
|
|
|
|
|
delete state_;
|
|
|
|
|
state_ = nullptr;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (read_id_() != last_id) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rocksdb_types_iterator operator++(int) {
|
|
|
|
|
rocksdb_types_iterator temp = *this;
|
|
|
|
|
++(*this);
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator==(const rocksdb_types_iterator& other) const {
|
|
|
|
|
if (state_ == nullptr && other.state_ == nullptr) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return read_id_() == other.read_id_();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator!=(const rocksdb_types_iterator& other) const {
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IfcParse::declaration* operator*() const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using const_iterator = rocksdb_types_iterator;
|
|
|
|
|
|
|
|
|
|
void register_inverse(unsigned, const IfcParse::entity* from_entity, int inst_id, int attribute_index);
|
|
|
|
|
void unregister_inverse(unsigned, const IfcParse::entity* from_entity, IfcUtil::IfcBaseClass*, int attribute_index);
|
|
|
|
|
|
|
|
|
|
// @todo a bit hard as a map because of value_type being an aggregate
|
|
|
|
|
void add_type_ref(IfcUtil::IfcBaseClass* new_entity);
|
|
|
|
|
void remove_type_ref(IfcUtil::IfcBaseClass* new_entity);
|
|
|
|
|
|
|
|
|
|
IfcUtil::IfcBaseClass* instance_by_id(int id);
|
|
|
|
|
|
|
|
|
|
void process_deletion_inverse(IfcUtil::IfcBaseClass* inst);
|
2023-09-17 12:30:17 +02:00
|
|
|
};
|
2025-02-21 16:12:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum filetype {
|
|
|
|
|
ifcspf,
|
|
|
|
|
ifcxml,
|
|
|
|
|
rocksdb,
|
|
|
|
|
autodetect
|
|
|
|
|
};
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
/// This class provides access to the entity instances in an IFC file
|
|
|
|
|
/// The file takes ownership of instances added to this file and deletes them when the file is deleted.
|
|
|
|
|
class IFC_PARSE_API IfcFile {
|
|
|
|
|
public:
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
typedef std::map<uint32_t, IfcUtil::IfcBaseClass*> entity_entity_map_t;
|
|
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
// @todo determine the constness of things (probably needs to be all const, we don't want to overwrite)
|
|
|
|
|
// @todo we have variant_iterator and MapVariant, we probably need to retain only one?
|
|
|
|
|
public:
|
|
|
|
|
using const_iterator = variant_iterator<impl::in_memory_file_storage::iterator, impl::rocks_db_file_storage::const_iterator>;
|
|
|
|
|
using type_iterator = variant_iterator<impl::in_memory_file_storage::type_iterator, impl::rocks_db_file_storage::rocksdb_types_iterator>;
|
|
|
|
|
using storage_t = std::variant<std::monostate, impl::in_memory_file_storage, impl::rocks_db_file_storage>;
|
|
|
|
|
// @todo temporarily public for header
|
|
|
|
|
storage_t storage_;
|
|
|
|
|
private:
|
2023-09-17 12:30:17 +02:00
|
|
|
file_open_status good_ = file_open_status::SUCCESS;
|
|
|
|
|
|
|
|
|
|
const IfcParse::schema_definition* schema_;
|
|
|
|
|
const IfcParse::declaration* ifcroot_type_;
|
|
|
|
|
|
2023-11-06 20:38:19 +01:00
|
|
|
entity_entity_map_t entity_file_map_;
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
unsigned int max_id_;
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
IfcSpfHeader _header;
|
|
|
|
|
|
|
|
|
|
void setDefaultHeaderValues();
|
|
|
|
|
|
|
|
|
|
typedef boost::multi_index_container<
|
|
|
|
|
int,
|
|
|
|
|
boost::multi_index::indexed_by<
|
|
|
|
|
boost::multi_index::sequenced<>,
|
|
|
|
|
boost::multi_index::ordered_unique<
|
|
|
|
|
boost::multi_index::identity<int>>>>
|
|
|
|
|
batch_deletion_ids_t;
|
|
|
|
|
batch_deletion_ids_t batch_deletion_ids_;
|
|
|
|
|
bool batch_mode_ = false;
|
|
|
|
|
void process_deletion_();
|
|
|
|
|
|
|
|
|
|
public:
|
2017-12-12 10:33:24 +01:00
|
|
|
#ifdef USE_MMAP
|
2023-11-06 20:29:00 +01:00
|
|
|
IfcFile(const std::string& path, bool mmap = false);
|
2017-12-12 10:33:24 +01:00
|
|
|
#else
|
2025-02-21 16:12:16 +01:00
|
|
|
IfcFile(const std::string& path, filetype ty=ifcspf);
|
2017-12-12 10:33:24 +01:00
|
|
|
#endif
|
2023-11-06 20:29:00 +01:00
|
|
|
IfcFile(std::istream& stream, int length);
|
|
|
|
|
IfcFile(void* data, int length);
|
|
|
|
|
IfcFile(IfcParse::IfcSpfStream* stream);
|
2023-09-17 12:30:17 +02:00
|
|
|
IfcFile(const IfcParse::schema_definition* schema = IfcParse::schema_by_name("IFC4"));
|
|
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
~IfcFile() {
|
|
|
|
|
for (const auto& p : byidentity_) {
|
|
|
|
|
delete p.second;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
file_open_status good() const { return good_; }
|
|
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
/// Returns the first entity in the range of instances contained in the model,
|
|
|
|
|
/// in arbitrary order
|
|
|
|
|
auto begin() const {
|
|
|
|
|
return byid_.begin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the first entity in the range of instances contained in the model,
|
|
|
|
|
/// in arbitrary order
|
|
|
|
|
auto end() const {
|
|
|
|
|
return byid_.end();
|
|
|
|
|
}
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
type_iterator types_begin() const;
|
|
|
|
|
type_iterator types_end() const;
|
|
|
|
|
|
|
|
|
|
/// Returns all entities in the file that match the template argument.
|
|
|
|
|
/// NOTE: This also returns subtypes of the requested type, for example:
|
|
|
|
|
/// IfcWall will also return IfcWallStandardCase entities
|
|
|
|
|
template <class T>
|
|
|
|
|
typename T::list::ptr instances_by_type() {
|
|
|
|
|
aggregate_of_instance::ptr untyped_list = instances_by_type(&T::Class());
|
|
|
|
|
if (untyped_list) {
|
|
|
|
|
return untyped_list->as<T>();
|
|
|
|
|
}
|
2023-10-24 12:21:32 +02:00
|
|
|
return typename T::list::ptr(new typename T::list);
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
typename T::list::ptr instances_by_type_excl_subtypes() {
|
|
|
|
|
aggregate_of_instance::ptr untyped_list = instances_by_type_excl_subtypes(&T::Class());
|
|
|
|
|
if (untyped_list) {
|
|
|
|
|
return untyped_list->as<T>();
|
|
|
|
|
}
|
2023-10-24 12:21:32 +02:00
|
|
|
return typename T::list::ptr(new typename T::list);
|
2023-09-17 12:30:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns all entities in the file that match the positional argument.
|
|
|
|
|
/// NOTE: This also returns subtypes of the requested type, for example:
|
|
|
|
|
/// IfcWall will also return IfcWallStandardCase entities
|
|
|
|
|
aggregate_of_instance::ptr instances_by_type(const IfcParse::declaration*);
|
|
|
|
|
|
|
|
|
|
/// Returns all entities in the file that match the positional argument.
|
|
|
|
|
aggregate_of_instance::ptr instances_by_type_excl_subtypes(const IfcParse::declaration*);
|
|
|
|
|
|
|
|
|
|
/// Returns all entities in the file that match the positional argument.
|
|
|
|
|
/// NOTE: This also returns subtypes of the requested type, for example:
|
|
|
|
|
/// IfcWall will also return IfcWallStandardCase entities
|
2023-11-06 20:29:00 +01:00
|
|
|
aggregate_of_instance::ptr instances_by_type(const std::string& type);
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
/// Returns all entities in the file that match the positional argument.
|
2023-11-06 20:29:00 +01:00
|
|
|
aggregate_of_instance::ptr instances_by_type_excl_subtypes(const std::string& type);
|
2017-12-13 18:05:10 +01:00
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
/// Returns all entities in the file that reference the id
|
|
|
|
|
aggregate_of_instance::ptr instances_by_reference(int id);
|
2018-08-29 12:54:25 +02:00
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
/// Returns the entity with the specified id
|
|
|
|
|
IfcUtil::IfcBaseClass* instance_by_id(int id);
|
2018-08-29 12:54:25 +02:00
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
/// Returns the entity with the specified GlobalId
|
|
|
|
|
IfcUtil::IfcBaseClass* instance_by_guid(const std::string& guid);
|
2022-10-05 13:03:52 +02:00
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
/// Performs a depth-first traversal, returning all entity instance
|
|
|
|
|
/// attributes as a flat list. NB: includes the root instance specified
|
|
|
|
|
/// in the first function argument.
|
2024-08-23 20:29:07 +02:00
|
|
|
static aggregate_of_instance::ptr traverse(IfcUtil::IfcBaseClass* instance, int max_level = -1);
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
/// Same as traverse() but maintains topological order by using a
|
|
|
|
|
/// breadth-first search
|
2024-08-23 20:29:07 +02:00
|
|
|
static aggregate_of_instance::ptr traverse_breadth_first(IfcUtil::IfcBaseClass* instance, int max_level = -1);
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
/// Get the attribute indices corresponding to the list of entity instances
|
|
|
|
|
/// returned by getInverse().
|
|
|
|
|
std::vector<int> get_inverse_indices(int instance_id);
|
|
|
|
|
|
2023-06-01 22:03:31 +02:00
|
|
|
template <typename T>
|
2023-06-01 21:39:08 +02:00
|
|
|
typename T::list::ptr getInverse(int instance_id, int attribute_index) {
|
2023-06-01 22:03:31 +02:00
|
|
|
return getInverse(instance_id, &T::Class(), attribute_index)->template as<T>();
|
2023-06-01 21:39:08 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
aggregate_of_instance::ptr getInverse(int instance_id, const IfcParse::declaration* type, int attribute_index);
|
|
|
|
|
|
2024-08-23 20:29:07 +02:00
|
|
|
size_t getTotalInverses(int instance_id);
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
unsigned int FreshId() { return ++max_id_; }
|
2023-09-17 12:30:17 +02:00
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
unsigned int getMaxId() const { return max_id_; }
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
const IfcParse::declaration* ifcroot_type() const { return ifcroot_type_; }
|
|
|
|
|
|
|
|
|
|
void recalculate_id_counter();
|
|
|
|
|
|
|
|
|
|
IfcUtil::IfcBaseClass* addEntity(IfcUtil::IfcBaseClass* entity, int id = -1);
|
2023-11-06 20:29:00 +01:00
|
|
|
void addEntities(aggregate_of_instance::ptr entities);
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
/// Removes entity instance from file and unsets references.
|
|
|
|
|
///
|
|
|
|
|
/// Attention when running removeEntity inside a loop over a list of entities to be removed.
|
|
|
|
|
/// This invalidates the iterator. A workaround is to reverse the loop:
|
|
|
|
|
/// boost::shared_ptr<aggregate_of_instance> entities = ...;
|
|
|
|
|
/// for (auto it = entities->end() - 1; it >= entities->begin(); --it) {
|
|
|
|
|
/// IfcUtil::IfcBaseClass *const inst = *it;
|
|
|
|
|
/// model->removeEntity(inst);
|
|
|
|
|
/// }
|
|
|
|
|
void removeEntity(IfcUtil::IfcBaseClass* entity);
|
|
|
|
|
|
|
|
|
|
const IfcSpfHeader& header() const { return _header; }
|
|
|
|
|
IfcSpfHeader& header() { return _header; }
|
|
|
|
|
|
2024-08-23 20:29:07 +02:00
|
|
|
static std::string createTimestamp() ;
|
2023-09-17 12:30:17 +02:00
|
|
|
|
|
|
|
|
const IfcParse::schema_definition* schema() const { return schema_; }
|
|
|
|
|
|
|
|
|
|
std::pair<IfcUtil::IfcBaseClass*, double> getUnit(const std::string& unit_type);
|
|
|
|
|
|
|
|
|
|
void build_inverses();
|
|
|
|
|
|
2025-02-21 16:12:16 +01:00
|
|
|
// @todo variant apply_visitor
|
|
|
|
|
void register_inverse(unsigned, const IfcParse::entity* from_entity, int inst_id, int attribute_index);
|
|
|
|
|
void unregister_inverse(unsigned, const IfcParse::entity* from_entity, IfcUtil::IfcBaseClass*, int attribute_index);
|
|
|
|
|
|
|
|
|
|
typedef VariantMap<impl::in_memory_file_storage::entity_by_guid_t, impl::rocks_db_file_storage::entity_by_guid_t> entity_by_guid_t;
|
|
|
|
|
entity_by_guid_t byguid_;
|
|
|
|
|
typedef VariantMap<impl::in_memory_file_storage::identity_by_id_t, impl::rocks_db_file_storage::identity_by_id_t> identity_by_id_t;
|
|
|
|
|
identity_by_id_t byid_;
|
|
|
|
|
typedef VariantMap<impl::in_memory_file_storage::entity_by_iden_t, impl::rocks_db_file_storage::entity_by_iden_cache_t> entity_by_iden_t;
|
|
|
|
|
entity_by_iden_t byidentity_;
|
|
|
|
|
|
|
|
|
|
// @todo
|
|
|
|
|
entity_by_guid_t internal_guid_map() { return byguid_; };
|
|
|
|
|
|
|
|
|
|
void add_type_ref(IfcUtil::IfcBaseClass* new_entity);
|
|
|
|
|
void remove_type_ref(IfcUtil::IfcBaseClass* new_entity);
|
|
|
|
|
void process_deletion_inverse(IfcUtil::IfcBaseClass* inst);
|
|
|
|
|
|
|
|
|
|
void build_inverses_(IfcUtil::IfcBaseClass*);
|
2017-12-13 18:05:10 +01:00
|
|
|
};
|
|
|
|
|
|
2018-08-29 12:54:25 +02:00
|
|
|
#ifdef WITH_IFCXML
|
|
|
|
|
IFC_PARSE_API IfcFile* parse_ifcxml(const std::string& filename);
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-09-17 12:30:17 +02:00
|
|
|
} // namespace IfcParse
|
2011-09-25 09:53:22 +00:00
|
|
|
|
2018-12-14 12:14:12 +01:00
|
|
|
namespace std {
|
2023-09-17 12:30:17 +02:00
|
|
|
template <>
|
|
|
|
|
struct iterator_traits<IfcParse::IfcFile::type_iterator> {
|
|
|
|
|
typedef ptrdiff_t difference_type;
|
|
|
|
|
typedef const IfcParse::declaration* value_type;
|
|
|
|
|
typedef const IfcParse::declaration*& reference;
|
|
|
|
|
typedef const IfcParse::declaration** pointer;
|
|
|
|
|
typedef std::forward_iterator_tag iterator_category;
|
|
|
|
|
};
|
|
|
|
|
} // namespace std
|
2018-12-14 11:56:09 +01:00
|
|
|
|
2017-12-13 18:05:10 +01:00
|
|
|
#endif
|