File indexing completed on 2024-04-06 11:56:19
0001 #ifndef GENERS_REFERENCE_HH_
0002 #define GENERS_REFERENCE_HH_
0003
0004 #include "Alignment/Geners/interface/AbsReference.hh"
0005 #include <memory>
0006
0007 #include <memory>
0008
0009 namespace gs {
0010 template <typename T>
0011 class Reference : public AbsReference {
0012 public:
0013 inline Reference(AbsArchive &ar, const unsigned long long itemId)
0014 : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", itemId) {}
0015
0016 inline Reference(AbsArchive &ar, const char *name, const char *category)
0017 : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name, category) {}
0018
0019 #ifndef SWIG
0020 inline Reference(AbsArchive &ar, const std::string &name, const char *category)
0021 : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name.c_str(), category) {}
0022
0023 inline Reference(AbsArchive &ar, const char *name, const std::string &category)
0024 : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name, category.c_str()) {}
0025
0026 inline Reference(AbsArchive &ar, const std::string &name, const std::string &category)
0027 : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", name.c_str(), category.c_str()) {}
0028 #endif
0029
0030 inline Reference(AbsArchive &ar, const SearchSpecifier &namePattern, const SearchSpecifier &categoryPattern)
0031 : AbsReference(ar, ClassId::makeId<T>(), "gs::Single", namePattern, categoryPattern) {}
0032
0033
0034 void restore(unsigned long index, T *obj) const;
0035 std::unique_ptr<T> get(unsigned long index) const;
0036 std::shared_ptr<T> getShared(unsigned long index) const;
0037
0038 Reference() = delete;
0039
0040 private:
0041 T *getPtr(unsigned long index) const;
0042 };
0043 }
0044
0045 #include "Alignment/Geners/interface/GenericIO.hh"
0046
0047 namespace gs {
0048 template <typename T>
0049 inline void Reference<T>::restore(const unsigned long index, T *obj) const {
0050 const unsigned long long itemId = this->id(index);
0051 assert(itemId);
0052 restore_item(this->positionInputStream(itemId), obj, true);
0053 }
0054
0055 template <typename T>
0056 inline T *Reference<T>::getPtr(const unsigned long index) const {
0057 const unsigned long long itemId = this->id(index);
0058 assert(itemId);
0059 T *barePtr = nullptr;
0060 std::vector<ClassId> state;
0061 if (GenericReader<std::istream, std::vector<ClassId>, T *, Int2Type<IOTraits<int>::ISNULLPOINTER>>::process(
0062 barePtr, this->positionInputStream(itemId), &state, true))
0063 assert(barePtr);
0064 else {
0065 delete barePtr;
0066 barePtr = nullptr;
0067 }
0068 if (!barePtr)
0069 throw IOInvalidData(
0070 "In gs::Reference::getPtr: "
0071 "failed to read in the object");
0072 return barePtr;
0073 }
0074
0075 template <typename T>
0076 inline std::unique_ptr<T> Reference<T>::get(const unsigned long index) const {
0077 return std::unique_ptr<T>(getPtr(index));
0078 }
0079
0080 template <typename T>
0081 inline std::shared_ptr<T> Reference<T>::getShared(const unsigned long index) const {
0082 return std::shared_ptr<T>(getPtr(index));
0083 }
0084 }
0085
0086 #endif