summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/code/AssetLib/Step/STEPFile.h
blob: e09faad98210aa12141025f496939e81d92091a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2020, assimp team


All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------
*/

#ifndef INCLUDED_AI_STEPFILE_H
#define INCLUDED_AI_STEPFILE_H

#include <bitset>
#include <map>
#include <memory>
#include <set>
#include <typeinfo>
#include <vector>

#include "AssetLib/FBX/FBXDocument.h" //ObjectMap::value_type

#include <assimp/DefaultLogger.hpp>

#ifdef _MSC_VER
#    pragma warning(push)
#    pragma warning(disable : 4127 4456 4245 4512 )
#endif // _MSC_VER

//
#if _MSC_VER > 1500 || (defined __GNUC___)
#    define ASSIMP_STEP_USE_UNORDERED_MULTIMAP
#else
#    define step_unordered_map map
#    define step_unordered_multimap multimap
#endif

#ifdef ASSIMP_STEP_USE_UNORDERED_MULTIMAP
#    include <unordered_map>
#    if defined(_MSC_VER) && _MSC_VER <= 1600
#        define step_unordered_map tr1::unordered_map
#        define step_unordered_multimap tr1::unordered_multimap
#    else
#        define step_unordered_map unordered_map
#        define step_unordered_multimap unordered_multimap
#    endif
#endif

#include <assimp/LineSplitter.h>

// uncomment this to have the loader evaluate all entities upon loading.
// this is intended as stress test - by default, entities are evaluated
// lazily and therefore not unless needed.

//#define ASSIMP_IFC_TEST

namespace Assimp {

// ********************************************************************************
// before things get complicated, this is the basic outline:

namespace STEP {

namespace EXPRESS {

// base data types known by EXPRESS schemata - any custom data types will derive one of those
class DataType;
class UNSET; /*: public DataType */
class ISDERIVED; /*: public DataType */
//  class REAL;         /*: public DataType */
class ENUM; /*: public DataType */
//  class STRING;       /*: public DataType */
//  class INTEGER;      /*: public DataType */
class ENTITY; /*: public DataType */
class LIST; /*: public DataType */
//  class SELECT;       /*: public DataType */

// a conversion schema is not exactly an EXPRESS schema, rather it
// is a list of pointers to conversion functions to build up the
// object tree from an input file.
class ConversionSchema;
} // namespace EXPRESS

struct HeaderInfo;
class Object;
class LazyObject;
class DB;

typedef Object *(*ConvertObjectProc)(const DB &db, const EXPRESS::LIST &params);
} // namespace STEP

// ********************************************************************************

namespace STEP {

// -------------------------------------------------------------------------------
/** Exception class used by the STEP loading & parsing code. It is typically
     *  coupled with a line number. */
// -------------------------------------------------------------------------------
struct SyntaxError : DeadlyImportError {
    enum : uint64_t {
        LINE_NOT_SPECIFIED = 0xfffffffffffffffLL
    };

    SyntaxError(const std::string &s, uint64_t line = LINE_NOT_SPECIFIED);
};

// -------------------------------------------------------------------------------
/** Exception class used by the STEP loading & parsing code when a type
     *  error (i.e. an entity expects a string but receives a bool) occurs.
     *  It is typically coupled with both an entity id and a line number.*/
// -------------------------------------------------------------------------------
struct TypeError : DeadlyImportError {
    enum : uint64_t {
        ENTITY_NOT_SPECIFIED = 0xffffffffffffffffUL,
        ENTITY_NOT_SPECIFIED_32 = 0x00000000ffffffff
    };

    TypeError(const std::string &s, uint64_t entity = ENTITY_NOT_SPECIFIED, uint64_t line = SyntaxError::LINE_NOT_SPECIFIED);
};

// hack to make a given member template-dependent
template <typename T, typename T2>
T2 &Couple(T2 &in) {
    return in;
}

namespace EXPRESS {

// -------------------------------------------------------------------------------
//** Base class for all STEP data types */
// -------------------------------------------------------------------------------
class DataType {
public:
    typedef std::shared_ptr<const DataType> Out;

public:
    virtual ~DataType() {
    }

public:
    template <typename T>
    const T &To() const {
        return dynamic_cast<const T &>(*this);
    }

    template <typename T>
    T &To() {
        return dynamic_cast<T &>(*this);
    }

