From a3b5a07cf7c64f1a038839d3ef2f7b1d9fbf6c42 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 27 Jul 2013 19:20:35 -0700 Subject: gras: added arg check with nice warning to callable and factory --- include/gras/detail/callable.hpp | 45 ++++++++++++++++++++-------------------- include/gras/detail/factory.hpp | 23 ++++++++++---------- lib/callable.cpp | 7 +++++++ lib/factory.cpp | 7 +++++++ tmpl/callable_detail.tmpl.hpp | 5 +++-- tmpl/factory_detail.tmpl.hpp | 3 ++- 6 files changed, 54 insertions(+), 36 deletions(-) diff --git a/include/gras/detail/callable.hpp b/include/gras/detail/callable.hpp index 68766fe..a2e9af4 100644 --- a/include/gras/detail/callable.hpp +++ b/include/gras/detail/callable.hpp @@ -16,6 +16,7 @@ struct GRAS_API CallableRegistryEntry CallableRegistryEntry(void); virtual ~CallableRegistryEntry(void); virtual PMCC call(const PMCC &args) = 0; + void arg_check(const PMCList &args, const size_t nargs); }; /*********************************************************************** @@ -30,7 +31,7 @@ struct CallableRegistryEntryImpl0 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 0) throw a; + this->arg_check(a, 0); return PMC_M((_obj->*_fcn)()); } ClassType *_obj; Fcn _fcn; @@ -53,7 +54,7 @@ struct CallableRegistryEntryImplVoid0 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 0) throw a; + this->arg_check(a, 0); (_obj->*_fcn)(); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -79,7 +80,7 @@ struct CallableRegistryEntryImpl1 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 1) throw a; + this->arg_check(a, 1); return PMC_M((_obj->*_fcn)(a[0].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -102,7 +103,7 @@ struct CallableRegistryEntryImplVoid1 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 1) throw a; + this->arg_check(a, 1); (_obj->*_fcn)(a[0].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -128,7 +129,7 @@ struct CallableRegistryEntryImpl2 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 2) throw a; + this->arg_check(a, 2); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -151,7 +152,7 @@ struct CallableRegistryEntryImplVoid2 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 2) throw a; + this->arg_check(a, 2); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -177,7 +178,7 @@ struct CallableRegistryEntryImpl3 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 3) throw a; + this->arg_check(a, 3); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -200,7 +201,7 @@ struct CallableRegistryEntryImplVoid3 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 3) throw a; + this->arg_check(a, 3); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -226,7 +227,7 @@ struct CallableRegistryEntryImpl4 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 4) throw a; + this->arg_check(a, 4); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -249,7 +250,7 @@ struct CallableRegistryEntryImplVoid4 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 4) throw a; + this->arg_check(a, 4); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -275,7 +276,7 @@ struct CallableRegistryEntryImpl5 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 5) throw a; + this->arg_check(a, 5); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -298,7 +299,7 @@ struct CallableRegistryEntryImplVoid5 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 5) throw a; + this->arg_check(a, 5); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -324,7 +325,7 @@ struct CallableRegistryEntryImpl6 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 6) throw a; + this->arg_check(a, 6); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -347,7 +348,7 @@ struct CallableRegistryEntryImplVoid6 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 6) throw a; + this->arg_check(a, 6); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -373,7 +374,7 @@ struct CallableRegistryEntryImpl7 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 7) throw a; + this->arg_check(a, 7); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -396,7 +397,7 @@ struct CallableRegistryEntryImplVoid7 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 7) throw a; + this->arg_check(a, 7); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -422,7 +423,7 @@ struct CallableRegistryEntryImpl8 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 8) throw a; + this->arg_check(a, 8); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -445,7 +446,7 @@ struct CallableRegistryEntryImplVoid8 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 8) throw a; + this->arg_check(a, 8); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -471,7 +472,7 @@ struct CallableRegistryEntryImpl9 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 9) throw a; + this->arg_check(a, 9); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as(), a[8].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -494,7 +495,7 @@ struct CallableRegistryEntryImplVoid9 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 9) throw a; + this->arg_check(a, 9); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as(), a[8].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; @@ -520,7 +521,7 @@ struct CallableRegistryEntryImpl10 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 10) throw a; + this->arg_check(a, 10); return PMC_M((_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as(), a[8].safe_as(), a[9].safe_as())); } ClassType *_obj; Fcn _fcn; @@ -543,7 +544,7 @@ struct CallableRegistryEntryImplVoid10 : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 10) throw a; + this->arg_check(a, 10); (_obj->*_fcn)(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as(), a[8].safe_as(), a[9].safe_as()); return PMCC(); } ClassType *_obj; Fcn _fcn; diff --git a/include/gras/detail/factory.hpp b/include/gras/detail/factory.hpp index a2de116..ac4df6f 100644 --- a/include/gras/detail/factory.hpp +++ b/include/gras/detail/factory.hpp @@ -16,6 +16,7 @@ struct GRAS_API FactoryRegistryEntry FactoryRegistryEntry(void); virtual ~FactoryRegistryEntry(void); virtual Element *make(const PMCC &args) = 0; + void arg_check(const PMCList &args, const size_t nargs); }; /*********************************************************************** @@ -29,7 +30,7 @@ struct FactoryRegistryEntryImpl0 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 0) throw a; + this->arg_check(a, 0); return _fcn(); } Fcn _fcn; @@ -53,7 +54,7 @@ struct FactoryRegistryEntryImpl1 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 1) throw a; + this->arg_check(a, 1); return _fcn(a[0].safe_as()); } Fcn _fcn; @@ -77,7 +78,7 @@ struct FactoryRegistryEntryImpl2 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 2) throw a; + this->arg_check(a, 2); return _fcn(a[0].safe_as(), a[1].safe_as()); } Fcn _fcn; @@ -101,7 +102,7 @@ struct FactoryRegistryEntryImpl3 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 3) throw a; + this->arg_check(a, 3); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as()); } Fcn _fcn; @@ -125,7 +126,7 @@ struct FactoryRegistryEntryImpl4 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 4) throw a; + this->arg_check(a, 4); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as()); } Fcn _fcn; @@ -149,7 +150,7 @@ struct FactoryRegistryEntryImpl5 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 5) throw a; + this->arg_check(a, 5); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as()); } Fcn _fcn; @@ -173,7 +174,7 @@ struct FactoryRegistryEntryImpl6 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 6) throw a; + this->arg_check(a, 6); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as()); } Fcn _fcn; @@ -197,7 +198,7 @@ struct FactoryRegistryEntryImpl7 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 7) throw a; + this->arg_check(a, 7); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as()); } Fcn _fcn; @@ -221,7 +222,7 @@ struct FactoryRegistryEntryImpl8 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 8) throw a; + this->arg_check(a, 8); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as()); } Fcn _fcn; @@ -245,7 +246,7 @@ struct FactoryRegistryEntryImpl9 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 9) throw a; + this->arg_check(a, 9); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as(), a[8].safe_as()); } Fcn _fcn; @@ -269,7 +270,7 @@ struct FactoryRegistryEntryImpl10 : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < 10) throw a; + this->arg_check(a, 10); return _fcn(a[0].safe_as(), a[1].safe_as(), a[2].safe_as(), a[3].safe_as(), a[4].safe_as(), a[5].safe_as(), a[6].safe_as(), a[7].safe_as(), a[8].safe_as(), a[9].safe_as()); } Fcn _fcn; diff --git a/lib/callable.cpp b/lib/callable.cpp index a7bc7da..59ada7f 100644 --- a/lib/callable.cpp +++ b/lib/callable.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -62,3 +63,9 @@ CallableRegistryEntry::~CallableRegistryEntry(void) { //NOP } + +void CallableRegistryEntry::arg_check(const PMCList &args, const size_t nargs) +{ + if (args.size() != nargs) throw std::runtime_error(str(boost::format( + "callable expected %u arguments but for %u") % nargs % args.size())); +} diff --git a/lib/factory.cpp b/lib/factory.cpp index 5c405f3..35b1b24 100644 --- a/lib/factory.cpp +++ b/lib/factory.cpp @@ -1,6 +1,7 @@ // Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. #include +#include #include #include #include @@ -18,6 +19,12 @@ FactoryRegistryEntry::~FactoryRegistryEntry(void) //NOP } +void FactoryRegistryEntry::arg_check(const PMCList &args, const size_t nargs) +{ + if (args.size() != nargs) throw std::runtime_error(str(boost::format( + "factory expected %u arguments but for %u") % nargs % args.size())); +} + typedef std::map > FactoryRegistryType; static FactoryRegistryType &get_factory_registry(void) diff --git a/tmpl/callable_detail.tmpl.hpp b/tmpl/callable_detail.tmpl.hpp index 65750e7..656b695 100644 --- a/tmpl/callable_detail.tmpl.hpp +++ b/tmpl/callable_detail.tmpl.hpp @@ -16,6 +16,7 @@ struct GRAS_API CallableRegistryEntry CallableRegistryEntry(void); virtual ~CallableRegistryEntry(void); virtual PMCC call(const PMCC &args) = 0; + void arg_check(const PMCList &args, const size_t nargs); }; #for $NARGS in range($MAX_ARGS) @@ -31,7 +32,7 @@ struct CallableRegistryEntryImpl$(NARGS) : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < $NARGS) throw a; + this->arg_check(a, $NARGS); return PMC_M((_obj->*_fcn)($expand('a[%d].safe_as()', $NARGS))); } ClassType *_obj; Fcn _fcn; @@ -54,7 +55,7 @@ struct CallableRegistryEntryImplVoid$(NARGS) : CallableRegistryEntry PMCC call(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < $NARGS) throw a; + this->arg_check(a, $NARGS); (_obj->*_fcn)($expand('a[%d].safe_as()', $NARGS)); return PMCC(); } ClassType *_obj; Fcn _fcn; diff --git a/tmpl/factory_detail.tmpl.hpp b/tmpl/factory_detail.tmpl.hpp index 87a0756..ea30235 100644 --- a/tmpl/factory_detail.tmpl.hpp +++ b/tmpl/factory_detail.tmpl.hpp @@ -16,6 +16,7 @@ struct GRAS_API FactoryRegistryEntry FactoryRegistryEntry(void); virtual ~FactoryRegistryEntry(void); virtual Element *make(const PMCC &args) = 0; + void arg_check(const PMCList &args, const size_t nargs); }; #for $NARGS in range($MAX_ARGS) @@ -30,7 +31,7 @@ struct FactoryRegistryEntryImpl$(NARGS) : FactoryRegistryEntry Element *make(const PMCC &args) { const PMCList &a = args.as(); - if (a.size() < $NARGS) throw a; + this->arg_check(a, $NARGS); return _fcn($expand('a[%d].safe_as()', $NARGS)); } Fcn _fcn; -- cgit