Skip to content

Commit

Permalink
gras: added arg check with nice warning to callable and factory
Browse files Browse the repository at this point in the history
  • Loading branch information
guruofquality committed Jul 28, 2013
1 parent 9ee559e commit a3b5a07
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 36 deletions.
45 changes: 23 additions & 22 deletions include/gras/detail/callable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/***********************************************************************
Expand All @@ -30,7 +31,7 @@ struct CallableRegistryEntryImpl0 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 0) throw a;
this->arg_check(a, 0);
return PMC_M((_obj->*_fcn)());
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -53,7 +54,7 @@ struct CallableRegistryEntryImplVoid0 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 0) throw a;
this->arg_check(a, 0);
(_obj->*_fcn)(); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -79,7 +80,7 @@ struct CallableRegistryEntryImpl1 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 1) throw a;
this->arg_check(a, 1);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -102,7 +103,7 @@ struct CallableRegistryEntryImplVoid1 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 1) throw a;
this->arg_check(a, 1);
(_obj->*_fcn)(a[0].safe_as<A0>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -128,7 +129,7 @@ struct CallableRegistryEntryImpl2 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 2) throw a;
this->arg_check(a, 2);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -151,7 +152,7 @@ struct CallableRegistryEntryImplVoid2 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 2) throw a;
this->arg_check(a, 2);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -177,7 +178,7 @@ struct CallableRegistryEntryImpl3 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 3) throw a;
this->arg_check(a, 3);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -200,7 +201,7 @@ struct CallableRegistryEntryImplVoid3 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 3) throw a;
this->arg_check(a, 3);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -226,7 +227,7 @@ struct CallableRegistryEntryImpl4 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 4) throw a;
this->arg_check(a, 4);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -249,7 +250,7 @@ struct CallableRegistryEntryImplVoid4 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 4) throw a;
this->arg_check(a, 4);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -275,7 +276,7 @@ struct CallableRegistryEntryImpl5 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 5) throw a;
this->arg_check(a, 5);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -298,7 +299,7 @@ struct CallableRegistryEntryImplVoid5 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 5) throw a;
this->arg_check(a, 5);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -324,7 +325,7 @@ struct CallableRegistryEntryImpl6 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 6) throw a;
this->arg_check(a, 6);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -347,7 +348,7 @@ struct CallableRegistryEntryImplVoid6 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 6) throw a;
this->arg_check(a, 6);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -373,7 +374,7 @@ struct CallableRegistryEntryImpl7 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 7) throw a;
this->arg_check(a, 7);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -396,7 +397,7 @@ struct CallableRegistryEntryImplVoid7 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 7) throw a;
this->arg_check(a, 7);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -422,7 +423,7 @@ struct CallableRegistryEntryImpl8 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 8) throw a;
this->arg_check(a, 8);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -445,7 +446,7 @@ struct CallableRegistryEntryImplVoid8 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 8) throw a;
this->arg_check(a, 8);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -471,7 +472,7 @@ struct CallableRegistryEntryImpl9 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 9) throw a;
this->arg_check(a, 9);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>(), a[8].safe_as<A8>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -494,7 +495,7 @@ struct CallableRegistryEntryImplVoid9 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 9) throw a;
this->arg_check(a, 9);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>(), a[8].safe_as<A8>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -520,7 +521,7 @@ struct CallableRegistryEntryImpl10 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 10) throw a;
this->arg_check(a, 10);
return PMC_M((_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>(), a[8].safe_as<A8>(), a[9].safe_as<A9>()));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -543,7 +544,7 @@ struct CallableRegistryEntryImplVoid10 : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 10) throw a;
this->arg_check(a, 10);
(_obj->*_fcn)(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>(), a[8].safe_as<A8>(), a[9].safe_as<A9>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand Down
23 changes: 12 additions & 11 deletions include/gras/detail/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

/***********************************************************************
Expand All @@ -29,7 +30,7 @@ struct FactoryRegistryEntryImpl0 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 0) throw a;
this->arg_check(a, 0);
return _fcn();
}
Fcn _fcn;
Expand All @@ -53,7 +54,7 @@ struct FactoryRegistryEntryImpl1 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 1) throw a;
this->arg_check(a, 1);
return _fcn(a[0].safe_as<A0>());
}
Fcn _fcn;
Expand All @@ -77,7 +78,7 @@ struct FactoryRegistryEntryImpl2 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 2) throw a;
this->arg_check(a, 2);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>());
}
Fcn _fcn;
Expand All @@ -101,7 +102,7 @@ struct FactoryRegistryEntryImpl3 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 3) throw a;
this->arg_check(a, 3);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>());
}
Fcn _fcn;
Expand All @@ -125,7 +126,7 @@ struct FactoryRegistryEntryImpl4 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 4) throw a;
this->arg_check(a, 4);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>());
}
Fcn _fcn;
Expand All @@ -149,7 +150,7 @@ struct FactoryRegistryEntryImpl5 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 5) throw a;
this->arg_check(a, 5);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>());
}
Fcn _fcn;
Expand All @@ -173,7 +174,7 @@ struct FactoryRegistryEntryImpl6 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 6) throw a;
this->arg_check(a, 6);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>());
}
Fcn _fcn;
Expand All @@ -197,7 +198,7 @@ struct FactoryRegistryEntryImpl7 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 7) throw a;
this->arg_check(a, 7);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>());
}
Fcn _fcn;
Expand All @@ -221,7 +222,7 @@ struct FactoryRegistryEntryImpl8 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 8) throw a;
this->arg_check(a, 8);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>());
}
Fcn _fcn;
Expand All @@ -245,7 +246,7 @@ struct FactoryRegistryEntryImpl9 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 9) throw a;
this->arg_check(a, 9);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>(), a[8].safe_as<A8>());
}
Fcn _fcn;
Expand All @@ -269,7 +270,7 @@ struct FactoryRegistryEntryImpl10 : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < 10) throw a;
this->arg_check(a, 10);
return _fcn(a[0].safe_as<A0>(), a[1].safe_as<A1>(), a[2].safe_as<A2>(), a[3].safe_as<A3>(), a[4].safe_as<A4>(), a[5].safe_as<A5>(), a[6].safe_as<A6>(), a[7].safe_as<A7>(), a[8].safe_as<A8>(), a[9].safe_as<A9>());
}
Fcn _fcn;
Expand Down
7 changes: 7 additions & 0 deletions lib/callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <gras/callable.hpp>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/format.hpp>
#include <stdexcept>
#include <map>

Expand Down Expand Up @@ -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()));
}
7 changes: 7 additions & 0 deletions lib/factory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include <gras/factory.hpp>
#include <boost/format.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <stdexcept>
Expand All @@ -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<std::string, boost::shared_ptr<FactoryRegistryEntry> > FactoryRegistryType;

static FactoryRegistryType &get_factory_registry(void)
Expand Down
5 changes: 3 additions & 2 deletions tmpl/callable_detail.tmpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -31,7 +32,7 @@ struct CallableRegistryEntryImpl$(NARGS) : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < $NARGS) throw a;
this->arg_check(a, $NARGS);
return PMC_M((_obj->*_fcn)($expand('a[%d].safe_as<A%d>()', $NARGS)));
}
ClassType *_obj; Fcn _fcn;
Expand All @@ -54,7 +55,7 @@ struct CallableRegistryEntryImplVoid$(NARGS) : CallableRegistryEntry
PMCC call(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < $NARGS) throw a;
this->arg_check(a, $NARGS);
(_obj->*_fcn)($expand('a[%d].safe_as<A%d>()', $NARGS)); return PMCC();
}
ClassType *_obj; Fcn _fcn;
Expand Down
3 changes: 2 additions & 1 deletion tmpl/factory_detail.tmpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -30,7 +31,7 @@ struct FactoryRegistryEntryImpl$(NARGS) : FactoryRegistryEntry
Element *make(const PMCC &args)
{
const PMCList &a = args.as<PMCList>();
if (a.size() < $NARGS) throw a;
this->arg_check(a, $NARGS);
return _fcn($expand('a[%d].safe_as<A%d>()', $NARGS));
}
Fcn _fcn;
Expand Down

0 comments on commit a3b5a07

Please sign in to comment.