    template <typename T>
    const T *ToPtr() const {
        return dynamic_cast<const T *>(this);
    }

    template <typename T>
    T *ToPtr() {
        return dynamic_cast<T *>(this);
    }

    // utilities to deal with SELECT entities, which currently lack automatic
    // conversion support.
    template <typename T>
    const T &ResolveSelect(const DB &db) const {
        return Couple<T>(db).MustGetObject(To<EXPRESS::ENTITY>())->template To<T>();
    }

    template <typename T>
    const T *ResolveSelectPtr(const DB &db) const {
        const EXPRESS::ENTITY *e = ToPtr<EXPRESS::ENTITY>();
        return e ? Couple<T>(db).MustGetObject(*e)->template ToPtr<T>() : (const T *)0;
    }

public:
    /** parse a variable from a string and set 'inout' to the character
             *  behind the last consumed character. An optional schema enables,
             *  if specified, automatic conversion of custom data types.
             *
             *  @throw SyntaxError
             */
    static std::shared_ptr<const EXPRESS::DataType> Parse(const char *&inout,
            uint64_t line = SyntaxError::LINE_NOT_SPECIFIED,
            const EXPRESS::ConversionSchema *schema = NULL);

public:
};

typedef DataType SELECT;
typedef DataType LOGICAL;

// -------------------------------------------------------------------------------
/** Sentinel class to represent explicitly unset (optional) fields ($) */
// -------------------------------------------------------------------------------
class UNSET : public DataType {
public:
private:
};

// -------------------------------------------------------------------------------
/** Sentinel class to represent explicitly derived fields (*) */
// -------------------------------------------------------------------------------
class ISDERIVED : public DataType {
public:
private:
};

// -------------------------------------------------------------------------------
/** Shared implementation for some of the primitive data type, i.e. int, float */
// -------------------------------------------------------------------------------
template <typename T>
class PrimitiveDataType : public DataType {
public:
    // This is the type that will cd ultimatively be used to
    // expose this data type to the user.
    typedef T Out;

    PrimitiveDataType() {}
    PrimitiveDataType(const T &val) :
            val(val) {}

    PrimitiveDataType(const PrimitiveDataType &o) {
        (*this) = o;
    }

    operator const T &() const {
        return val;
    }

    PrimitiveDataType &operator=(const PrimitiveDataType &o) {
        val = o.val;
        return *this;
    }

protected:
    T val;
};

typedef PrimitiveDataType<int64_t> INTEGER;
typedef PrimitiveDataType<double> REAL;
typedef PrimitiveDataType<double> NUMBER;
typedef PrimitiveDataType<std::string> STRING;

// -------------------------------------------------------------------------------
/** Generic base class for all enumerated types */
// -------------------------------------------------------------------------------
class ENUMERATION : public STRING {
public:
    ENUMERATION(const std::string &val) :
            STRING(val) {}

private:
};

typedef ENUMERATION BOOLEAN;

// -------------------------------------------------------------------------------
/** This is just a reference to an entity/object somewhere else */
// -------------------------------------------------------------------------------
class ENTITY : public PrimitiveDataType<uint64_t> {
public:
    ENTITY(uint64_t val) :
            PrimitiveDataType<uint64_t>(val) {
        ai_assert(val != 0);
    }

    ENTITY() :
            PrimitiveDataType<uint64_t>(TypeError::ENTITY_NOT_SPECIFIED) {
        // empty
    }

private:
};

// -------------------------------------------------------------------------------
/** Wrap any STEP aggregate: LIST, SET, ... */
// -------------------------------------------------------------------------------
class LIST : public DataType {
public:
    // access a particular list index, throw std::range_error for wrong indices
    std::shared_ptr<const DataType> operator[](size_t index) const {
        return members[index];
    }

    size_t GetSize() const {
        return members.size();
    }

public:
    /** @see DaraType::Parse */
    static std::shared_ptr<const EXPRESS::LIST> Parse(const char *&inout,
            uint64_t line = SyntaxError::LINE_NOT_SPECIFIED,
            const EXPRESS::ConversionSchema *schema = NULL);

private:
    typedef std::vector<std::shared_ptr<const DataType>> MemberList;
    MemberList members;
};

class BINARY : public PrimitiveDataType<uint32_t> {
public:
    BINARY(uint32_t val) :
            PrimitiveDataType<uint32_t>(val) {
        // empty
    }

