Line Code
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
#ifndef GENERS_ABSREFERENCE_HH_
#define GENERS_ABSREFERENCE_HH_

#include "Alignment/Geners/interface/ClassId.hh"
#include "Alignment/Geners/interface/SearchSpecifier.hh"

#include <iostream>
#include <memory>
#include <vector>

namespace gs {
  class AbsArchive;
  class CatalogEntry;

  class AbsReference {
  public:
    AbsReference() = delete;
    inline virtual ~AbsReference() {}

    inline AbsArchive &archive() const { return archive_; }
    inline const ClassId &type() const { return classId_; }
    inline const std::string &ioPrototype() const { return ioProto_; }
    inline const SearchSpecifier &namePattern() const { return namePattern_; }
    inline const SearchSpecifier &categoryPattern() const { return categoryPattern_; }

    // Determine if the item in the catalog is compatible for I/O
    // purposes with the one referenced here
    virtual bool isIOCompatible(const CatalogEntry &r) const;

    // Check I/O prototype only allowing for class id mismatch
    bool isSameIOPrototype(const CatalogEntry &r) const;

    // Are there any items referenced?
    bool empty() const;

    // Exactly one item referenced?
    bool unique() const;

    // How many items are referenced?
    unsigned long size() const;

    // The following function throws gs::IOOutOfRange exception
    // if the index is out of range
    unsigned long long id(unsigned long index) const;

    // Catalog entry retrieval by index in the list of referenced items.
    // Throws gs::IOOutOfRange exception if the index is out of range.
    std::shared_ptr<const CatalogEntry> indexedCatalogEntry(unsigned long index) const;

  protected:
    // Use the following constructor to retrieve an item with
    // a known id
    AbsReference(AbsArchive &ar, const ClassId &classId, const char *ioProto, unsigned long long itemId);

    // Use the following constructor to search for items which
    // match name and category patterns
    AbsReference(AbsArchive &ar,
                 const ClassId &classId,
                 const char *ioProto,
                 const SearchSpecifier &namePattern,
                 const SearchSpecifier &categoryPattern);

    std::istream &positionInputStream(unsigned long long id) const;

  private:
    friend class AbsArchive;

    void initialize() const;
    void addItemId(unsigned long long id);

    AbsArchive &archive_;
    ClassId classId_;
    std::string ioProto_;

    // The following items will be filled or not,
    // depending on which constructor was called
    unsigned long long searchId_;
    SearchSpecifier namePattern_;
    SearchSpecifier categoryPattern_;

    // Id for a unique verified item
    unsigned long long itemId_;

    // The item list in case ids are not unique
    std::vector<unsigned long long> idList_;

    // We can't talk to the archive from the constructor
    // because we need correct "isIOCompatible" which
    // can be overriden by derived classes. Therefore,
    // we will delay archive searching until the first
    // function call.
    bool initialized_;
  };
}  // namespace gs

#endif  // GENERS_ABSREFERENCE_HH_