]> git.tdb.fi Git - libs/datafile.git/commitdiff
Don't use hierarchical names when loading items into a Collection master
authorMikko Rasa <tdb@tdb.fi>
Tue, 27 Aug 2024 11:56:43 +0000 (14:56 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 27 Aug 2024 11:56:43 +0000 (14:56 +0300)
source/collection.h
source/loader.h

index 2d0986ba1e56497aed10e8094e467e448211ce87..cd4220908b2fc5015d66ca4d397881103778b7a9 100644 (file)
@@ -71,7 +71,7 @@ public:
                Collection &get_object() const { return coll; }
        private:
                template<typename T>
-               void item(const std::string &n) { make_sub<T>().into(dynamic_cast<typename CollectionType<typename T::Loader>::Type &>(coll)n).load(); }
+               void item(const std::string &n) { make_sub<T>().into(dynamic_cast<typename CollectionType<typename T::Loader>::Type &>(coll)).raw_name(n).load(); }
        };
 
 private:
index c759f597535962bfb99fc6c3cf702b241d7b9695..ee7f443476d39249738fad25bbfad3f8ce911f4f 100644 (file)
@@ -5,6 +5,7 @@
 #include <map>
 #include <optional>
 #include <type_traits>
+#include <msp/core/except.h>
 #include <msp/io/file.h>
 #include "loaderaction.h"
 #include "meta.h"
@@ -62,12 +63,16 @@ protected:
                SubOps(Loader &p, P o, C *c, const std::string &n): parent(p), obj(std::move(o)), coll(c) { name(n); }
 
                SubOps &name(const std::string &n) { name_ = parent.context+": "+n; return *this; }
+               SubOps &raw_name(const std::string &n) { name_ = n; return *this; }
 
                /** Stores the object in a collection after loading.  The object must be
                created by the loader. */
                template<typename C2>
                SubOps<P, L, C2> into(C2 &c, const std::string &n) { return { parent, std::move(obj), &c, n }; }
 
+               template<typename C2>
+               SubOps<P, L, C2> into(C2 &c) { return { parent, std::move(obj), &c, {} }; }
+
                /** Loads the object.  Arguments are passed to the loader's constructor.
                If into() was called, a reference to the collection will be passed as
                well. */
@@ -221,6 +226,10 @@ template<typename P, typename L, typename C>
 template<typename... Args>
 typename Loader::SubOps<P, L, C>::LoadResult Loader::SubOps<P, L, C>::load(Args &&... args)
 {
+       if constexpr(!std::is_same_v<C, void>)
+               if(name_.empty())
+                       throw invalid_state("missing name");
+
        std::optional<L> ldr;
        if constexpr(std::is_same_v<P, std::nullptr_t>)
        {