]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/loader.h
Cosmetic changes
[libs/datafile.git] / source / loader.h
index 604886125614728c2552d486decfa01afa19c804..3fe26e7c03bccfce0cd2441a46e055b99ffb8831 100644 (file)
@@ -1,9 +1,11 @@
 #ifndef MSP_DATAFILE_LOADER_H_
 #define MSP_DATAFILE_LOADER_H_
 
+#include <type_traits>
 #include <map>
 #include <msp/io/file.h>
 #include "loaderaction.h"
+#include "meta.h"
 #include "parser.h"
 #include "statement.h"
 
@@ -32,30 +34,30 @@ destructor.
 
 See also classes ObjectLoader and CollectionObjectLoader in objectloader.h.
 */
-class Loader
+class Loader: private NonCopyable
 {
-private:
-       struct ActionKey
+protected:
+       class ActionMap: public std::map<StatementKey, LoaderAction *>, private NonCopyable
        {
-               std::string keyword;
-               std::string signature;
-
-               ActionKey(const std::string &, const std::string &);
-
-               bool operator<(const ActionKey &) const;
+       public:
+               ~ActionMap();
        };
 
-       typedef std::map<ActionKey, LoaderAction *> ActionMap;
-
-       ActionMap actions;
-       const Statement *cur_st;
-       bool sub_loaded;
+private:
+       ActionMap local_actions;
+       ActionMap *actions = nullptr;
+       Parser *cur_parser = nullptr;
+       unsigned cur_level = 0;
+       const Statement *cur_st = nullptr;
+       bool sub_loaded = false;
+       bool direct = false;
+       std::vector<Loader *> aux_loaders;
 protected:
-       bool check_sub_loads;
+       bool check_sub_loads = false;
 
-       Loader();
+       Loader() = default;
 public:
-       virtual ~Loader();
+       virtual ~Loader() = default;
 
        /** Loads statements from a parser. */
        void load(Parser &p);
@@ -64,6 +66,9 @@ private:
        /** Loads data from a statement. */
        void load(const Statement &st);
 
+       /** Loads statemsnts from a parser, feeding them directly to actions. */
+       void load_direct(Parser &, unsigned);
+
        /** Processes a single statement */
        void load_statement(const Statement &st);
 
@@ -78,18 +83,23 @@ protected:
        }
 
        /** Loads a sub-object from the statement being processed with an extra
-       parameter for the Loader.  The Loader class of the sub-object is
+       arguments for the Loader.  The Loader class of the sub-object is
        automatically used. */
-       template<typename S, typename T>
-       void load_sub(S &s, T &p)
+       template<typename S, typename... Args>
+       void load_sub(S &s, Args &&... args)
        {
-               typename S::Loader ldr(s, p);
+               typename S::Loader ldr(s, std::forward<Args>(args)...);
                load_sub_with(ldr);
        }
 
        /** Processes the current statement's substatements with another Loader. */
        void load_sub_with(Loader &);
 
+       /** Sets the actions to be used when loading.  If the map is empty,
+       init_actions will be called. */
+       void set_actions(ActionMap &);
+       virtual void init_actions() { }
+
        /** Adds a keyword that is loaded by calling a function. */
        template<typename L>
        void add(const std::string &k, void (L::*func)())
@@ -99,21 +109,19 @@ protected:
        void add(const std::string &k, void (L::*func)(A0))
        { add(k, new LoaderFunc1<L, A0>(func)); }
 
-       template<typename L, typename A0, typename A1>
-       void add(const std::string &k, void (L::*func)(A0, A1))
-       { add(k, new LoaderFunc2<L, A0, A1>(func)); }
-
-       template<typename L, typename A0, typename A1, typename A2>
-       void add(const std::string &k, void (L::*func)(A0, A1, A2))
-       { add(k, new LoaderFunc3<L, A0, A1, A2>(func)); }
+       template<typename L, typename... Args>
+       void add(const std::string &k, void (L::*func)(Args...))
+       { add(k, new LoaderFuncN<L, Args...>(func)); }
 