    BINARY() :
            PrimitiveDataType<uint32_t>(TypeError::ENTITY_NOT_SPECIFIED_32) {
        // empty
    }
};

// -------------------------------------------------------------------------------
/* Not exactly a full EXPRESS schema but rather a list of conversion functions
         * to extract valid C++ objects out of a STEP file. Those conversion functions
         * may, however, perform further schema validations. */
// -------------------------------------------------------------------------------
class ConversionSchema {
public:
    struct SchemaEntry {
        SchemaEntry(const char *name, ConvertObjectProc func) :
                mName(name), mFunc(func) {
            // empty
        }

        const char *mName;
        ConvertObjectProc mFunc;
    };

    typedef std::map<std::string, ConvertObjectProc> ConverterMap;

    template <size_t N>
    explicit ConversionSchema(const SchemaEntry (&schemas)[N]) {
        *this = schemas;
    }

    ConversionSchema() {
    }

    ConvertObjectProc GetConverterProc(const std::string &name) const {
        ConverterMap::const_iterator it = converters.find(name);
        return it == converters.end() ? nullptr : (*it).second;
    }

    bool IsKnownToken(const std::string &name) const {
        return converters.find(name) != converters.end();
    }

    const char *GetStaticStringForToken(const std::string &token) const {
        ConverterMap::const_iterator it = converters.find(token);
        return it == converters.end() ? nullptr : (*it).first.c_str();
    }

    template <size_t N>
    const ConversionSchema &operator=(const SchemaEntry (&schemas)[N]) {
        for (size_t i = 0; i < N; ++i) {
            const SchemaEntry &schema = schemas[i];
            converters[schema.mName] = schema.mFunc;
        }
        return *this;
    }

private:
    ConverterMap converters;
};
} // namespace EXPRESS

// ------------------------------------------------------------------------------
/** Bundle all the relevant info from a STEP header, parts of which may later
     *  be plainly dumped to the logfile, whereas others may help the caller pick an
     *  appropriate loading strategy.*/
// ------------------------------------------------------------------------------
struct HeaderInfo {
    std::string timestamp;
    std::string app;
    std::string fileSchema;
};

// ------------------------------------------------------------------------------
/** Base class for all concrete object instances */
// ------------------------------------------------------------------------------
class Object {
public:
    Object(const char *classname = "unknown") :
            id(0), classname(classname) {
        // empty
    }

    virtual ~Object() {
        // empty
    }

    // utilities to simplify casting to concrete types
    template <typename T>
    const T &To() const {
        return dynamic_cast<const T &>(*this);
    }

    template <typename T>
    T &To() {
        return dynamic_cast<T &>(*this);
    }

    template <typename T>
    const T *ToPtr() const {
        return dynamic_cast<const T *>(this);
    }

    template <typename T>
    T *ToPtr() {
        return dynamic_cast<T *>(this);
    }

    uint64_t GetID() const {
        return id;
    }

    std::string GetClassName() const {
        return classname;
    }

    void SetID(uint64_t newval) {
        id = newval;
    }

private:
    uint64_t id;
    const char *const classname;
};

template <typename T>
size_t GenericFill(const STEP::DB &db, const EXPRESS::LIST &params, T *in);
// (intentionally undefined)

// ------------------------------------------------------------------------------
/** CRTP shared base class for use by concrete entity implementation classes */
// ------------------------------------------------------------------------------
template <typename TDerived, size_t arg_count>
struct ObjectHelper : virtual Object {
    ObjectHelper() :
            aux_is_derived(0) {
        // empty
    }

    static Object *Construct(const STEP::DB &db, const EXPRESS::LIST &params) {
        // make sure we don't leak if Fill() throws an exception
        std::unique_ptr<TDerived> impl(new TDerived());

        // GenericFill<T> is undefined so we need to have a specialization
        const size_t num_args = GenericFill<TDerived>(db, params, &*impl);
        (void)num_args;

        // the following check is commented because it will always trigger if
        // parts of the entities are generated with dummy wrapper code.
        // This is currently done to reduce the size of the loader
        // code.
        //if (num_args != params.GetSize() && impl->GetClassName() != "NotImplemented") {
        //  DefaultLogger::get()->debug("STEP: not all parameters consumed");
        //}
        return impl.release();
    }

