From: Mikko Rasa Date: Sat, 20 Feb 2010 19:05:09 +0000 (+0000) Subject: Add some optional (for now) extra checks for Loader X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=e2a4cefe59dd3e6e1b2fac2fb7232326bb2b0787;p=libs%2Fdatafile.git Add some optional (for now) extra checks for Loader Remove argument count checks from LoaderActions, the signatures take care of that now --- diff --git a/source/loader.cpp b/source/loader.cpp index 6da8599..de9323a 100644 --- a/source/loader.cpp +++ b/source/loader.cpp @@ -51,6 +51,12 @@ bool signature_match(const string &st_sig, const string &act_sig) namespace Msp { namespace DataFile { +Loader::Loader(): + cur_st(0), + allow_pointer_reload(true), + check_sub_loads(false) +{ } + Loader::~Loader() { for(ActionMap::iterator i = actions.begin(); i!=actions.end(); ++i) @@ -83,7 +89,12 @@ void Loader::load_statement(const Statement &st) { LoaderAction *act = find_action(ActionKey(st.keyword, st.get_signature())); if(act) + { + sub_loaded = false; act->execute(*this, st); + if(check_sub_loads && !st.sub.empty() && !sub_loaded) + throw Exception("Substatements were not loaded"); + } } catch(Exception &e) { @@ -103,6 +114,7 @@ void Loader::load_sub_with(Loader &ldr) throw InvalidState("load_sub called without current statement"); ldr.load(*cur_st); + sub_loaded = true; } void Loader::add(const string &kwd, LoaderAction *act) diff --git a/source/loader.h b/source/loader.h index 6f7e614..126de57 100644 --- a/source/loader.h +++ b/source/loader.h @@ -56,14 +56,19 @@ private: typedef std::map ActionMap; - ActionMap actions; + ActionMap actions; const Statement *cur_st; - + bool sub_loaded; protected: - Loader(): cur_st(0) { } + bool allow_pointer_reload; + bool check_sub_loads; + + Loader(); public: virtual ~Loader(); + bool is_pointer_reload_allowed() const { return allow_pointer_reload; } + /** Loads statements from a parser. */ void load(Parser &p); diff --git a/source/loaderaction.h b/source/loaderaction.h index cfbe281..03a9da7 100644 --- a/source/loaderaction.h +++ b/source/loaderaction.h @@ -47,9 +47,8 @@ private: public: LoaderFunc0(FuncType f): func(f) { } - virtual void execute(Loader &l, const Statement &st) const + virtual void execute(Loader &l, const Statement &) const { - if(st.args.size()!=0) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(); }; @@ -74,7 +73,6 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get()); } @@ -153,7 +151,6 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get()); } @@ -180,7 +177,6 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=3) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get()); } @@ -208,7 +204,6 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=4) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get()); } @@ -237,7 +232,6 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=5) throw TypeError("Wrong number of arguments"); (dynamic_cast(l).*func)(st.args[0].get(), st.args[1].get(), st.args[2].get(), st.args[3].get(), st.args[4].get()); } @@ -267,8 +261,7 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); - dynamic_cast(l).get_object().*ptr0=st.args[0].get(); + dynamic_cast(l).get_object().*ptr0 = st.args[0].get(); } virtual std::string get_signature() const @@ -289,9 +282,10 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=1) throw TypeError("Wrong number of arguments"); - typename L::Loader &ldr=dynamic_cast(l); - ldr.get_object().*ptr0=ldr.get_collection().template get(st.args[0].get()); + typename L::Loader &ldr = dynamic_cast(l); + if(!ldr.is_pointer_reload_allowed() && ldr.get_object().*ptr0) + throw InvalidState("The pointer has already been loaded"); + ldr.get_object().*ptr0 = ldr.get_collection().template get(st.args[0].get()); } virtual std::string get_signature() const @@ -314,9 +308,8 @@ public: virtual void execute(Loader &l, const Statement &st) const { - if(st.args.size()!=2) throw TypeError("Wrong number of arguments"); - dynamic_cast(l).get_object().*ptr0=st.args[0].get(); - dynamic_cast(l).get_object().*ptr1=st.args[1].get(); + dynamic_cast(l).get_object().*ptr0 = st.args[0].get(); + dynamic_cast(l).get_object().*ptr1 = st.args[1].get(); } virtual std::string get_signature() const