]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/loader.h
Allow overloading keywords with different signatures
[libs/datafile.git] / source / loader.h
index 0c1dda2cc776db8d293b8696ba6e0e4739aab016..5c8726a179683c841e6439e57fd3cfa4772394e2 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspdatafile
-Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
+Copyright © 2006-2008, 2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -44,24 +44,60 @@ See also classes BasicLoader and BasicLoader2.
 class Loader
 {
 private:
-       /**
-       Loads data from a statement.
-       */
-       void load(const Statement &st);
+       struct ActionKey
+       {
+               std::string keyword;
+               std::string signature;
+
+               ActionKey(const std::string &, const std::string &);
+
+               bool operator<(const ActionKey &) const;
+       };
+
+       typedef std::map<ActionKey, LoaderAction *> ActionMap;
+
+       ActionMap       actions;
+       const Statement *cur_st;
 
+protected:
+       Loader(): cur_st(0) { }
 public:
-       /**
-       Loads statements from a parser.
-       */
+       virtual ~Loader();
+
+       /** Loads statements from a parser. */
        void load(Parser &p);
 
-       virtual ~Loader();
+private:
+       /** Loads data from a statement. */
+       void load(const Statement &st);
+
+       /** Processes a single statement */
+       void load_statement(const Statement &st);
+
 protected:
-       Loader(): cur_st(0) { }
+       /** Loads a sub-object from the statement being processed.  The Loader class
+       of the sub-object is automatically used. */
+       template<typename S>
+       void load_sub(S &s)
+       {
+               typename S::Loader ldr(s);
+               load_sub_with(ldr);
+       }
+
+       /** Loads a sub-object from the statement being processed with an extra
+       parameter 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)
+       {
+               typename S::Loader ldr(s, p);
+               load_sub_with(ldr);
+       }
+
+       /** Processes the current statement's substatements with another Loader. */
+       void load_sub_with(Loader &);
 
-       /**
-       Adds a keyword that is loaded with a zero-argument function.
-       */
+       /** Adds a keyword that is loaded by calling a function. */
        template<typename L>
        void add(const std::string &k, void (L::*func)())
        { add(k, new LoaderFunc0<L>(func)); }
@@ -86,9 +122,7 @@ protected:
        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)); }
 
-       /**
-       Adds a keyword that is loaded into a variable of the loaded object.
-       */
+       /** Adds a keyword that is loaded into a member of the loaded object. */
        template<typename L, typename T0>
        void add(const std::string &k, T0 L::*p0)
        { add(k, new LoadValue1<L, T0>(p0)); }
@@ -97,44 +131,19 @@ protected:
        void add(const std::string &k, T0 L::*p0, T1 L::*p1)
        { add(k, new LoadValue2<L, T0, T1>(p0, p1)); }
 
-       /**
-       Adds a keyword that is recognized but ignored.
-       */
+       /** Adds a keyword that is recognized but ignored. */
        void add(const std::string &k)
        { add(k, 0); }
 
-       /**
-       Loads a sub-object from the statement being processed.  The Loader class of
-       the sub-object is automatically used.
-       */
-       template<typename S>
-       void load_sub(S &s)
-       {
-               typename S::Loader ldr(s);
-               load_sub_with(ldr);
-       }
-
-       /**
-       Loads a sub-object from the statement being processed with an extra parameter
-       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)
-       {
-               typename S::Loader ldr(s, p);
-               load_sub_with(ldr);
-       }
+private:
+       void add(const std::string &, LoaderAction *);
 
-       /**
-       Processes the current statement's substatements with another Loader.
-       */
-       void load_sub_with(Loader &);
+       LoaderAction *find_action(const ActionKey &) const;
 
-       /**
-       Returns the source of the statement being processed.  This can be used to
-       implement relative paths in include-like statements.  Note that the source
-       may not necessarily be a file.
-       */
+protected:
+       /** Returns the source of the statement being processed.  This can be used
+       to implement relative paths in include-like statements.  Note that the
+       source may not necessarily be a file. */
        const std::string &get_source() const
        {
                if(!cur_st)
@@ -143,14 +152,6 @@ protected:
        }
 
        virtual void finish() { }
-private:
-       typedef std::map<std::string, LoaderAction *> ActionMap;
-
-       ActionMap       actions;
-       const Statement *cur_st;
-
-       void add(const std::string &, LoaderAction *);
-       void load_statement(const Statement &st);
 };