I have derived FunctionConstraint from from Constraint<T> (TCLAP version 1.2.1)
The constructor FunctionConstraint(bool (*f)(T), std::string desc) accept a function of the kind "bool func(T val)" and a string "desc" which describe which kind of contraints are implemented in "func".
Then bool FunctionConstraint<T>::check( const T& val ) simply calls f(val) and return the bool according to the implementation of "func".
For an example on the use see below.
FunctionConstraint.h is attached. In CmdLine.h at line 42 "#include <tclap/FunctionConstraint.h>" must be added
I haven't done a proper debugging, but I've tested the new class with a bunch of different functions and different command line arguments and worked.
I hope that this new class will be included in future releases of TCLAP.
Cheers,
Francesco
########################
Example
#include ....
template<class T>
bool range(T val){ //function which returns true if 'val' is in the range, false otherwise
if( val>= 3 && val < 15) return true;
else return false;
}
std::string descr = "value in [3, 15)"; //descrition of the constraint
int main(int argc, char* argv[])
{
TCLAP::CmdLine cmd("test", ' ', "1"); //command line object
TCLAP::ValueArg<double> foo("f", "foo", "fooing around", false, 0.2, "double"); //foo
cmd.add(foo);
TCLAP::FunctionConstraint<int> choiserange(range, descr); //FunctionConstraint object
TCLAP::ValueArg<int> counts("c", "counts", "Number of counts", false, 0, &choiserange); //pass it to ValueArg
cmd.add(counts);
cmd.parse(argc, argv); //parse
std::cout << foo.getValue() << std::endl; //print out
std::cout << counts.getValue() << std::endl;
exit(0);
}
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
Class implementing contraints from a custom function
View and moderate all "patches Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Patches"
I forgot to say that following two files must also be changed
include/Makefile.am, line 26, add "FunctionConstraint.h/"
include/Makefile.in, line 175, add "FunctionConstraint.h/"
Is there any reason why this patch was ignored? I think that the library would benefit from some pre-made constraint classes.
I think the idea is good, but the patch requires more work.
1) It should support a function object rather than just function pointer
2) There needs to be example/tests added
If you're interrested in contributing to such a patch and creating a pull request I'll be happy to review and merge it once done. Otherwise I may end up doing it when I'm bored enough, which may be never :)
The only reason this has been ignored is my lack of time or energy to dedicate to tclap. I agree that this patch would be useful.
I'll try to take a look in the weekend.