From: Mikko Rasa Date: Tue, 12 Dec 2023 10:07:18 +0000 (+0200) Subject: Make PackSource use vector> instead of list X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=07cec1e711e2c80ae771ab1c79eb501da6fa300b;p=libs%2Fdatafile.git Make PackSource use vector> instead of list Consequently it's no longer copyable. --- diff --git a/source/packsource.cpp b/source/packsource.cpp index 560c373..dcbc7d7 100644 --- a/source/packsource.cpp +++ b/source/packsource.cpp @@ -19,31 +19,6 @@ void delete_io(Msp::IO::Base *io) namespace Msp { namespace DataFile { -PackSource::PackSource(const PackSource &other): - packs(other.packs), - files(other.files), - objects(other.objects) -{ - translate_maps(other); -} - -PackSource &PackSource::operator=(const PackSource &other) -{ - packs = list(other.packs.begin(), other.packs.end()); - files = other.files; - objects = other.objects; - translate_maps(other); - return *this; -} - -void PackSource::translate_maps(const PackSource &other) -{ - for(auto i=packs.begin(), j=other.packs.begin(); (i!=packs.end() && j!=other.packs.end()); ++i, ++j) - i->translate_files(files, *j); - for(auto i=files.begin(), j=other.files.begin(); (i!=files.end() && j!=other.files.end()); ++i, ++j) - i->second->translate_objects(objects, *j->second); -} - void PackSource::add_pack_file(const string &fn) { add_pack_file(fn, string()); @@ -66,23 +41,23 @@ void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &f void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter) { - auto i = find_if(packs, [io, &fn](const Pack &p){ return p.get_filename()==fn || (io && p.get_io()==io); }); + auto i = find_if(packs, [io, &fn](const auto &p){ return p->get_filename()==fn || (io && p->get_io()==io); }); if(i==packs.end()) { - packs.push_back(Pack(io, fn)); + packs.emplace_back(make_unique(io, fn)); i = prev(packs.end()); if(io) { DataFile::Parser parser(*io, fn); - Pack::Loader loader(*i); + Pack::Loader loader(**i); loader.load(parser); } else - DataFile::load(*i, fn); + DataFile::load(**i, fn); } FileMap pack_files; - i->collect_files(pack_files, filter); + (*i)->collect_files(pack_files, filter); for(const auto &kvp: pack_files) { files[kvp.first] = kvp.second; @@ -164,38 +139,19 @@ PackSource::Pack::Pack(IO::Seekable *i, const string &fn): io(i) { } -PackSource::Pack::Pack(const Pack &other): - filename(other.filename), - io(other.io), - base_offset(other.base_offset) -{ - for(const File &f: other.files) - files.push_back(File(f, *this)); -} - void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const { if(filter.empty()) { - for(const File &f: files) - fm[f.get_filename()] = &f; + for(const auto &f: files) + fm[f->get_filename()] = f.get(); } else { Regex re(filter); - for(const File &f: files) - if(re.match(f.get_filename())) - fm[f.get_filename()] = &f; - } -} - -void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const -{ - for(auto i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j) - { - auto k = fm.find(i->get_filename()); - if(k!=fm.end() && k->second==&*j) - k->second = &*i; + for(const auto &f: files) + if(re.match(f->get_filename())) + fm[f->get_filename()] = f.get(); } } @@ -205,17 +161,6 @@ PackSource::File::File(const Pack &p, const string &fn): filename(fn) { } -PackSource::File::File(const File &other, const Pack &p): - pack(p), - filename(other.filename), - offset(other.offset), - length(other.length), - collection(other.collection) -{ - for(const Object &o: other.objects) - objects.push_back(Object(o, *this)); -} - unique_ptr PackSource::File::open() const { if(pack.get_io()) @@ -245,18 +190,8 @@ string PackSource::File::get_full_name() const void PackSource::File::collect_objects(ObjectMap &objs) const { - for(const Object &o: objects) - objs[o.get_name()] = &o; -} - -void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const -{ - for(auto i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j) - { - auto k = objs.find(i->get_name()); - if(k!=objs.end() && k->second==&*j) - k->second = &*i; - } + for(const auto &o: objects) + objs[o->get_name()] = o.get(); } @@ -266,12 +201,6 @@ PackSource::Object::Object(const File &f, const string &n, const string &k): keyword(k) { } -PackSource::Object::Object(const Object &other, const File &f): - file(f), - name(other.name), - keyword(other.keyword) -{ } - PackSource::Pack::Loader::Loader(Pack &p): ObjectLoader(p) @@ -282,8 +211,7 @@ PackSource::Pack::Loader::Loader(Pack &p): void PackSource::Pack::Loader::file(const string &fn) { - obj.files.push_back(File(obj, fn)); - load_sub(obj.files.back()); + obj.files.emplace_back(make_sub(obj, fn).load()); } @@ -297,15 +225,12 @@ PackSource::File::Loader::Loader(File &f): void PackSource::File::Loader::finish() { if(!obj.collection) - { - PackSource::Object ob(obj, obj.filename, string()); - obj.objects.push_back(ob); - } + obj.objects.emplace_back(make_unique(obj, obj.filename, string())); } void PackSource::File::Loader::object(const string &name, const string &kwd) { - obj.objects.push_back(PackSource::Object(obj, name, kwd)); + obj.objects.emplace_back(make_unique(obj, name, kwd)); obj.collection = true; } diff --git a/source/packsource.h b/source/packsource.h index 16d72ad..aea3b68 100644 --- a/source/packsource.h +++ b/source/packsource.h @@ -52,18 +52,16 @@ private: std::string filename; IO::Seekable *io = nullptr; IO::SeekOffset base_offset = 0; - std::list files; + std::vector> files; public: Pack(IO::Seekable *, const std::string &); - Pack(const Pack &); const std::string &get_filename() const { return filename; } IO::Seekable *get_io() const { return io; } IO::SeekOffset get_base_offset() const { return base_offset; } void collect_files(FileMap &, const std::string &) const; - void translate_files(FileMap &, const Pack &) const; }; class File @@ -84,11 +82,10 @@ private: IO::SeekOffset offset = 0; IO::SeekOffset length = 0; bool collection = false; - std::list objects; + std::vector> objects; public: File(const Pack &, const std::string &); - File(const File &, const Pack &); std::unique_ptr open() const; const std::string &get_filename() const { return filename; } @@ -97,7 +94,6 @@ private: bool is_collection() const { return collection; } void collect_objects(ObjectMap &) const; - void translate_objects(ObjectMap &, const File &) const; }; class Object @@ -109,24 +105,16 @@ private: public: Object(const File &, const std::string &, const std::string &); - Object(const Object &, const File &); const File &get_file() const { return file; } const std::string &get_name() const { return name; } const std::string &get_keyword() const { return keyword; } }; - std::list packs; + std::vector> packs; FileMap files; ObjectMap objects; -public: - PackSource() = default; - PackSource(const PackSource &); - PackSource &operator=(const PackSource &); -private: - void translate_maps(const PackSource &); - public: /// Adds a pack file to load objects from. The index is read immediately. void add_pack_file(const std::string &);