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
#ifndef GENERS_REFERENCE_HH_
#define GENERS_REFERENCE_HH_

#include "Alignment/Geners/interface/AbsReference.hh"
#include <memory>

#include <memory>

namespace gs {
  template <typename T>
  class Reference : public AbsReference {
  public:
    inline Reference(AbsArchive &ar, const unsigned long long itemId)
        : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", itemId) {}

    inline Reference(AbsArchive &ar, const char *name, const char *category)
        : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name, category) {}

#ifndef SWIG
    inline Reference(AbsArchive &ar, const std::string &name, const char *category)
        : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name.c_str(), category) {}

    inline Reference(AbsArchive &ar, const char *name, const std::string &category)
        : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name, category.c_str()) {}

    inline Reference(AbsArchive &ar, const std::string &name, const std::string &category)
        : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name.c_str(), category.c_str()) {}
#endif

    inline Reference(AbsArchive &ar, const SearchSpecifier &namePattern, const SearchSpecifier &categoryPattern)
        : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", namePattern, categoryPattern) {}

    // Methods to retrieve the item
    void restore(unsigned long index, T *obj) const;
    std::unique_ptr<T> get(unsigned long index) const;
    std::shared_ptr<T> getShared(unsigned long index) const;

    Reference() = delete;

  private:
    T *getPtr(unsigned long index) const;
  };
}  // namespace gs

#include "Alignment/Geners/interface/GenericIO.hh"

namespace gs {
  template <typename T>
  inline void Reference<T>::restore(const unsigned long index, T *obj) const {
    const unsigned long long itemId = this->id(index);
    assert(itemId);
    restore_item(this->positionInputStream(itemId), obj, true);
  }

  template <typename T>
  inline T *Reference<T>::getPtr(const unsigned long index) const {
    const unsigned long long itemId = this->id(index);
    assert(itemId);
    T *barePtr = nullptr;
    std::vector<ClassId> state;
    if (GenericReader<std::istream, std::vector<ClassId>, T *, Int2Type<IOTraits<int>::ISNULLPOINTER>>::process(
            barePtr, this->positionInputStream(itemId), &state, true))
      assert(barePtr);
    else {
      delete barePtr;
      barePtr = nullptr;
    }
    if (!barePtr)
      throw IOInvalidData(
          "In gs::Reference::getPtr: "
          "failed to read in the object");
    return barePtr;
  }

  template <typename T>
  inline std::unique_ptr<T> Reference<T>::get(const unsigned long index) const {
    return std::unique_ptr<T>(getPtr(index));
  }

  template <typename T>
  inline std::shared_ptr<T> Reference<T>::getShared(const unsigned long index) const {
    return std::shared_ptr<T>(getPtr(index));
  }
}  // namespace gs

#endif  // GENERS_REFERENCE_HH_