-       template<typename L, typename A0, typename A1, typename A2, typename A3>
-       void add(const std::string &k, void (L::*func)(A0, A1, A2, A3))
-       { add(k, new LoaderFunc4<L, A0, A1, A2, A3>(func)); }
+       /** Adds a keyword that is loaded by calling a function with a bound
+       first argument. */
+       template<typename L, typename B0, typename... Args>
+       void add(const std::string &k, void (L::*func)(B0, Args...), const typename std::remove_reference<B0>::type &b0)
+       { add(k, new LoaderFuncNBound1<L, B0, Args...>(func, b0)); }
 
-       template<typename L, typename A0, typename A1, typename A2, typename A3, typename A4>
-       void add(const std::string &k, void (L::*func)(A0, A1, A2, A3, A4))
-       { add(k, new LoaderFunc5<L, A0, A1, A2, A3, A4>(func)); }
+       template<typename L, typename B0, typename... Args>
+       void add(const std::string &k, void (L::*func)(B0, Args...), B0 &&b0)
+       { add(k, new LoaderFuncNBound1<L, B0, Args...>(func, std::forward<B0>(b0))); }
 
        /** Adds a keyword that is loaded into a member of the loaded object. */
        template<typename L, typename T0>
@@ -126,12 +134,17 @@ protected:
 
        /** Adds a keyword that is recognized but ignored. */
        void add(const std::string &k)
-       { add(k, 0); }
+       { add(k, nullptr); }
 
 private:
        void add(const std::string &, LoaderAction *);
 
-       LoaderAction *find_action(const ActionKey &) const;
+protected:
+       void add_auxiliary_loader(Loader &);
+
+private:
+       bool has_action(const StatementKey &) const;
+       LoaderAction *find_action(const StatementKey &) const;
 
 protected:
        /** Returns the source of the statement being processed.  This can be used
@@ -139,30 +152,58 @@ protected:
        source may not necessarily be a file. */
        const std::string &get_source() const;
 
+       /** Returns the keyword of the statement being processed.  Can be used to
+       implement dynamic keywords. */
+       const std::string &get_keyword() const;
+
        virtual void finish() { }
 };
 
 
 /**
-Loads an object from a file.  The object must have a public Loader class.
+Loads an object from a file.  The object must have a public Loader class.  Any
+extra arguments are passed to the Loader constructor.
 */
-template<typename T>
-void load(T &obj, const std::string &fn)
+template<typename T, typename... Args>
+void load(T &obj, const std::string &fn, Args &&... args)
 {
        IO::BufferedFile in(fn);
 
        Parser parser(in, fn);
-       typename T::Loader loader(obj);
+       typename T::Loader loader(obj, std::forward<Args>(args)...);
        loader.load(parser);
 }
 
-template<typename T, typename U>
-void load(T &obj, const std::string &fn, U &arg)
+/**
+Loads an object from a file stored in a collection.  The object must have a
+public Loader class.  The collection is passed to the Loader constructor,
+followed by any extra arguments.
+*/
+template<typename T, typename... Args>
+void load(T &obj, typename T::Loader::Collection &coll, const std::string &fn, Args &&... args)
 {
-       IO::BufferedFile in(fn);
+       RefPtr<IO::Seekable> in = coll.open_raw(fn);
+       if(!in)
+               throw IO::file_not_found(fn);
 
-       Parser parser(in, fn);
-       typename T::Loader loader(obj, arg);
+       Parser parser(*in, fn);
+       typename T::Loader loader(obj, coll, std::forward<Args>(args)...);
+       loader.load(parser);
+}
+
+/**
+Loads an object from a file stored in a collection.  The object must have a
+public Loader class.  Any extra arguments are passed to the Loader constructor.
+*/
+template<typename T, typename C, typename... Args>
+typename std::enable_if<!NeedsCollection<typename T::Loader>::value>::type load(T &obj, C &coll, const std::string &fn, Args &&... args)
+{
+       RefPtr<IO::Seekable> in = coll.open_raw(fn);
+       if(!in)
+               throw IO::file_not_found(fn);
+
+       Parser parser(*in, fn);
+       typename T::Loader loader(obj, std::forward<Args>(args)...);
        loader.load(parser);
 }