    // note that this member always exists multiple times within the hierarchy
    // of an individual object, so any access to it must be disambiguated.
    std::bitset<arg_count> aux_is_derived;
};

// ------------------------------------------------------------------------------
/** Class template used to represent OPTIONAL data members in the converted schema */
// ------------------------------------------------------------------------------
template <typename T>
struct Maybe {
    Maybe() :
            have() {
        // empty
    }

    explicit Maybe(const T &ptr) :
            ptr(ptr), have(true) {
        // empty
    }

    void flag_invalid() {
        have = false;
    }

    void flag_valid() {
        have = true;
    }

    bool operator!() const {
        return !have;
    }

    operator bool() const {
        return have;
    }

    operator const T &() const {
        return Get();
    }

    const T &Get() const {
        ai_assert(have);
        return ptr;
    }

    Maybe &operator=(const T &_ptr) {
        ptr = _ptr;
        have = true;
        return *this;
    }

private:
    template <typename T2>
    friend struct InternGenericConvert;

    operator T &() {
        return ptr;
    }

    T ptr;
    bool have;
};

// ------------------------------------------------------------------------------
/** A LazyObject is created when needed. Before this happens, we just keep
       the text line that contains the object definition. */
// -------------------------------------------------------------------------------
class LazyObject {
    friend class DB;

public:
    LazyObject(DB &db, uint64_t id, uint64_t line, const char *type, const char *args);
    ~LazyObject();

    Object &operator*() {
        if (!obj) {
            LazyInit();
            ai_assert(obj);
        }
        return *obj;
    }

    const Object &operator*() const {
        if (!obj) {
            LazyInit();
            ai_assert(obj);
        }
        return *obj;
    }

    template <typename T>
    const T &To() const {
        return dynamic_cast<const T &>(**this);
    }

    template <typename T>
    T &To() {
        return dynamic_cast<T &>(**this);
    }

    template <typename T>
    const T *ToPtr() const {
        return dynamic_cast<const T *>(&**this);
    }

    template <typename T>
    T *ToPtr() {
        return dynamic_cast<T *>(&**this);
    }

    Object *operator->() {
        return &**this;
    }

    const Object *operator->() const {
        return &**this;
    }

    bool operator==(const std::string &atype) const {
        return type == atype;
    }

    bool operator!=(const std::string &atype) const {
        return type != atype;
    }

    uint64_t GetID() const {
        return id;
    }

private:
    void LazyInit() const;

private:
    mutable uint64_t id;
    const char *const type;
    DB &db;
    mutable const char *args;
    mutable Object *obj;
};

template <typename T>
inline bool operator==(const std::shared_ptr<LazyObject> &lo, T whatever) {
    return *lo == whatever; // XXX use std::forward if we have 0x
}

template <typename T>
inline bool operator==(const std::pair<uint64_t, std::shared_ptr<LazyObject>> &lo, T whatever) {
    return *(lo.second) == whatever; // XXX use std::forward if we have 0x
}

// ------------------------------------------------------------------------------
/** Class template used to represent lazily evaluated object references in the converted schema */
// ------------------------------------------------------------------------------
template <typename T>
struct Lazy {
    typedef Lazy Out;
    Lazy(const LazyObject *obj = nullptr) :
            obj(obj) {
        // empty
    }

    operator const T *() const {
        return obj->ToPtr<T>();
    }

    operator const T &() const {
        return obj->To<T>();
    }

    const T &operator*() const {
        return obj->To<T>();
    }

    const T *operator->() const {
        return &obj->To<T>();
    }

    const LazyObject *obj;
};

// ------------------------------------------------------------------------------
/** Class template used to represent LIST and SET data members in the converted schema */
// ------------------------------------------------------------------------------
template <typename T, uint64_t min_cnt, uint64_t max_cnt = 0uL>
struct ListOf : public std::vector<typename T::Out> {
    typedef typename T::Out OutScalar;
    typedef ListOf Out;

