]> git.tdb.fi Git - libs/datafile.git/commitdiff
Make PackSource use vector<unique_ptr<T>> instead of list<T>
authorMikko Rasa <tdb@tdb.fi>
Tue, 12 Dec 2023 10:07:18 +0000 (12:07 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 12 Dec 2023 10:10:31 +0000 (12:10 +0200)
Consequently it's no longer copyable.

source/packsource.cpp
source/packsource.h

index 560c3732c70e40664ec4b8117925bf7d6afc8663..dcbc7d7c08aa2bfe0169607570d3c2886b72c359 100644 (file)
@@ -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<Pack>(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<Pack>(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<IO::Seekable> 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<Pack>(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<File>(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<PackSource::Object>(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<PackSource::Object>(obj, name, kwd));
        obj.collection = true;
 }
 
index 16d72adf8cbed9983a0286014a59706c019a1896..aea3b683603f9e9efc63537cc245a6ee589a5a52 100644 (file)
@@ -52,18 +52,16 @@ private:
                std::string filename;
                IO::Seekable *io = nullptr;
                IO::SeekOffset base_offset = 0;
-               std::list<File> files;
+               std::vector<std::unique_ptr<File>> 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<Object> objects;
+               std::vector<std::unique_ptr<Object>> objects;
 
        public:
                File(const Pack &, const std::string &);
-               File(const File &, const Pack &);
 
                std::unique_ptr<IO::Seekable> 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<Pack> packs;
+       std::vector<std::unique_ptr<Pack>> 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 &);