Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:20

0001 #ifndef GENERS_STRINGARCHIVE_HH_
0002 #define GENERS_STRINGARCHIVE_HH_
0003 
0004 #include "Alignment/Geners/interface/AbsArchive.hh"
0005 #include "Alignment/Geners/interface/CharBuffer.hh"
0006 #include "Alignment/Geners/interface/ContiguousCatalog.hh"
0007 
0008 namespace gs {
0009   class StringArchive : public AbsArchive {
0010   public:
0011     inline StringArchive(const char *name = nullptr) : AbsArchive(name) {}
0012     ~StringArchive() override {}
0013 
0014     inline bool isOpen() const override { return true; }
0015     inline std::string error() const override { return std::string(""); }
0016     inline bool isReadable() const override { return true; }
0017     inline bool isWritable() const override { return true; }
0018     inline unsigned long long size() const override { return catalog_.size(); }
0019     inline unsigned long long smallestId() const override { return catalog_.smallestId(); }
0020     inline unsigned long long largestId() const override { return catalog_.largestId(); }
0021     inline bool idsAreContiguous() const override { return catalog_.isContiguous(); }
0022     inline bool itemExists(const unsigned long long id) const override { return catalog_.itemExists(id); }
0023     inline void itemSearch(const SearchSpecifier &namePattern,
0024                            const SearchSpecifier &categoryPattern,
0025                            std::vector<unsigned long long> *idsFound) const override {
0026       catalog_.search(namePattern, categoryPattern, idsFound);
0027     }
0028 
0029     inline std::shared_ptr<const CatalogEntry> catalogEntry(const unsigned long long id) override {
0030       return catalog_.retrieveEntry(id);
0031     }
0032 
0033     inline void flush() override { stream_.flush(); }
0034 
0035     inline std::string str() const { return static_cast<const std::stringbuf *>(stream_.rdbuf())->str(); }
0036 
0037     // The following operation is equivalent to but much more efficient
0038     // than calling str() and finding out the size of the obtained string
0039     inline unsigned long dataSize() const { return stream_.size(); }
0040 
0041     // Methods related to I/O
0042     inline ClassId classId() const { return ClassId(*this); }
0043     bool write(std::ostream &of) const;
0044 
0045     static inline const char *classname() { return "gs::StringArchive"; }
0046     static inline unsigned version() { return 1; }
0047 
0048     // Can't have "restore" function: no default constructor.
0049     // Note that lastItemId() and lastItemLength() methods
0050     // of the parent class will return 0 when the archive is
0051     // read back from a stream and nothing has been inserted
0052     // into it yet.
0053     static StringArchive *read(const ClassId &id, std::istream &in);
0054 
0055   protected:
0056     bool isEqual(const AbsArchive &) const override;
0057 
0058   private:
0059     void search(AbsReference &reference) override;
0060     std::istream &inputStream(unsigned long long id, long long *sz) override;
0061 
0062     inline std::ostream &outputStream() override {
0063       lastpos_ = stream_.tellp();
0064       return stream_;
0065     }
0066 
0067     inline unsigned long long addToCatalog(const AbsRecord &record,
0068                                            const unsigned compressCode,
0069                                            const unsigned long long itemLength) override {
0070       return catalog_.makeEntry(record, compressCode, itemLength, ItemLocation(lastpos_, nullptr));
0071     }
0072 
0073     CharBuffer stream_;
0074     std::streampos lastpos_;
0075     ContiguousCatalog catalog_;
0076   };
0077 }  // namespace gs
0078 
0079 #endif  // GENERS_STRINGARCHIVE_HH_