    ListOf() {
        static_assert(min_cnt <= max_cnt || !max_cnt, "min_cnt <= max_cnt || !max_cnt");
    }
};

// ------------------------------------------------------------------------------
template <typename TOut>
struct PickBaseType {
    typedef EXPRESS::PrimitiveDataType<TOut> Type;
};

template <typename TOut>
struct PickBaseType<Lazy<TOut>> {
    typedef EXPRESS::ENTITY Type;
};

template <>
struct PickBaseType<std::shared_ptr<const EXPRESS::DataType>>;

// ------------------------------------------------------------------------------
template <typename T>
struct InternGenericConvert {
    void operator()(T &out, const std::shared_ptr<const EXPRESS::DataType> &in, const STEP::DB & /*db*/) {
        try {
            out = dynamic_cast<const typename PickBaseType<T>::Type &>(*in);
        } catch (std::bad_cast &) {
            throw TypeError("type error reading literal field");
        }
    }
};

template <>
struct InternGenericConvert<std::shared_ptr<const EXPRESS::DataType>> {
    void operator()(std::shared_ptr<const EXPRESS::DataType> &out, const std::shared_ptr<const EXPRESS::DataType> &in, const STEP::DB & /*db*/) {
        out = in;
    }
};

template <typename T>
struct InternGenericConvert<Maybe<T>> {
    void operator()(Maybe<T> &out, const std::shared_ptr<const EXPRESS::DataType> &in, const STEP::DB &db) {
        GenericConvert((T &)out, in, db);
        out.flag_valid();
    }
};

#if _MSC_VER > 1920
#pragma warning(push)
#pragma warning(disable : 4127)
#endif // _WIN32

template <typename T, uint64_t min_cnt, uint64_t max_cnt>
struct InternGenericConvertList {
    void operator()(ListOf<T, min_cnt, max_cnt> &out, const std::shared_ptr<const EXPRESS::DataType> &inp_base, const STEP::DB &db) {

        const EXPRESS::LIST *inp = dynamic_cast<const EXPRESS::LIST *>(inp_base.get());
        if (!inp) {
            throw TypeError("type error reading aggregate");
        }

        // XXX is this really how the EXPRESS notation ([?:3],[1:3]) is intended?
        const size_t len = inp->GetSize();
        if (0 != max_cnt && len > max_cnt) {
            ASSIMP_LOG_WARN("too many aggregate elements");
        } else if (len < min_cnt) {
            ASSIMP_LOG_WARN("too few aggregate elements");
        }

        out.reserve(inp->GetSize());
        for (size_t i = 0; i < inp->GetSize(); ++i) {

            out.push_back(typename ListOf<T, min_cnt, max_cnt>::OutScalar());
            try {
                GenericConvert(out.back(), (*inp)[i], db);
            } catch (const TypeError &t) {
                throw TypeError(t.what() + std::string(" of aggregate"));
            }
        }
    }
};

template <typename T>
struct InternGenericConvert<Lazy<T>> {
    void operator()(Lazy<T> &out, const std::shared_ptr<const EXPRESS::DataType> &in_base, const STEP::DB &db) {
        const EXPRESS::ENTITY *in = dynamic_cast<const EXPRESS::ENTITY *>(in_base.get());
        if (!in) {
            throw TypeError("type error reading entity");
        }
        out = Couple<T>(db).GetObject(*in);
    }
};

template <typename T1>
inline void GenericConvert(T1 &a, const std::shared_ptr<const EXPRESS::DataType> &b, const STEP::DB &db) {
    return InternGenericConvert<T1>()(a, b, db);
}

template <typename T1, uint64_t N1, uint64_t N2>
inline void GenericConvert(ListOf<T1, N1, N2> &a, const std::shared_ptr<const EXPRESS::DataType> &b, const STEP::DB &db) {
    return InternGenericConvertList<T1, N1, N2>()(a, b, db);
}

// ------------------------------------------------------------------------------
/** Lightweight manager class that holds the map of all objects in a
     *  STEP file. DB's are exclusively maintained by the functions in
     *  STEPFileReader.h*/
// -------------------------------------------------------------------------------
class DB {
    friend DB *ReadFileHeader(std::shared_ptr<IOStream> stream);
    friend void ReadFile(DB &db, const EXPRESS::ConversionSchema &scheme,
            const char *const *types_to_track, size_t len,
            const char *const *inverse_indices_to_track, size_t len2);

    friend class LazyObject;

public:
    // objects indexed by ID - this can grow pretty large (i.e some hundred million
    // entries), so use raw pointers to avoid *any* overhead.
    typedef std::map<uint64_t, const LazyObject *> ObjectMap;

    // objects indexed by their declarative type, but only for those that we truly want
    typedef std::set<const LazyObject *> ObjectSet;
    typedef std::map<std::string, ObjectSet> ObjectMapByType;

    // list of types for which to keep inverse indices for all references
    // that the respective objects keep.
    // the list keeps pointers to strings in static storage
    typedef std::set<const char *> InverseWhitelist;

