From c425ae7693b604c190d3904e7ef965f93d34edee Mon Sep 17 00:00:00 2001 From: schodet Date: Sun, 2 Apr 2006 17:23:53 +0000 Subject: Support des fonctions qui ne retournent pas de bool. Support du dry run. --- i/marvin/src/interpreter/interpreter.cc | 7 ++-- i/marvin/src/interpreter/interpreter.hh | 5 +-- i/marvin/src/interpreter/interpreter.tcc | 53 +++++++++++++++------------- i/marvin/src/interpreter/test_interpreter.cc | 16 ++++----- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/i/marvin/src/interpreter/interpreter.cc b/i/marvin/src/interpreter/interpreter.cc index 4b034e3..a7f905c 100644 --- a/i/marvin/src/interpreter/interpreter.cc +++ b/i/marvin/src/interpreter/interpreter.cc @@ -47,8 +47,9 @@ Interpreter::add (const std::string &s, Func *f) } /// Call a function by name. -bool -Interpreter::call (const std::string &s, const Args &a) const +void +Interpreter::call (const std::string &s, const Args &a, + bool dryrun/*false*/) const { Funcs::const_iterator i; i = funcs_.find (s); @@ -56,7 +57,7 @@ Interpreter::call (const std::string &s, const Args &a) const throw std::runtime_error ("function \'" + s + "\' does not exist"); try { - return (*i->second) (a); + (*i->second) (a, dryrun); } catch (const std::exception &e) { diff --git a/i/marvin/src/interpreter/interpreter.hh b/i/marvin/src/interpreter/interpreter.hh index 729f321..169e0f2 100644 --- a/i/marvin/src/interpreter/interpreter.hh +++ b/i/marvin/src/interpreter/interpreter.hh @@ -44,7 +44,7 @@ class Interpreter { } /// Called on function invocation, getting the argument list and /// returning true on success. - virtual bool operator() (const Args &) = 0; + virtual void operator() (const Args &, bool dryrun) = 0; }; private: /// Func class construction helper. @@ -61,7 +61,8 @@ class Interpreter /// Add a function, Interpreter owns f. void add (const std::string &s, Func *f); /// Call a function by name. - bool call (const std::string &s, const Args &a) const; + void call (const std::string &s, const Args &a, + bool dryrun = false) const; /// Take all the template sophistications out of the programmer hands. template static Func *memFunc (T &i, F f); diff --git a/i/marvin/src/interpreter/interpreter.tcc b/i/marvin/src/interpreter/interpreter.tcc index 0e4fcf9..5af0e0a 100644 --- a/i/marvin/src/interpreter/interpreter.tcc +++ b/i/marvin/src/interpreter/interpreter.tcc @@ -36,85 +36,89 @@ class Interpreter::MemFunc /// Partially specialised for member functions taking an arguments list. template -class Interpreter::MemFunc : public Func +class Interpreter::MemFunc +: public Func { T &i_; - typedef bool (T::*F) (const Interpreter::Args &); + typedef void (T::*F) (const Interpreter::Args &, bool); F f_; public: MemFunc (T &i, F f) : i_ (i), f_ (f) { } - bool operator() (const Args &a) + void operator() (const Args &a, bool dryrun) { - return (i_.*f_) (a); + (i_.*f_) (a, dryrun); } }; /// Partially specialised for members functions taking no argument. -template -class Interpreter::MemFunc : public Func +template +class Interpreter::MemFunc : public Func { T &i_; - typedef bool (T::*F) (void); + typedef R (T::*F) (void); F f_; public: MemFunc (T &i, F f) : i_ (i), f_ (f) { } - bool operator() (const Args &a) + void operator() (const Args &a, bool dryrun) { if (!a.empty ()) throw std::runtime_error ("no argument expected"); - return (i_.*f_) (); + if (!dryrun) + (i_.*f_) (); } }; /// Partially specialised for members functions taking one argument. -template -class Interpreter::MemFunc : public Func +template +class Interpreter::MemFunc : public Func { T &i_; - typedef bool (T::*F) (A1); + typedef R (T::*F) (A1); F f_; public: MemFunc (T &i, F f) : i_ (i), f_ (f) { } - bool operator() (const Args &a) + void operator() (const Args &a, bool dryrun) { Args::const_iterator i = a.begin (); if (a.size () != 1) throw std::runtime_error ("one argument expected"); A1 a1 = any_cast::type> (*i); - return (i_.*f_) (a1); + if (!dryrun) + (i_.*f_) (a1); } }; /// Partially specialised for members functions taking two arguments. -template -class Interpreter::MemFunc : public Func +template +class Interpreter::MemFunc : public Func { T &i_; - typedef bool (T::*F) (A1, A2); + typedef R (T::*F) (A1, A2); F f_; public: MemFunc (T &i, F f) : i_ (i), f_ (f) { } - bool operator() (const Args &a) + void operator() (const Args &a, bool dryrun) { Args::const_iterator i = a.begin (); if (a.size () != 2) throw std::runtime_error ("two arguments expected"); A1 a1 = any_cast::type> (*i); A2 a2 = any_cast::type> (*++i); - return (i_.*f_) (a1, a2); + if (!dryrun) + (i_.*f_) (a1, a2); } }; /// Partially specialised for members functions taking three arguments. -template -class Interpreter::MemFunc : public Func +template +class Interpreter::MemFunc : public Func { T &i_; - typedef bool (T::*F) (A1, A2, A3); + typedef R (T::*F) (A1, A2, A3); F f_; public: MemFunc (T &i, F f) : i_ (i), f_ (f) { } - bool operator() (const Args &a) + void operator() (const Args &a, bool dryrun) { Args::const_iterator i = a.begin (); if (a.size () != 3) @@ -122,7 +126,8 @@ class Interpreter::MemFunc : public Func A1 a1 = any_cast::type> (*i); A2 a2 = any_cast::type> (*++i); A3 a3 = any_cast::type> (*++i); - return (i_.*f_) (a1, a2, a3); + if (!dryrun) + (i_.*f_) (a1, a2, a3); } }; diff --git a/i/marvin/src/interpreter/test_interpreter.cc b/i/marvin/src/interpreter/test_interpreter.cc index 99a9ad9..8c2eb88 100644 --- a/i/marvin/src/interpreter/test_interpreter.cc +++ b/i/marvin/src/interpreter/test_interpreter.cc @@ -32,30 +32,28 @@ class TestInterpreter { Interpreter t; public: - bool funcA (const Interpreter::Args &a) + void funcA (const Interpreter::Args &a, bool dryrun) { - std::cout << " a " << a << std::endl; - return true; + if (!dryrun) + std::cout << " a " << a << std::endl; } bool funcB (void) { std::cout << " b ( )" << std::endl; return true; } - bool funcC (int i) + int funcC (int i) { std::cout << " c ( " << i << " )" << std::endl; - return true; + return 0; } - bool funcD (const std::string &s) + void funcD (const std::string &s) { std::cout << " d ( " << s << " )" << std::endl; - return true; } - bool funcE (int i, const std::string &s, double d) + void funcE (int i, const std::string &s, double d) { std::cout << " e ( " << i << ' ' << s << ' ' << d << " )" << std::endl; - return true; } void call (const std::string &s, const Interpreter::Args &a) { -- cgit v1.2.3