    // references - for each object id the ids of all objects which reference it
    // this is used to simulate STEP inverse indices for selected types.
    typedef std::step_unordered_multimap<uint64_t, uint64_t> RefMap;
    typedef std::pair<RefMap::const_iterator, RefMap::const_iterator> RefMapRange;

private:
    DB(const std::shared_ptr<StreamReaderLE> &reader) :
            reader(reader), splitter(*reader, true, true), evaluated_count(), schema(nullptr) {}

public:
    ~DB() {
        for (ObjectMap::value_type &o : objects) {
            delete o.second;
        }
    }

    uint64_t GetObjectCount() const {
        return objects.size();
    }

    uint64_t GetEvaluatedObjectCount() const {
        return evaluated_count;
    }

    const HeaderInfo &GetHeader() const {
        return header;
    }

    const EXPRESS::ConversionSchema &GetSchema() const {
        return *schema;
    }

    const ObjectMap &GetObjects() const {
        return objects;
    }

    const ObjectMapByType &GetObjectsByType() const {
        return objects_bytype;
    }

    const RefMap &GetRefs() const {
        return refs;
    }

    bool KeepInverseIndicesForType(const char *const type) const {
        return inv_whitelist.find(type) != inv_whitelist.end();
    }

    // get the yet unevaluated object record with a given id
    const LazyObject *GetObject(uint64_t id) const {
        const ObjectMap::const_iterator it = objects.find(id);
        if (it != objects.end()) {
            return (*it).second;
        }
        return nullptr;
    }

    // get an arbitrary object out of the soup with the only restriction being its type.
    const LazyObject *GetObject(const std::string &type) const {
        const ObjectMapByType::const_iterator it = objects_bytype.find(type);
        if (it != objects_bytype.end() && (*it).second.size()) {
            return *(*it).second.begin();
        }
        return NULL;
    }

    // same, but raise an exception if the object doesn't exist and return a reference
    const LazyObject &MustGetObject(uint64_t id) const {
        const LazyObject *o = GetObject(id);
        if (!o) {
            throw TypeError("requested entity is not present", id);
        }
        return *o;
    }

    const LazyObject &MustGetObject(const std::string &type) const {
        const LazyObject *o = GetObject(type);
        if (!o) {
            throw TypeError("requested entity of type " + type + "is not present");
        }
        return *o;
    }

#ifdef ASSIMP_IFC_TEST

    // evaluate *all* entities in the file. this is a power test for the loader
    void EvaluateAll() {
        for (ObjectMap::value_type &e : objects) {
            **e.second;
        }
        ai_assert(evaluated_count == objects.size());
    }

#endif

private:
    // full access only offered to close friends - they should
    // use the provided getters rather than messing around with
    // the members directly.
    LineSplitter &GetSplitter() {
        return splitter;
    }

    void InternInsert(const LazyObject *lz) {
        objects[lz->GetID()] = lz;

        const ObjectMapByType::iterator it = objects_bytype.find(lz->type);
        if (it != objects_bytype.end()) {
            (*it).second.insert(lz);
        }
    }

    void SetSchema(const EXPRESS::ConversionSchema &_schema) {
        schema = &_schema;
    }

    void SetTypesToTrack(const char *const *types, size_t N) {
        for (size_t i = 0; i < N; ++i) {
            objects_bytype[types[i]] = ObjectSet();
        }
    }

    void SetInverseIndicesToTrack(const char *const *types, size_t N) {
        for (size_t i = 0; i < N; ++i) {
            const char *const sz = schema->GetStaticStringForToken(types[i]);
            ai_assert(sz);
            inv_whitelist.insert(sz);
        }
    }

    HeaderInfo &GetHeader() {
        return header;
    }

    void MarkRef(uint64_t who, uint64_t by_whom) {
        refs.insert(std::make_pair(who, by_whom));
    }

private:
    HeaderInfo header;
    ObjectMap objects;
    ObjectMapByType objects_bytype;
    RefMap refs;
    InverseWhitelist inv_whitelist;
    std::shared_ptr<StreamReaderLE> reader;
    LineSplitter splitter;
    uint64_t evaluated_count;
    const EXPRESS::ConversionSchema *schema;
};

#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER

} // namespace STEP

} // namespace Assimp

#endif // INCLUDED_AI_STEPFILE_H